You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cntrlCategory.swift 1.1 KiB

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