Initial commit

This commit is contained in:
Johan
2026-01-26 00:37:35 +01:00
commit f303cb46b7
30 changed files with 2251 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
//
// UserAuthRepository.swift
// ExodaiAcademy
//
// Created by Exodai on 25/01/2026.
//
import Foundation
protocol UserAuthRepository {
func findByEmail(_ email: String) async throws -> UserModel?
func findByUsername(_ username: String) async throws -> UserModel?
func create(
username: String,
email: String,
passwordHash: String,
role: Role
) async throws -> UserModel
func updatePassword(userID: UUID, passwordHash: String) async throws
func delete(userID: UUID) async throws
}
import Fluent
import Vapor
struct FluentUserAuthRepository: UserAuthRepository {
let db: any FluentKit.Database
func findByEmail(_ email: String) async throws -> UserModel? {
try await UserModel.query(on: db)
.filter(\.$email == email)
.first()
}
func findByUsername(_ username: String) async throws -> UserModel? {
try await UserModel.query(on: db)
.filter(\.$username == username)
.first()
}
func create(username: String, email: String, passwordHash: String, role: Role) async throws -> UserModel {
let user = UserModel(
username: username,
email: email,
password: passwordHash,
role: role
)
try await user.create(on: db)
return user
}
func updatePassword(userID: UUID, passwordHash: String) async throws {
guard let user = try await UserModel.find(userID, on: db) else {
throw Abort(.notFound)
}
user.password = passwordHash
try await user.save(on: db)
}
func delete(userID: UUID) async throws {
try await UserModel.find(userID, on: db)?
.delete(on: db)
}
}

View File

@@ -0,0 +1,46 @@
//
// UserRepository.swift
// ExodaiAcademy
//
// Created by Exodai on 25/01/2026.
//
import Foundation
protocol UserRepository {
func find(id: UUID) async throws -> UserModel.Public?
func findByEmail(_ email: String) async throws -> UserModel.Public?
func findByUsername(_ username: String) async throws -> UserModel.Public?
func all() async throws -> [UserModel.Public]
}
import Fluent
struct FluentUserRepository: UserRepository {
let db: any FluentKit.Database
func find(id: UUID) async throws -> UserModel.Public? {
try await UserModel.find(id, on: db)?
.convertToPublic()
}
func findByEmail(_ email: String) async throws -> UserModel.Public? {
try await UserModel.query(on: db)
.filter(\.$email == email)
.first()?
.convertToPublic()
}
func findByUsername(_ username: String) async throws -> UserModel.Public? {
try await UserModel.query(on: db)
.filter(\.$username == username)
.first()?
.convertToPublic()
}
func all() async throws -> [UserModel.Public] {
try await UserModel.query(on: db)
.all()
.convertToPublic()
}
}