feat: Initial commit
This commit is contained in:
49
Sources/App/Controllers/ApiController.swift
Normal file
49
Sources/App/Controllers/ApiController.swift
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user