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; + } + } +}