From 6e7086c0857dfec98e24ed274a8ed346a18f01d3 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:17:16 +0700 Subject: [PATCH] feat: check if profile exists --- src/controllers/ProfileCertificateController.ts | 12 ++++++++++++ src/controllers/ProfileHonorController.ts | 12 ++++++++++++ src/controllers/ProfileInsigniaController.ts | 12 ++++++++++++ src/controllers/ProfileTrainingController.ts | 12 ++++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/controllers/ProfileCertificateController.ts b/src/controllers/ProfileCertificateController.ts index d859491c..18ed3f3e 100644 --- a/src/controllers/ProfileCertificateController.ts +++ b/src/controllers/ProfileCertificateController.ts @@ -23,11 +23,13 @@ import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; @Route("api/v1/org/profile/certificate") @Tags("ProfileCertificate") @Security("bearerAuth") export class ProfileCertificateController extends Controller { + private profileRepo = AppDataSource.getRepository(Profile); private certificateRepo = AppDataSource.getRepository(ProfileCertificate); private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory); @@ -110,6 +112,16 @@ export class ProfileCertificateController extends Controller { @Request() req: RequestWithUser, @Body() body: CreateProfileCertificate, ) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const data = new ProfileCertificate(); const history = new ProfileCertificateHistory(); diff --git a/src/controllers/ProfileHonorController.ts b/src/controllers/ProfileHonorController.ts index b0cf23a6..1cf305b2 100644 --- a/src/controllers/ProfileHonorController.ts +++ b/src/controllers/ProfileHonorController.ts @@ -19,11 +19,13 @@ import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileHonorHistory } from "../entities/ProfileHonorHistory"; import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; @Route("api/v1/org/profile/honor") @Tags("ProfileHonor") @Security("bearerAuth") export class ProfileHonorController extends Controller { + private profileRepo = AppDataSource.getRepository(Profile); private honorRepo = AppDataSource.getRepository(ProfileHonor); private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory); @@ -104,6 +106,16 @@ export class ProfileHonorController extends Controller { @Post() public async newHonor(@Request() req: RequestWithUser, @Body() body: CreateProfileHonor) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const data = new ProfileHonor(); const history = new ProfileHonorHistory(); diff --git a/src/controllers/ProfileInsigniaController.ts b/src/controllers/ProfileInsigniaController.ts index 5e659265..d05ef9f7 100644 --- a/src/controllers/ProfileInsigniaController.ts +++ b/src/controllers/ProfileInsigniaController.ts @@ -23,11 +23,13 @@ import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileInsigniaHistory } from "../entities/ProfileInsigniaHistory"; import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; @Route("api/v1/org/profile/insignia") @Tags("ProfileInsignia") @Security("bearerAuth") export class ProfileInsigniaController extends Controller { + private profileRepo = AppDataSource.getRepository(Profile); private insigniaRepo = AppDataSource.getRepository(ProfileInsignia); private insigniaHistoryRepo = AppDataSource.getRepository(ProfileInsigniaHistory); @@ -131,6 +133,16 @@ export class ProfileInsigniaController extends Controller { @Post() public async newInsignia(@Request() req: RequestWithUser, @Body() body: CreateProfileInsignia) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const data = new ProfileInsignia(); const history = new ProfileInsigniaHistory(); diff --git a/src/controllers/ProfileTrainingController.ts b/src/controllers/ProfileTrainingController.ts index a2c6f457..32a1a123 100644 --- a/src/controllers/ProfileTrainingController.ts +++ b/src/controllers/ProfileTrainingController.ts @@ -23,11 +23,13 @@ import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; @Route("api/v1/org/profile/training") @Tags("ProfileTraining") @Security("bearerAuth") export class ProfileTrainingController extends Controller { + private profileRepo = AppDataSource.getRepository(Profile); private trainingRepo = AppDataSource.getRepository(ProfileTraining); private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory); @@ -123,6 +125,16 @@ export class ProfileTrainingController extends Controller { @Post() public async newTraining(@Request() req: RequestWithUser, @Body() body: CreateProfileTraining) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const data = new ProfileTraining(); const history = new ProfileTrainingHistory();