feat: Implements common database interactions as dependencies.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import Dependencies
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
// TODO: Add update and get by id.
|
||||
struct UserApiController: RouteCollection {
|
||||
let users = UserDB()
|
||||
|
||||
@Dependency(\.users) var users
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let unProtected = routes.apiUnprotected(route: "users")
|
||||
@@ -19,7 +21,7 @@ struct UserApiController: RouteCollection {
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [User.DTO] {
|
||||
try await users.fetchAll(on: req.db)
|
||||
try await users.fetchAll()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
@@ -33,13 +35,13 @@ struct UserApiController: RouteCollection {
|
||||
}
|
||||
try User.Create.validate(content: req)
|
||||
let model = try req.content.decode(User.Create.self)
|
||||
return try await users.create(model, on: req.db)
|
||||
return try await users.create(model)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func login(req: Request) async throws -> UserToken {
|
||||
let user = try req.auth.require(User.self)
|
||||
return try await users.login(user: user, on: req.db)
|
||||
return try await users.login(user)
|
||||
}
|
||||
|
||||
// @Sendable
|
||||
@@ -53,7 +55,7 @@ struct UserApiController: RouteCollection {
|
||||
guard let id = req.parameters.get("id", as: User.IDValue.self) else {
|
||||
throw Abort(.badRequest, reason: "User id not provided")
|
||||
}
|
||||
try await users.delete(id: id, on: req.db)
|
||||
try await users.delete(id)
|
||||
return .ok
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user