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