|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- //
- // File.swift
- //
- //
- // Created by Michiel Carman on 4/1/24.
- //
-
- import Fluent
- import Vapor
- import Crypto
-
-
-
- struct cntrlUser: RouteCollection {
- func boot(routes: RoutesBuilder) throws {
- let rts = routes.grouped("users")
- rts.get(use: index)
- rts.get("login", ":x", use: login)
- rts.get(":x", use: read)
- rts.post(use: create)
- rts.group(":x") { todo in
- rts.delete(use: delete)
- }
- }
-
- func api(req: Request) async throws -> String{
- return Environment.get("API_KEY")!
- }
-
- func login(req: Request) async throws -> mdlUser{
- return try await mdlUser.ObjQuery(req: req).first()!
- }
-
- func index(req: Request) async throws -> [mdlUser] {
- //try await mdlUser.query(on: req.db).with(\.$roles).all()
- return try await mdlUser.ObjQuery(req: req).all()
- }
-
- func read(req:Request) async throws -> mdlUser{
- return try await mdlUser.ObjQuery(req: req).first()!
- }
-
- func create(req: Request) async throws -> mdlUser {
- let obj = try req.content.decode(mdlUser.self)
- if(obj.id != nil){
- obj._$idExists = true
- }
- try await obj.save(on: req.db)
- return obj
- }
-
-
- func delete(req: Request) async throws -> HTTPStatus {
- guard let obj = try await mdlUser.find(req.parameters.get("x"), on: req.db) else {
- throw Abort(.notFound)
- }
- try await obj.delete(on: req.db)
- return .noContent
- }
- }
|