PropertiesController.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import Property, { PropertyType } 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. name: schema.string(),
  16. value: schema.string(),
  17. remark: schema.string(),
  18. type: schema.enum(Object.values(PropertyType))
  19. })
  20. })
  21. return await Property.create(data)
  22. }
  23. public async show({ params, bouncer }: HttpContextContract) {
  24. // await bouncer.authorize('admin')
  25. return await Property.findOrFail(params.id)
  26. }
  27. public async findByName({ request }) {
  28. console.log(request)
  29. const data = await request.validate({
  30. schema: schema.create({
  31. name: schema.string()
  32. })
  33. })
  34. console.log(data)
  35. return await Property.findBy('name', data.name)
  36. }
  37. public async update({ request, params, bouncer }: HttpContextContract) {
  38. await bouncer.authorize('admin')
  39. const data = await request.validate({
  40. schema: schema.create({
  41. name: schema.string.optional([]),
  42. remark: schema.string.optional(),
  43. value: schema.string.optional(),
  44. type: schema.enum(Object.values(PropertyType))
  45. })
  46. })
  47. const property = await Property.findOrFail(params.id)
  48. property.merge(data)
  49. await property.save()
  50. return property
  51. }
  52. public async destroy({ params, bouncer }: HttpContextContract) {
  53. await bouncer.authorize('admin')
  54. // const property = await Property.findOrFail(params.id)
  55. // await property.delete()
  56. throw new Exception('Not implemented', 501)
  57. }
  58. }