PropertiesController.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import Property from 'App/Models/Property'
  3. import PaginationService from 'App/Services/PaginationService'
  4. import { schema, rules } from '@ioc:Adonis/Core/Validator'
  5. import { Exception } from '@adonisjs/core/build/standalone'
  6. export default class PropertiesController {
  7. private paginationService = new PaginationService(Property)
  8. public async index({ request }: HttpContextContract) {
  9. return await this.paginationService.paginate(request.all())
  10. }
  11. public async store({ request, bouncer }: HttpContextContract) {
  12. await bouncer.authorize('admin')
  13. const data = await request.validate({
  14. schema: schema.create({
  15. id: schema.string({ trim: true }, [
  16. rules.regex(/^[a-zA-Z0-9_]+$/),
  17. rules.maxLength(72)
  18. ]),
  19. name: schema.string(),
  20. value: schema.string(),
  21. type: schema.string()
  22. })
  23. })
  24. return await Property.create(data)
  25. }
  26. public async show({ params, bouncer }: HttpContextContract) {
  27. // await bouncer.authorize('admin')
  28. return await Property.findOrFail(params.id)
  29. }
  30. public async update({ request, params, bouncer }: HttpContextContract) {
  31. await bouncer.authorize('admin')
  32. const data = await request.validate({
  33. schema: schema.create({
  34. name: schema.string.optional([]),
  35. value: schema.string.optional(),
  36. type: schema.string.optional()
  37. })
  38. })
  39. const property = await Property.findOrFail(params.id)
  40. property.merge(data)
  41. await property.save()
  42. return property
  43. }
  44. public async destroy({ params, bouncer }: HttpContextContract) {
  45. await bouncer.authorize('admin')
  46. // const property = await Property.findOrFail(params.id)
  47. // await property.delete()
  48. throw new Exception('Not implemented', 501)
  49. }
  50. }