|
| 1 | +import { Prisma } from '@prisma/client'; |
| 2 | +import { type NextFunction, type Request, type Response } from 'express'; |
| 3 | +import { StatusCodes } from 'http-status-codes'; |
| 4 | + |
| 5 | +import { ErrorResponse } from '../../../../common/response'; |
| 6 | +import { AirplaneService } from './airplane.service'; |
| 7 | + |
| 8 | +interface RequestWithUser extends Request { |
| 9 | + user?: { |
| 10 | + user_id?: string; |
| 11 | + id?: string; |
| 12 | + sub?: string; |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +interface CreateGameBody { |
| 17 | + title: string; |
| 18 | + description: string; |
| 19 | + game_data?: string | object; |
| 20 | +} |
| 21 | + |
| 22 | +interface UpdateGameBody { |
| 23 | + title?: string; |
| 24 | + description?: string; |
| 25 | + game_data?: string | object; |
| 26 | + is_published?: string | boolean; |
| 27 | + is_publish?: string | boolean; |
| 28 | +} |
| 29 | + |
| 30 | +export class AirplaneController { |
| 31 | + static async create( |
| 32 | + request: Request, |
| 33 | + response: Response, |
| 34 | + next: NextFunction, |
| 35 | + ) { |
| 36 | + try { |
| 37 | + const { user } = request as RequestWithUser; |
| 38 | + const creatorId = user?.user_id || user?.id || user?.sub; |
| 39 | + |
| 40 | + if (!creatorId) { |
| 41 | + throw new ErrorResponse( |
| 42 | + StatusCodes.UNAUTHORIZED, |
| 43 | + 'Unauthorized: No User ID', |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + const thumbnailFile = request.file; |
| 48 | + |
| 49 | + if (!thumbnailFile) { |
| 50 | + throw new ErrorResponse( |
| 51 | + StatusCodes.BAD_REQUEST, |
| 52 | + 'Thumbnail image is required', |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + const body = request.body as CreateGameBody; |
| 57 | + const { title, description } = body; |
| 58 | + |
| 59 | + let gameDataPayload: Prisma.JsonObject = {}; |
| 60 | + |
| 61 | + try { |
| 62 | + const rawGameData = body.game_data; |
| 63 | + |
| 64 | + if (typeof rawGameData === 'string') { |
| 65 | + gameDataPayload = JSON.parse(rawGameData) as Prisma.JsonObject; |
| 66 | + } else if (typeof rawGameData === 'object') { |
| 67 | + gameDataPayload = rawGameData as Prisma.JsonObject; |
| 68 | + } |
| 69 | + } catch { |
| 70 | + gameDataPayload = {}; |
| 71 | + } |
| 72 | + |
| 73 | + const result = await AirplaneService.create( |
| 74 | + { |
| 75 | + title, |
| 76 | + description, |
| 77 | + game_data: gameDataPayload, |
| 78 | + thumbnail_image: thumbnailFile, |
| 79 | + }, |
| 80 | + creatorId, |
| 81 | + ); |
| 82 | + |
| 83 | + response.status(StatusCodes.CREATED).json({ |
| 84 | + success: true, |
| 85 | + message: 'Airplane game created successfully', |
| 86 | + data: result, |
| 87 | + }); |
| 88 | + } catch (error) { |
| 89 | + if ( |
| 90 | + error instanceof Prisma.PrismaClientKnownRequestError && |
| 91 | + error.code === 'P2002' |
| 92 | + ) { |
| 93 | + return response.status(StatusCodes.CONFLICT).json({ |
| 94 | + status: false, |
| 95 | + message: |
| 96 | + 'Game with this title already exists. Please choose another title.', |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + next(error); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + static async findAll( |
| 105 | + request: Request, |
| 106 | + response: Response, |
| 107 | + next: NextFunction, |
| 108 | + ) { |
| 109 | + try { |
| 110 | + const result = await AirplaneService.findAll(request); |
| 111 | + response.status(StatusCodes.OK).json(result); |
| 112 | + } catch (error) { |
| 113 | + next(error); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + static async findOne( |
| 118 | + request: Request, |
| 119 | + response: Response, |
| 120 | + next: NextFunction, |
| 121 | + ) { |
| 122 | + try { |
| 123 | + const result = await AirplaneService.findOne(request.params.id); |
| 124 | + response.status(StatusCodes.OK).json({ success: true, data: result }); |
| 125 | + } catch (error) { |
| 126 | + next(error); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + static async update( |
| 131 | + request: Request, |
| 132 | + response: Response, |
| 133 | + next: NextFunction, |
| 134 | + ) { |
| 135 | + try { |
| 136 | + const { user } = request as RequestWithUser; |
| 137 | + const creatorId = user?.user_id || user?.id; |
| 138 | + |
| 139 | + const thumbnailFile = request.file; |
| 140 | + |
| 141 | + let gameDataPayload: Prisma.JsonObject | undefined; |
| 142 | + const body = request.body as UpdateGameBody; |
| 143 | + |
| 144 | + if (body.game_data) { |
| 145 | + try { |
| 146 | + gameDataPayload = |
| 147 | + typeof body.game_data === 'string' |
| 148 | + ? (JSON.parse(body.game_data) as Prisma.JsonObject) |
| 149 | + : (body.game_data as Prisma.JsonObject); |
| 150 | + } catch { |
| 151 | + // Ignore |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + let isPublished: boolean | undefined; |
| 156 | + |
| 157 | + if (body.is_published !== undefined) { |
| 158 | + isPublished = String(body.is_published) === 'true'; |
| 159 | + } else if (body.is_publish !== undefined) { |
| 160 | + isPublished = String(body.is_publish) === 'true'; |
| 161 | + } |
| 162 | + |
| 163 | + const payload = { |
| 164 | + title: body.title, |
| 165 | + description: body.description, |
| 166 | + game_data: gameDataPayload, |
| 167 | + is_published: isPublished, |
| 168 | + }; |
| 169 | + |
| 170 | + const result = await AirplaneService.update( |
| 171 | + request.params.id, |
| 172 | + payload, |
| 173 | + creatorId as string, |
| 174 | + thumbnailFile, |
| 175 | + ); |
| 176 | + response.status(StatusCodes.OK).json({ |
| 177 | + success: true, |
| 178 | + message: 'Game updated successfully', |
| 179 | + data: result, |
| 180 | + }); |
| 181 | + } catch (error) { |
| 182 | + next(error); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + static async delete( |
| 187 | + request: Request, |
| 188 | + response: Response, |
| 189 | + next: NextFunction, |
| 190 | + ) { |
| 191 | + try { |
| 192 | + const { user } = request as RequestWithUser; |
| 193 | + const creatorId = user?.user_id || user?.id; |
| 194 | + |
| 195 | + await AirplaneService.delete(request.params.id, creatorId as string); |
| 196 | + response |
| 197 | + .status(StatusCodes.OK) |
| 198 | + .json({ success: true, message: 'Game deleted successfully' }); //delete |
| 199 | + } catch (error) { |
| 200 | + next(error); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + static async play(request: Request, response: Response, next: NextFunction) { |
| 205 | + try { |
| 206 | + await AirplaneService.play(request.params.id); |
| 207 | + response |
| 208 | + .status(StatusCodes.OK) |
| 209 | + .json({ success: true, message: 'Play count updated' }); |
| 210 | + } catch (error) { |
| 211 | + next(error); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments