Files
vapor-po/Sources/App/Controllers/Api/UserApiController.swift

62 lines
1.6 KiB
Swift

import Dependencies
import Fluent
import Vapor
// TODO: Add update and get by id.
struct UserApiController: RouteCollection {
@Dependency(\.users) var users
func boot(routes: any RoutesBuilder) throws {
let unProtected = routes.apiUnprotected(route: "users")
let protected = routes.apiProtected(route: "users")
unProtected.post(use: create(req:))
protected.get(use: index(req:))
protected.get("login", use: login(req:))
protected.group(":id") {
$0.delete(use: delete(req:))
}
}
@Sendable
func index(req: Request) async throws -> [User.DTO] {
try await users.fetchAll()
}
@Sendable
func create(req: Request) async throws -> User.DTO {
// Allow the first user to be created without authentication.
let count = try await User.query(on: req.db).count()
if count > 0 {
guard req.auth.get(User.self) != nil else {
throw Abort(.unauthorized)
}
}
try User.Create.validate(content: req)
let model = try req.content.decode(User.Create.self)
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)
}
// @Sendable
// func get(req: Request) async throws -> User.DTO {
// guard let id = req.parameters.get("id", as: User.IDValue.self),
// let user = users.
// }
@Sendable
func delete(req: Request) async throws -> HTTPStatus {
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)
return .ok
}
}