選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cntrlSpec.swift 1.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // cntrlSpec.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 4/1/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. struct cntrlSpec: RouteCollection {
  10. func boot(routes: RoutesBuilder) throws {
  11. let rts = routes.grouped("specs")
  12. rts.get(use: index)
  13. rts.get(":x", use: read)
  14. rts.post(use: create)
  15. rts.group(":x") { rt in
  16. rts.delete(use: delete)
  17. }
  18. ///Add custom routes here
  19. }
  20. func index(req: Request) async throws -> [mdlSpec] {
  21. try await mdlSpec.ObjQuery(req: req).all()
  22. }
  23. func read(req:Request) async throws -> mdlSpec{
  24. return try await mdlSpec.ObjQuery(req: req).first()!
  25. }
  26. func create(req: Request) async throws -> mdlSpec {
  27. let obj = try req.content.decode(mdlSpec.self)
  28. if(obj.id != nil){
  29. obj._$idExists = true
  30. }
  31. try await obj.save(on: req.db)
  32. return obj
  33. }
  34. func delete(req: Request) async throws -> HTTPStatus {
  35. guard let obj = try await mdlSpec.find(req.parameters.get("id"), on: req.db) else {
  36. throw Abort(.notFound)
  37. }
  38. try await obj.delete(on: req.db)
  39. return .noContent
  40. }
  41. }