|
- //
- // cntrlLocation.swift
- //
- //
- // Created by Michiel Carman on 4/1/24.
- //
-
- import Fluent
- import Vapor
-
- struct cntrlLocation: RouteCollection {
- func boot(routes: RoutesBuilder) throws {
- let rts = routes.grouped("locations")
- rts.get(use: index)
- rts.get(":x", use: read)
- rts.post(use: create)
- rts.group(":x") { rt in
- rts.delete(use: delete)
- }
- ///Add custom routes here
- }
-
- func index(req: Request) async throws -> [mdlLocation] {
- try await mdlLocation.ObjQuery(req: req).all()
- }
-
- func read(req:Request) async throws -> mdlLocation{
- return try await mdlLocation.ObjQuery(req: req).first()!
- }
-
- func create(req: Request) async throws -> mdlLocation {
- let obj = try req.content.decode(mdlLocation.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 mdlLocation.find(req.parameters.get("id"), on: req.db) else {
- throw Abort(.notFound)
- }
- try await obj.delete(on: req.db)
- return .noContent
- }
- }
|