feat: check if profile exists

This commit is contained in:
Methapon2001 2024-03-12 17:17:16 +07:00
parent 96b53a4ca5
commit 6e7086c085
4 changed files with 48 additions and 0 deletions

View file

@ -23,11 +23,13 @@ import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
@Route("api/v1/org/profile/certificate") @Route("api/v1/org/profile/certificate")
@Tags("ProfileCertificate") @Tags("ProfileCertificate")
@Security("bearerAuth") @Security("bearerAuth")
export class ProfileCertificateController extends Controller { export class ProfileCertificateController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private certificateRepo = AppDataSource.getRepository(ProfileCertificate); private certificateRepo = AppDataSource.getRepository(ProfileCertificate);
private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory); private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory);
@ -110,6 +112,16 @@ export class ProfileCertificateController extends Controller {
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Body() body: CreateProfileCertificate, @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 data = new ProfileCertificate();
const history = new ProfileCertificateHistory(); const history = new ProfileCertificateHistory();

View file

@ -19,11 +19,13 @@ import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { ProfileHonorHistory } from "../entities/ProfileHonorHistory"; import { ProfileHonorHistory } from "../entities/ProfileHonorHistory";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
@Route("api/v1/org/profile/honor") @Route("api/v1/org/profile/honor")
@Tags("ProfileHonor") @Tags("ProfileHonor")
@Security("bearerAuth") @Security("bearerAuth")
export class ProfileHonorController extends Controller { export class ProfileHonorController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private honorRepo = AppDataSource.getRepository(ProfileHonor); private honorRepo = AppDataSource.getRepository(ProfileHonor);
private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory); private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory);
@ -104,6 +106,16 @@ export class ProfileHonorController extends Controller {
@Post() @Post()
public async newHonor(@Request() req: RequestWithUser, @Body() body: CreateProfileHonor) { 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 data = new ProfileHonor();
const history = new ProfileHonorHistory(); const history = new ProfileHonorHistory();

View file

@ -23,11 +23,13 @@ import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { ProfileInsigniaHistory } from "../entities/ProfileInsigniaHistory"; import { ProfileInsigniaHistory } from "../entities/ProfileInsigniaHistory";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
@Route("api/v1/org/profile/insignia") @Route("api/v1/org/profile/insignia")
@Tags("ProfileInsignia") @Tags("ProfileInsignia")
@Security("bearerAuth") @Security("bearerAuth")
export class ProfileInsigniaController extends Controller { export class ProfileInsigniaController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private insigniaRepo = AppDataSource.getRepository(ProfileInsignia); private insigniaRepo = AppDataSource.getRepository(ProfileInsignia);
private insigniaHistoryRepo = AppDataSource.getRepository(ProfileInsigniaHistory); private insigniaHistoryRepo = AppDataSource.getRepository(ProfileInsigniaHistory);
@ -131,6 +133,16 @@ export class ProfileInsigniaController extends Controller {
@Post() @Post()
public async newInsignia(@Request() req: RequestWithUser, @Body() body: CreateProfileInsignia) { 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 data = new ProfileInsignia();
const history = new ProfileInsigniaHistory(); const history = new ProfileInsigniaHistory();

View file

@ -23,11 +23,13 @@ import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
@Route("api/v1/org/profile/training") @Route("api/v1/org/profile/training")
@Tags("ProfileTraining") @Tags("ProfileTraining")
@Security("bearerAuth") @Security("bearerAuth")
export class ProfileTrainingController extends Controller { export class ProfileTrainingController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private trainingRepo = AppDataSource.getRepository(ProfileTraining); private trainingRepo = AppDataSource.getRepository(ProfileTraining);
private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory); private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory);
@ -123,6 +125,16 @@ export class ProfileTrainingController extends Controller {
@Post() @Post()
public async newTraining(@Request() req: RequestWithUser, @Body() body: CreateProfileTraining) { 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 data = new ProfileTraining();
const history = new ProfileTrainingHistory(); const history = new ProfileTrainingHistory();