EpisodesController.ts 3.7 KB

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