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) {