CollectionsController.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import Collection from 'App/Models/Collection'
  3. import PaginationService from 'App/Services/PaginationService'
  4. import { schema } from '@ioc:Adonis/Core/Validator'
  5. import Episode from 'App/Models/Episode'
  6. import Series from 'App/Models/Series'
  7. import Drive from '@ioc:Adonis/Core/Drive'
  8. export default class CollectionsController {
  9. private paginationService = new PaginationService(Collection)
  10. public async index({ request, auth }: HttpContextContract) {
  11. const page = await this.paginationService.paginate({
  12. ...request.all(),
  13. userId: auth.user!.id
  14. })
  15. const seriesIds = new Set(
  16. page
  17. .all()
  18. .map((record) => record.seriesId)
  19. .filter((id) => !!id)
  20. )
  21. const series = await Series.findMany(Array.from(seriesIds))
  22. await Promise.all(
  23. series.map(async (s) => {
  24. try {
  25. if (s.cover) {
  26. const url = new URL(s.cover)
  27. const filePath = url.pathname.replace(/^\//, '')
  28. s.cover = await Drive.getSignedUrl(filePath)
  29. }
  30. } catch (e) {}
  31. try {
  32. if (s.landscapeCover) {
  33. const url = new URL(s.landscapeCover)
  34. const filePath = url.pathname.replace(/^\//, '')
  35. s.landscapeCover = await Drive.getSignedUrl(filePath)
  36. }
  37. } catch (e) {}
  38. })
  39. )
  40. const episodeIds = new Set(
  41. page
  42. .all()
  43. .map((record) => record.curEpId)
  44. .filter((id) => !!id)
  45. )
  46. const episodes = await Episode.findMany(Array.from(episodeIds))
  47. page.all().forEach((record) => {
  48. if (record.seriesId) {
  49. record.series = series.find((s) => s.id === record.seriesId)
  50. }
  51. if (record.curEpId) {
  52. record.episode = episodes.find((e) => e.id === record.curEpId)
  53. }
  54. })
  55. return page
  56. }
  57. public async store({ request, auth }: HttpContextContract) {
  58. const data = await request.validate({
  59. schema: schema.create({
  60. seriesId: schema.number(),
  61. curEpId: schema.number()
  62. })
  63. })
  64. let collection = await Collection.query()
  65. .where('user_id', auth.user!.id)
  66. .where('series_id', data.seriesId)
  67. .first()
  68. const ep = await Episode.findOrFail(data.curEpId)
  69. if (collection) {
  70. collection.curEpId = data.curEpId
  71. collection.curEpNum = ep.episodeNum
  72. await collection.save()
  73. } else {
  74. collection = await Collection.create({
  75. userId: auth.user!.id,
  76. seriesId: data.seriesId,
  77. curEpId: data.curEpId,
  78. curEpNum: ep.episodeNum
  79. })
  80. }
  81. return collection
  82. }
  83. public async show({ params, auth }: HttpContextContract) {
  84. return await Collection.query()
  85. .where('userId', auth.user!.id)
  86. .where('seriesId', params.id)
  87. .firstOrFail()
  88. }
  89. public async update({ request, params, bouncer }: HttpContextContract) {
  90. const data = await request.validate({
  91. schema: schema.create({
  92. userId: schema.number.optional(),
  93. seriesId: schema.number.optional(),
  94. curEpId: schema.number.optional()
  95. })
  96. })
  97. const collection = await Collection.findOrFail(params.id)
  98. await bouncer.authorize('owner', collection)
  99. collection.merge(data)
  100. return await collection.save()
  101. }
  102. public async destroy({ params, auth }: HttpContextContract) {
  103. await Collection.query()
  104. .where('userId', auth.user!.id)
  105. .where('seriesId', params.id)
  106. .delete()
  107. }
  108. }