From 065d0fd7e5f83f1c1482450d3544bf5f1964dd1a Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 2 Feb 2024 17:38:31 +0700 Subject: [PATCH] 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; + } + } +}