PropertiesController.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 findByName({ request }) {
  31. console.log(request)
  32. const data = await request.validate({
  33. schema: schema.create({
  34. name: schema.string()
  35. })
  36. })
  37. console.log(data)
  38. return await Property.findBy('name', data.name)
  39. }
  40. public async update({ request, params, bouncer }: HttpContextContract) {
  41. await bouncer.authorize('admin')
  42. const data = await request.validate({
  43. schema: schema.create({
  44. name: schema.string.optional([]),
  45. value: schema.string.optional(),
  46. type: schema.string.optional()
  47. })
  48. })
  49. const property = await Property.findOrFail(params.id)
  50. property.merge(data)
  51. await property.save()
  52. return property
  53. }
  54. public async destroy({ params, bouncer }: HttpContextContract) {
  55. await bouncer.authorize('admin')
  56. // const property = await Property.findOrFail(params.id)
  57. // await property.delete()
  58. throw new Exception('Not implemented', 501)
  59. }
  60. }