|
|
@@ -2,7 +2,7 @@ import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
|
import Order from 'App/Models/Order'
|
|
|
import PaginationService from 'App/Services/PaginationService'
|
|
|
import { schema } from '@ioc:Adonis/Core/Validator'
|
|
|
-import { BadRequestException, InternalServerException } from 'App/Exceptions/Common'
|
|
|
+import { BadRequestException } from 'App/Exceptions/Common'
|
|
|
import Series from 'App/Models/Series'
|
|
|
import Episode from 'App/Models/Episode'
|
|
|
import UserBalanceService from 'App/Services/UserBalanceService'
|
|
|
@@ -11,8 +11,34 @@ import { BalanceRecordType } from 'App/Models/BalanceRecord'
|
|
|
|
|
|
export default class OrdersController {
|
|
|
private paginationService = new PaginationService(Order)
|
|
|
- public async index({ request }: HttpContextContract) {
|
|
|
- return await this.paginationService.paginate(request.all())
|
|
|
+ public async index({ request, auth }: HttpContextContract) {
|
|
|
+ const page = await this.paginationService.paginate({
|
|
|
+ ...request.all(),
|
|
|
+ userId: auth.user!.id
|
|
|
+ })
|
|
|
+ const seriesIds = new Set(
|
|
|
+ page
|
|
|
+ .all()
|
|
|
+ .map((record) => record.seriesId)
|
|
|
+ .filter((id) => !!id)
|
|
|
+ )
|
|
|
+ const series = await Series.findMany(Array.from(seriesIds))
|
|
|
+ const episodeIds = new Set(
|
|
|
+ page
|
|
|
+ .all()
|
|
|
+ .map((record) => record.episodeId)
|
|
|
+ .filter((id) => !!id)
|
|
|
+ )
|
|
|
+ const episodes = await Episode.findMany(Array.from(episodeIds))
|
|
|
+ page.all().forEach((record) => {
|
|
|
+ if (record.seriesId) {
|
|
|
+ record.series = series.find((s) => s.id === record.seriesId)
|
|
|
+ }
|
|
|
+ if (record.episodeId) {
|
|
|
+ record.episode = episodes.find((e) => e.id === record.episodeId)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return page
|
|
|
}
|
|
|
|
|
|
public async store({ request, auth }: HttpContextContract) {
|
|
|
@@ -38,14 +64,14 @@ export default class OrdersController {
|
|
|
if (data.seriesId) {
|
|
|
const series = await Series.findOrFail(data.seriesId)
|
|
|
if (series.price.comparedTo(userBalance.balance) > 0) {
|
|
|
- throw new InternalServerException('not enough balance')
|
|
|
+ throw new Error('not enough balance')
|
|
|
}
|
|
|
data.price = series.price
|
|
|
data.type = BalanceRecordType.Purchase
|
|
|
} else {
|
|
|
const episode = await Episode.findOrFail(data.episodeId)
|
|
|
if (episode.price.comparedTo(userBalance.balance) > 0) {
|
|
|
- throw new InternalServerException('not enough balance')
|
|
|
+ throw new Error('not enough balance')
|
|
|
}
|
|
|
data.seriesId = episode.seriesId
|
|
|
data.price = episode.price
|