import Fluent import Foundation import Vapor struct ApiController: RouteCollection { func boot(routes: any RoutesBuilder) throws { let api = routes.grouped("api") let users = api.grouped("users") let proCon = api.grouped("procons") let utils = api.grouped("utils") users.get(use: usersIndex(req:)) users.post(use: createUser(req:)) proCon.get(use: prosAndConsIndex(req:)) proCon.post(use: createProCon(req:)) proCon.delete(":id", use: deleteProCon(req:)) utils.post("check-words", use: checkWords(req:)) } @Sendable func usersIndex(req: Request) async throws -> [User] { try await User.query(on: req.db).all() } @Sendable func prosAndConsIndex(req: Request) async throws -> [ProCon] { try await ProCon.query(on: req.db).all() } @Sendable func createUser(req: Request) async throws -> User { let user = try req.content.decode(User.self) try await user.save(on: req.db) return user } @Sendable func createProCon(req: Request) async throws -> ProCon { let proconData = try req.content.decode(ProConDTO.self) let proCon = ProCon(type: proconData.type, description: proconData.description, userId: proconData.userId) try await proCon.create(on: req.db) return proCon } @Sendable func deleteProCon(req: Request) async throws -> HTTPStatus { guard let idString = req.parameters.get("id"), let id = UUID(uuidString: idString) else { throw Abort(.badRequest) } try await ProCon.query(on: req.db) .filter(\.$id == id) .delete() return .ok } @Sendable func checkWords(req: Request) async throws -> HTTPStatus { let input = try req.content.decode(CheckWords.self) try checkForBadWords(in: input.string) return .ok } } struct CheckWords: Content { let string: String } struct ProConDTO: Content { let id: UUID? let type: ProCon.ProConType let description: String let userId: User.IDValue }