EpisodesController.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import Episode from 'App/Models/Episode'
  3. import PaginationService from 'App/Services/PaginationService'
  4. import { schema } from '@ioc:Adonis/Core/Validator'
  5. import Decimal from 'decimal.js'
  6. import Order from 'App/Models/Order'
  7. import Drive from '@ioc:Adonis/Core/Drive'
  8. export default class EpisodesController {
  9. private paginationService = new PaginationService(Episode)
  10. public async index({ request }: HttpContextContract) {
  11. return await this.paginationService.paginate(request.all())
  12. }
  13. public async store({ request, bouncer }: HttpContextContract) {
  14. await bouncer.authorize('admin')
  15. const data: any = await request.validate({
  16. schema: schema.create({
  17. seriesId: schema.number(),
  18. title: schema.string.optional(),
  19. episodeNum: schema.number(),
  20. description: schema.string.optional(),
  21. cover: schema.string.optional(),
  22. video: schema.string.optional(),
  23. playCount: schema.number.optional(),
  24. price: schema.string.optional()
  25. })
  26. })
  27. if (data.price) {
  28. data.price = new Decimal(data.price)
  29. }
  30. return await Episode.create(data)
  31. }
  32. public async show({ params, auth }: HttpContextContract) {
  33. const episode = await Episode.findOrFail(params.id)
  34. if (episode.price.eq(0)) {
  35. episode.purchased = true
  36. } else if (auth.user) {
  37. let order = await Order.query()
  38. .where('userId', auth.user.id)
  39. .where('type', 'series')
  40. .where('seriesId', episode.seriesId)
  41. .first()
  42. if (!order) {
  43. order = await Order.query()
  44. .where('userId', auth.user.id)
  45. .where('type', 'episode')
  46. .where('episodeId', episode.id)
  47. .first()
  48. }
  49. if (order) {
  50. episode.purchased = true
  51. }
  52. }
  53. if (episode.purchased) {
  54. episode.signedUrl = await Drive.getSignedUrl(episode.video!, {
  55. expiresIn: '1h'
  56. })
  57. }
  58. return episode
  59. }
  60. public async update({ request, params, bouncer }: HttpContextContract) {
  61. await bouncer.authorize('admin')
  62. const data: any = await request.validate({
  63. schema: schema.create({
  64. seriesId: schema.number.optional(),
  65. title: schema.string.optional(),
  66. episodeNum: schema.number.optional(),
  67. description: schema.string.optional(),
  68. cover: schema.string.optional(),
  69. video: schema.string.optional(),
  70. playCount: schema.number.optional(),
  71. price: schema.number.optional()
  72. })
  73. })
  74. if (data.price) {
  75. data.price = new Decimal(data.price)
  76. }
  77. const episode = await Episode.findOrFail(params.id)
  78. episode.merge(data)
  79. return await episode.save()
  80. }
  81. public async destroy({ params, bouncer }: HttpContextContract) {
  82. await bouncer.authorize('admin')
  83. const episode = await Episode.findOrFail(params.id)
  84. await episode.delete()
  85. }
  86. }