feat: Initial commit

This commit is contained in:
2024-12-31 17:02:29 -05:00
parent bb568ba60e
commit 8dba393267
21 changed files with 1881 additions and 63 deletions

View File

@@ -0,0 +1,49 @@
import Fluent
import Foundation
import Vapor
struct ApiController: RouteCollection {
func boot(routes: any RoutesBuilder) throws {
let users = routes.grouped("api", "users")
users.get(use: usersIndex(req:))
users.post(use: createUser(req:))
let proCon = routes.grouped("api", "procons")
proCon.get(use: prosAndConsIndex(req:))
proCon.post(use: createProCon(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
}
}
struct ProConDTO: Content {
let id: UUID?
let type: ProCon.ProConType
let description: String
let userId: User.IDValue
}