From 2fa49ddfcc73bb77900521d2af6c6a0c277dadaf Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 2 Feb 2024 16:03:22 +0700 Subject: [PATCH] =?UTF-8?q?validate=20=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD?= =?UTF-8?q?=E0=B8=8B=E0=B9=89=E0=B8=B3=20=E0=B9=83=E0=B8=99=20DB=20?= =?UTF-8?q?=E0=B9=81=E0=B8=A5=E0=B8=B0=20CRUD=20Relationship?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PosExecutiveController.ts | 21 +- src/controllers/RelationshipController.ts | 303 ++++++++++++---------- 2 files changed, 184 insertions(+), 140 deletions(-) diff --git a/src/controllers/PosExecutiveController.ts b/src/controllers/PosExecutiveController.ts index c8d7c158..b4d7539c 100644 --- a/src/controllers/PosExecutiveController.ts +++ b/src/controllers/PosExecutiveController.ts @@ -44,11 +44,17 @@ export class PosExecutiveController extends Controller { requestBody: CreatePosExecutive, @Request() request: { user: Record }, ) { - const posExecutive = Object.assign(new PosExecutive(), requestBody); - if (!posExecutive) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + + const checkName = await this.posExecutiveRepository.findOne({ + where: { posExecutiveName: requestBody.posExecutiveName }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } + try { + const posExecutive = Object.assign(new PosExecutive(), requestBody); posExecutive.createdUserId = request.user.sub; posExecutive.createdFullName = request.user.name; posExecutive.lastUpdateUserId = request.user.sub; @@ -78,6 +84,15 @@ export class PosExecutiveController extends Controller { if (!posExecutive) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } + + const checkName = await this.posExecutiveRepository.findOne({ + where: { posExecutiveName: requestBody.posExecutiveName }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + try { posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateFullName = request.user.name; diff --git a/src/controllers/RelationshipController.ts b/src/controllers/RelationshipController.ts index 13b7a96d..d82aecc9 100644 --- a/src/controllers/RelationshipController.ts +++ b/src/controllers/RelationshipController.ts @@ -1,141 +1,170 @@ import { - Controller, - Post, - Put, - Delete, - Route, - Security, - Tags, - Body, - Path, - Request, - SuccessResponse, - Response, - Get, - } from "tsoa"; - import { AppDataSource } from "../database/data-source"; - import HttpSuccess from "../interfaces/http-success"; - import HttpStatusCode from "../interfaces/http-status"; - import HttpError from "../interfaces/http-error"; - import { CreateRelationship, Relationship } from "../entities/Relationship"; - @Route("api/v1/org/relationship") - @Tags("Relationship") - @Security("bearerAuth") - @Response( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", - ) - @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") - export class RelationshipController extends Controller { - private relationshipRepository = AppDataSource.getRepository(Relationship); - - /** - * API สร้างสถานภาพ - * - * @summary ORG_060 - สร้างสถานภาพ (ADMIN) #65 - * - */ - @Post("relationship") - async createRelationship( - @Body() - requestBody: CreateRelationship, - @Request() request: { user: Record }, - ) { + Controller, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + SuccessResponse, + Response, + Get, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatusCode from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { CreateRelationship, Relationship } from "../entities/Relationship"; +@Route("api/v1/org/relationship") +@Tags("Relationship") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class RelationshipController extends Controller { + private relationshipRepository = AppDataSource.getRepository(Relationship); + + /** + * API สร้างสถานภาพ + * + * @summary ORG_060 - สร้างสถานภาพ (ADMIN) #65 + * + */ + @Post() + async createRelationship( + @Body() + requestBody: CreateRelationship, + @Request() request: { user: Record }, + ) { + const checkName = await this.relationshipRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { const relationship = Object.assign(new Relationship(), requestBody); - if (!relationship) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } - try { - relationship.createdUserId = request.user.sub; - relationship.createdFullName = request.user.name; - relationship.lastUpdateUserId = request.user.sub; - relationship.lastUpdateFullName = request.user.name; - await this.relationshipRepository.save(relationship); - return new HttpSuccess(); - } catch (error) { - return error; - } + relationship.createdUserId = request.user.sub; + relationship.createdFullName = request.user.name; + relationship.lastUpdateUserId = request.user.sub; + relationship.lastUpdateFullName = request.user.name; + await this.relationshipRepository.save(relationship); + return new HttpSuccess(); + } catch (error) { + return error; } - - /** - * API แก้ไขสถานภาพ - * - * @summary ORG_060 - แก้ไขสถานภาพ (ADMIN) #65 - * - * @param {string} id Id ตำแหน่งทางการบริหาร - */ - @Put("relationship/{id}") - async updateRelationship( - @Path() id: string, - @Body() - requestBody: CreateRelationship, - @Request() request: { user: Record }, - ) { - const relationship = await this.relationshipRepository.findOne({where: {id: id}}); - if (!relationship) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); - } - try { - relationship.lastUpdateUserId = request.user.sub; - relationship.lastUpdateFullName = request.user.name; - this.relationshipRepository.merge(relationship, requestBody); - await this.relationshipRepository.save(relationship); - return new HttpSuccess(); - } catch (error) { - return error; - } - } - - // /** - // * API ลบอัตรากำลัง - // * - // * @summary ORG_043 - ลบตำแหน่งทางการบริหาร (ADMIN) #46 - // * - // * @param {string} id Id ตำแหน่งทางการบริหาร - // */ - // @Delete("executive/{id}") - // async deletePosExecutive(@Path() id: string) { - // const delPosExecutive = await this.posExecutiveRepository.findOne({ - // where: { id }, - // }); - // if (!delPosExecutive) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); - // } - // try { - // await this.positionRepository.delete({ posExecutiveId: id }); - // await this.posExecutiveRepository.delete({ id }); - // return new HttpSuccess(); - // } catch (error) { - // return error; - // } - // } - - // /** - // * API รายละเอียดตำแหน่งทางการบริหาร - // * - // * @summary ORG_044 - รายละเอียดตำแหน่งทางการบริหาร (ADMIN) #47 - // * - // * @param {string} id Id ตำแหน่งทางการบริหาร - // */ - // @Get("executive/{id}") - // async detailPosExecutive(@Path() id: string) { - - // const posExecutive = await this.posExecutiveRepository.findOne({ - // where: { id }, - // select:[ - // "id", - // "posExecutiveName", - // "posExecutivePriority" - // ] - // }); - // if (!posExecutive) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - // } - // try { - // return new HttpSuccess(posExecutive); - // } catch (error) { - // return error; - // } - // } } - \ No newline at end of file + + /** + * API แก้ไขสถานภาพ + * + * @summary ORG_060 - แก้ไขสถานภาพ (ADMIN) #65 + * + * @param {string} id Id ตำแหน่งทางการบริหาร + */ + @Put("{id}") + async updateRelationship( + @Path() id: string, + @Body() + requestBody: CreateRelationship, + @Request() request: { user: Record }, + ) { + const relationship = await this.relationshipRepository.findOne({ where: { id: id } }); + if (!relationship) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); + } + + const checkName = await this.relationshipRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + relationship.lastUpdateUserId = request.user.sub; + relationship.lastUpdateFullName = request.user.name; + this.relationshipRepository.merge(relationship, requestBody); + await this.relationshipRepository.save(relationship); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบสถานภาพ + * + * @summary ORG_060 - ลบสถานภาพ (ADMIN) #65 + * + * @param {string} id Id ตำแหน่งทางการบริหาร + */ + @Delete("{id}") + async deleteRelationship(@Path() id: string) { + const delRelationship = await this.relationshipRepository.findOne({ + where: { id }, + }); + if (!delRelationship) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); + } + try { + await this.relationshipRepository.delete({ id: id }); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการสถานภาพ + * + * @summary ORG_060 - รายละเอียดรายการสถานภาพ (ADMIN) #65 + * + * @param {string} id Id ตำแหน่งทางการบริหาร + */ + @Get("{id}") + async detailRelationship(@Path() id: string) { + const relationship = await this.relationshipRepository.findOne({ + where: { id }, + select: ["id", "name"], + }); + if (!relationship) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(relationship); + } catch (error) { + return error; + } + } + + /** + * API รายการสถานภาพ + * + * @summary ORG_060 - รายการสถานภาพ (ADMIN) #65 + * + * @param {string} id Id ตำแหน่งทางการบริหาร + */ + @Get() + async listRelationship() { + const relationship = await this.relationshipRepository.find({ + select: ["id", "name"], + }); + if (!relationship) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(relationship); + } catch (error) { + return error; + } + } +}