From e80af15e9a8e0a72d9158494966054bca84cc073 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 2 Feb 2024 16:56:47 +0700 Subject: [PATCH 1/5] educationlevel --- src/controllers/EducationLevelController.ts | 171 ++++++++++++++++++++ src/controllers/RelationshipController.ts | 7 +- tsoa.json | 21 +++ 3 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 src/controllers/EducationLevelController.ts diff --git a/src/controllers/EducationLevelController.ts b/src/controllers/EducationLevelController.ts new file mode 100644 index 00000000..d7e9a9ea --- /dev/null +++ b/src/controllers/EducationLevelController.ts @@ -0,0 +1,171 @@ +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 { CreateEducationLevel, EducationLevel } from "../entities/EducationLevel"; +@Route("api/v1/org/educationLevel") +@Tags("EducationLevel") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class EducationLevelController extends Controller { + private educationLevelRepository = AppDataSource.getRepository(EducationLevel); + + /** + * API สร้างระดับการศึกษา + * + * @summary ORG_061 - สร้างระดับการศึกษา (ADMIN) #65 + * + */ + @Post() + async createEducationLevel( + @Body() + requestBody: CreateEducationLevel, + @Request() request: { user: Record }, + ) { + const checkName = await this.educationLevelRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + const educationLevel = Object.assign(new EducationLevel(), requestBody); + educationLevel.createdUserId = request.user.sub; + educationLevel.createdFullName = request.user.name; + educationLevel.lastUpdateUserId = request.user.sub; + educationLevel.lastUpdateFullName = request.user.name; + await this.educationLevelRepository.save(educationLevel); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขระดับการศึกษา + * + * @summary ORG_061 - แก้ไขระดับการศึกษา (ADMIN) #65 + * + * @param {string} id Id ระดับการศึกษา + */ + @Put("{id}") + async updateEducationLevel( + @Path() id: string, + @Body() + requestBody: CreateEducationLevel, + @Request() request: { user: Record }, + ) { + const educationLevel = await this.educationLevelRepository.findOne({ where: { id: id } }); + if (!educationLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); + } + + const checkName = await this.educationLevelRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + educationLevel.lastUpdateUserId = request.user.sub; + educationLevel.lastUpdateFullName = request.user.name; + this.educationLevelRepository.merge(educationLevel, requestBody); + await this.educationLevelRepository.save(educationLevel); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบระดับการศึกษา + * + * @summary ORG_061 - ลบระดับการศึกษา (ADMIN) #65 + * + * @param {string} id Id ระดับการศึกษา + */ + @Delete("{id}") + async deleteEducationLevel(@Path() id: string) { + const delEducationLevel = await this.educationLevelRepository.findOne({ + where: { id }, + }); + if (!delEducationLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); + } + try { + await this.educationLevelRepository.delete({ id: id }); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการระดับการศึกษา + * + * @summary ORG_061 - รายละเอียดรายการระดับการศึกษา (ADMIN) #65 + * + * @param {string} id Id ระดับการศึกษา + */ + @Get("{id}") + async detailEducationLevel(@Path() id: string) { + const educationLevel = await this.educationLevelRepository.findOne({ + where: { id }, + select: ["id", "name" ,"rank"], + }); + if (!educationLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(educationLevel); + } catch (error) { + return error; + } + } + + /** + * API รายการระดับการศึกษา + * + * @summary ORG_061 - รายการระดับการศึกษา (ADMIN) #65 + * + */ + @Get() + async listEducationLevel() { + console.log("11111111111111111111111111111"); + const educationLevel = await this.educationLevelRepository.find({ + select: ["id", "name" ], + }); + + if (!educationLevel) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(educationLevel); + } catch (error) { + return error; + } + } +} diff --git a/src/controllers/RelationshipController.ts b/src/controllers/RelationshipController.ts index d82aecc9..84696a8a 100644 --- a/src/controllers/RelationshipController.ts +++ b/src/controllers/RelationshipController.ts @@ -67,7 +67,7 @@ export class RelationshipController extends Controller { * * @summary ORG_060 - แก้ไขสถานภาพ (ADMIN) #65 * - * @param {string} id Id ตำแหน่งทางการบริหาร + * @param {string} id Id สถานภาพ */ @Put("{id}") async updateRelationship( @@ -105,7 +105,7 @@ export class RelationshipController extends Controller { * * @summary ORG_060 - ลบสถานภาพ (ADMIN) #65 * - * @param {string} id Id ตำแหน่งทางการบริหาร + * @param {string} id Id สถานภาพ */ @Delete("{id}") async deleteRelationship(@Path() id: string) { @@ -128,7 +128,7 @@ export class RelationshipController extends Controller { * * @summary ORG_060 - รายละเอียดรายการสถานภาพ (ADMIN) #65 * - * @param {string} id Id ตำแหน่งทางการบริหาร + * @param {string} id Id สถานภาพ */ @Get("{id}") async detailRelationship(@Path() id: string) { @@ -151,7 +151,6 @@ export class RelationshipController extends Controller { * * @summary ORG_060 - รายการสถานภาพ (ADMIN) #65 * - * @param {string} id Id ตำแหน่งทางการบริหาร */ @Get() async listRelationship() { diff --git a/tsoa.json b/tsoa.json index 1af11402..ab2e9101 100644 --- a/tsoa.json +++ b/tsoa.json @@ -55,6 +55,27 @@ }, { "name": "PosExecutive", "description": "ตำแหน่งทางการบริหาร" + }, + { + "name": "Prefix", "description": "คำนำหน้า" + }, + { + "name": "Rank", "description": "ยศ" + }, + { + "name": "BloodGroup", "description": "กลุ่มเลือด" + }, + { + "name": "Gender", "description": "เพศ" + }, + { + "name": "Religion", "description": "ศาสนา" + }, + { + "name": "Relationship", "description": "สถานภาพ" + }, + { + "name": "EducationLevel", "description": "ระดับการศึกษา" } ] }, From 065d0fd7e5f83f1c1482450d3544bf5f1964dd1a Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 2 Feb 2024 17:38:31 +0700 Subject: [PATCH 2/5] religion --- src/controllers/EducationLevelController.ts | 1 - src/controllers/ReligionController.ts | 170 ++++++++++++++++++++ 2 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 src/controllers/ReligionController.ts diff --git a/src/controllers/EducationLevelController.ts b/src/controllers/EducationLevelController.ts index d7e9a9ea..37c106f0 100644 --- a/src/controllers/EducationLevelController.ts +++ b/src/controllers/EducationLevelController.ts @@ -154,7 +154,6 @@ export class EducationLevelController extends Controller { */ @Get() async listEducationLevel() { - console.log("11111111111111111111111111111"); const educationLevel = await this.educationLevelRepository.find({ select: ["id", "name" ], }); diff --git a/src/controllers/ReligionController.ts b/src/controllers/ReligionController.ts new file mode 100644 index 00000000..ae9f904d --- /dev/null +++ b/src/controllers/ReligionController.ts @@ -0,0 +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 { CreateReligion, Religion } from "../entities/Religion"; +@Route("api/v1/org/religion") +@Tags("Religion") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class ReligionController extends Controller { + private religionRepository = AppDataSource.getRepository(Religion); + + /** + * API สร้างศาสนา + * + * @summary ORG_059 - สร้างศาสนา (ADMIN) #65 + * + */ + @Post() + async createReligion( + @Body() + requestBody: CreateReligion, + @Request() request: { user: Record }, + ) { + const checkName = await this.religionRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + const religion = Object.assign(new Religion(), requestBody); + religion.createdUserId = request.user.sub; + religion.createdFullName = request.user.name; + religion.lastUpdateUserId = request.user.sub; + religion.lastUpdateFullName = request.user.name; + await this.religionRepository.save(religion); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขศาสนา + * + * @summary ORG_059 - แก้ไขศาสนา (ADMIN) #65 + * + * @param {string} id Id ศาสนา + */ + @Put("{id}") + async updateReligion( + @Path() id: string, + @Body() + requestBody: CreateReligion, + @Request() request: { user: Record }, + ) { + const religion = await this.religionRepository.findOne({ where: { id: id } }); + if (!religion) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); + } + + const checkName = await this.religionRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + religion.lastUpdateUserId = request.user.sub; + religion.lastUpdateFullName = request.user.name; + this.religionRepository.merge(religion, requestBody); + await this.religionRepository.save(religion); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบศาสนา + * + * @summary ORG_059 - ลบศาสนา (ADMIN) #65 + * + * @param {string} id Id ศาสนา + */ + @Delete("{id}") + async deleteReligion(@Path() id: string) { + const delReligion = await this.religionRepository.findOne({ + where: { id }, + }); + if (!delReligion) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); + } + try { + await this.religionRepository.delete({ id: id }); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการศาสนา + * + * @summary ORG_059 - รายละเอียดรายการศาสนา (ADMIN) #65 + * + * @param {string} id Id ศาสนา + */ + @Get("{id}") + async detailReligion(@Path() id: string) { + const religion = await this.religionRepository.findOne({ + where: { id }, + select: ["id", "name"], + }); + if (!religion) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(religion); + } catch (error) { + return error; + } + } + + /** + * API รายการศาสนา + * + * @summary ORG_059 - รายการศาสนา (ADMIN) #65 + * + */ + @Get() + async listReligion() { + const religion = await this.religionRepository.find({ + select: ["id", "name" ], + }); + + if (!religion) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(religion); + } catch (error) { + return error; + } + } +} From 271848dec023bb7bf9eedfda39ab1c367360e61a Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 2 Feb 2024 17:59:33 +0700 Subject: [PATCH 3/5] =?UTF-8?q?CRUD=20=E0=B8=84=E0=B8=B3=E0=B8=99=E0=B8=B3?= =?UTF-8?q?=E0=B8=AB=E0=B8=99=E0=B9=89=E0=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PrefixController.ts | 173 ++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/controllers/PrefixController.ts diff --git a/src/controllers/PrefixController.ts b/src/controllers/PrefixController.ts new file mode 100644 index 00000000..8fd37aa4 --- /dev/null +++ b/src/controllers/PrefixController.ts @@ -0,0 +1,173 @@ +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 { Prefixe, CreatePrefixe, UpdatePrefixe } from "../entities/Prefixe"; +import { Not } from "typeorm"; + +@Route("api/v1/org/prefix") +@Tags("Prefix") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class PrefixController extends Controller { + private prefixRepository = AppDataSource.getRepository(Prefixe); + + /** + * API list รายการคำนำหน้า + * + * @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61 + * + */ + @Get() + async Get() { + const _prefix = await this.prefixRepository.find({ + select: ["id", "name"], + }); + if (!_prefix) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(_prefix); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการคำนำหน้า + * + * @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61 + * + * @param {string} id Id คำนำหน้า + */ + @Get("{id}") + async GetById(@Path() id: string) { + const _prefix = await this.prefixRepository.findOne({ + where: { id }, + select: ["id", "name"], + }); + if (!_prefix) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(_prefix); + } catch (error) { + return error; + } + } + + /** + * API สร้างรายการ body คำนำหน้า + * + * @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61 + * + */ + @Post() + async Post( + @Body() + requestBody: CreatePrefixe, + @Request() request: { user: Record }, + ) { + const _prefix = Object.assign(new Prefixe(), requestBody); + if (!_prefix) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const checkName = await this.prefixRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + _prefix.createdUserId = request.user.sub; + _prefix.createdFullName = request.user.name; + _prefix.lastUpdateUserId = request.user.sub; + _prefix.lastUpdateFullName = request.user.name; + await this.prefixRepository.save(_prefix); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขรายการ body คำนำหน้า + * + * @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61 + * + * @param {string} id Id คำนำหน้า + */ + @Put("{id}") + async Put( + @Path() id: string, + @Body() + requestBody: UpdatePrefixe, + @Request() request: { user: Record }, + ) { + const _prefix = await this.prefixRepository.findOne({ where: { id: id } }); + if (!_prefix) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + id); + } + const checkName = await this.prefixRepository.findOne({ + where: { id: Not(id), name: requestBody.name }, + }); + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + _prefix.lastUpdateUserId = request.user.sub; + _prefix.lastUpdateFullName = request.user.name; + this.prefixRepository.merge(_prefix, requestBody); + await this.prefixRepository.save(_prefix); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบรายการคำนำหน้า + * + * @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61 + * + * @param {string} id Id คำนำหน้า + */ + @Delete("{id}") + async Delete(@Path() id: string) { + const _delPrefix = await this.prefixRepository.findOne({ + where: { id: id }, + }); + if (!_delPrefix) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + id); + } + try { + await this.prefixRepository.delete(_delPrefix.id); + return new HttpSuccess(); + } catch (error) { + return error; + } + } +} From 93373bc00fc11f13ffa8aceebf29ab4d48fc1283 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 2 Feb 2024 18:01:26 +0700 Subject: [PATCH 4/5] rank --- src/controllers/EducationLevelController.ts | 3 +- src/controllers/RankController.ts | 172 ++++++++++++++++++++ src/controllers/RelationshipController.ts | 3 +- src/controllers/ReligionController.ts | 3 +- 4 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 src/controllers/RankController.ts diff --git a/src/controllers/EducationLevelController.ts b/src/controllers/EducationLevelController.ts index 37c106f0..b168f72c 100644 --- a/src/controllers/EducationLevelController.ts +++ b/src/controllers/EducationLevelController.ts @@ -18,6 +18,7 @@ import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateEducationLevel, EducationLevel } from "../entities/EducationLevel"; +import { Not } from "typeorm"; @Route("api/v1/org/educationLevel") @Tags("EducationLevel") @Security("bearerAuth") @@ -82,7 +83,7 @@ export class EducationLevelController extends Controller { } const checkName = await this.educationLevelRepository.findOne({ - where: { name: requestBody.name }, + where: { id:Not(id),name: requestBody.name }, }); if (checkName) { diff --git a/src/controllers/RankController.ts b/src/controllers/RankController.ts new file mode 100644 index 00000000..3cb690fb --- /dev/null +++ b/src/controllers/RankController.ts @@ -0,0 +1,172 @@ +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 { CreateRank, Rank } from "../entities/Rank"; + import { Not } from "typeorm"; + @Route("api/v1/org/rank") + @Tags("Rank") + @Security("bearerAuth") + @Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", + ) + @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") + export class RankController extends Controller { + private rankRepository = AppDataSource.getRepository(Rank); + + /** + * API สร้างยศ + * + * @summary ORG_059 - สร้างยศ (ADMIN) #65 + * + */ + @Post() + async createRank( + @Body() + requestBody: CreateRank, + @Request() request: { user: Record }, + ) { + const checkName = await this.rankRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + const rank = Object.assign(new Rank(), requestBody); + rank.createdUserId = request.user.sub; + rank.createdFullName = request.user.name; + rank.lastUpdateUserId = request.user.sub; + rank.lastUpdateFullName = request.user.name; + await this.rankRepository.save(rank); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขยศ + * + * @summary ORG_059 - แก้ไขยศ (ADMIN) #65 + * + * @param {string} id Id ยศ + */ + @Put("{id}") + async updateRank( + @Path() id: string, + @Body() + requestBody: CreateRank, + @Request() request: { user: Record }, + ) { + const rank = await this.rankRepository.findOne({ where: { id: id } }); + if (!rank) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); + } + + const checkName = await this.rankRepository.findOne({ + where: { id:Not(id),name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + rank.lastUpdateUserId = request.user.sub; + rank.lastUpdateFullName = request.user.name; + this.rankRepository.merge(rank, requestBody); + await this.rankRepository.save(rank); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบยศ + * + * @summary ORG_059 - ลบยศ (ADMIN) #65 + * + * @param {string} id Id ยศ + */ + @Delete("{id}") + async deleteRank(@Path() id: string) { + const delRank = await this.rankRepository.findOne({ + where: { id }, + }); + if (!delRank) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); + } + try { + await this.rankRepository.delete({ id: id }); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการยศ + * + * @summary ORG_059 - รายละเอียดรายการยศ (ADMIN) #65 + * + * @param {string} id Id ยศ + */ + @Get("{id}") + async detailRank(@Path() id: string) { + const rank = await this.rankRepository.findOne({ + where: { id }, + select: ["id", "name"], + }); + if (!rank) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(rank); + } catch (error) { + return error; + } + } + + /** + * API รายการยศ + * + * @summary ORG_059 - รายการยศ (ADMIN) #65 + * + */ + @Get() + async listRank() { + const rank = await this.rankRepository.find({ + select: ["id", "name" ], + }); + + if (!rank) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(rank); + } catch (error) { + return error; + } + } + } + \ No newline at end of file diff --git a/src/controllers/RelationshipController.ts b/src/controllers/RelationshipController.ts index 84696a8a..cf6ae0bf 100644 --- a/src/controllers/RelationshipController.ts +++ b/src/controllers/RelationshipController.ts @@ -18,6 +18,7 @@ import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateRelationship, Relationship } from "../entities/Relationship"; +import { Not } from "typeorm"; @Route("api/v1/org/relationship") @Tags("Relationship") @Security("bearerAuth") @@ -82,7 +83,7 @@ export class RelationshipController extends Controller { } const checkName = await this.relationshipRepository.findOne({ - where: { name: requestBody.name }, + where: {id:Not(id),name: requestBody.name }, }); if (checkName) { diff --git a/src/controllers/ReligionController.ts b/src/controllers/ReligionController.ts index ae9f904d..692e03e9 100644 --- a/src/controllers/ReligionController.ts +++ b/src/controllers/ReligionController.ts @@ -18,6 +18,7 @@ import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateReligion, Religion } from "../entities/Religion"; +import { Not } from "typeorm"; @Route("api/v1/org/religion") @Tags("Religion") @Security("bearerAuth") @@ -82,7 +83,7 @@ export class ReligionController extends Controller { } const checkName = await this.religionRepository.findOne({ - where: { name: requestBody.name }, + where: { id:Not(id),name: requestBody.name }, }); if (checkName) { From 05be18d9f10d15129c0b66afddfccc3fd0e7e4e5 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 2 Feb 2024 18:15:52 +0700 Subject: [PATCH 5/5] =?UTF-8?q?CRUD=20=E0=B9=80=E0=B8=9E=E0=B8=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/GenderController.ts | 173 ++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/controllers/GenderController.ts diff --git a/src/controllers/GenderController.ts b/src/controllers/GenderController.ts new file mode 100644 index 00000000..5134251c --- /dev/null +++ b/src/controllers/GenderController.ts @@ -0,0 +1,173 @@ +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 { Gender, CreateGender, UpdateGender } from "../entities/Gender"; +import { Not } from "typeorm"; + +@Route("api/v1/org/gender") +@Tags("Gender") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class GenderController extends Controller { + private genderRepository = AppDataSource.getRepository(Gender); + + /** + * API list รายการเพศ + * + * @summary ORG_058 - CRUD เพศ (ADMIN) #62 + * + */ + @Get() + async Get() { + const _gender = await this.genderRepository.find({ + select: ["id", "name"], + }); + if (!_gender) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(_gender); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการเพศ + * + * @summary ORG_058 - CRUD เพศ (ADMIN) #62 + * + * @param {string} id Id เพศ + */ + @Get("{id}") + async GetById(@Path() id: string) { + const _gender = await this.genderRepository.findOne({ + where: { id }, + select: ["id", "name"], + }); + if (!_gender) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(_gender); + } catch (error) { + return error; + } + } + + /** + * API สร้างรายการ body เพศ + * + * @summary ORG_058 - CRUD เพศ (ADMIN) #62 + * + */ + @Post() + async Post( + @Body() + requestBody: CreateGender, + @Request() request: { user: Record }, + ) { + const _gender = Object.assign(new Gender(), requestBody); + if (!_gender) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const checkName = await this.genderRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + _gender.createdUserId = request.user.sub; + _gender.createdFullName = request.user.name; + _gender.lastUpdateUserId = request.user.sub; + _gender.lastUpdateFullName = request.user.name; + await this.genderRepository.save(_gender); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขรายการ body เพศ + * + * @summary ORG_058 - CRUD เพศ (ADMIN) #62 + * + * @param {string} id Id เพศ + */ + @Put("{id}") + async Put( + @Path() id: string, + @Body() + requestBody: UpdateGender, + @Request() request: { user: Record }, + ) { + const _gender = await this.genderRepository.findOne({ where: { id: id } }); + if (!_gender) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + id); + } + const checkName = await this.genderRepository.findOne({ + where: { id: Not(id), name: requestBody.name }, + }); + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + _gender.lastUpdateUserId = request.user.sub; + _gender.lastUpdateFullName = request.user.name; + this.genderRepository.merge(_gender, requestBody); + await this.genderRepository.save(_gender); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบรายการเพศ + * + * @summary ORG_058 - CRUD เพศ (ADMIN) #62 + * + * @param {string} id Id เพศ + */ + @Delete("{id}") + async Delete(@Path() id: string) { + const _delGender = await this.genderRepository.findOne({ + where: { id: id }, + }); + if (!_delGender) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + id); + } + try { + await this.genderRepository.delete(_delGender.id); + return new HttpSuccess(); + } catch (error) { + return error; + } + } +}