152 lines
4.7 KiB
TypeScript
152 lines
4.7 KiB
TypeScript
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 { SubDistrict, CreateSubDistrict, UpdateSubDistrict } from "../entities/SubDistrict";
|
|
import { Not } from "typeorm";
|
|
|
|
@Route("api/v1/org/metadata/subDistrict")
|
|
@Tags("SubDistrict")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class SubDistrictController extends Controller {
|
|
private subDistrictRepository = AppDataSource.getRepository(SubDistrict);
|
|
|
|
/**
|
|
* API list รายการเพศ
|
|
*
|
|
* @summary ORG_058 - CRUD เพศ (ADMIN) #62
|
|
*
|
|
*/
|
|
@Get()
|
|
async GetResult() {
|
|
const _subDistrict = await this.subDistrictRepository.find({
|
|
select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"],
|
|
order: { createdAt: "ASC" },
|
|
});
|
|
return new HttpSuccess(_subDistrict);
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการเพศ
|
|
*
|
|
* @summary ORG_058 - CRUD เพศ (ADMIN) #62
|
|
*
|
|
* @param {string} id Id เพศ
|
|
*/
|
|
@Get("{id}")
|
|
async GetById(@Path() id: string) {
|
|
const _subDistrict = await this.subDistrictRepository.findOne({
|
|
where: { id },
|
|
select: ["id", "name"],
|
|
});
|
|
if (!_subDistrict) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้");
|
|
}
|
|
|
|
return new HttpSuccess(_subDistrict);
|
|
}
|
|
|
|
/**
|
|
* API สร้างรายการ body เพศ
|
|
*
|
|
* @summary ORG_058 - CRUD เพศ (ADMIN) #62
|
|
*
|
|
*/
|
|
@Post()
|
|
async Post(
|
|
@Body()
|
|
requestBody: CreateSubDistrict,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const _subDistrict = Object.assign(new SubDistrict(), requestBody);
|
|
if (!_subDistrict) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้");
|
|
}
|
|
|
|
const checkName = await this.subDistrictRepository.findOne({
|
|
where: { name: requestBody.name },
|
|
});
|
|
|
|
if (checkName) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
|
|
_subDistrict.createdUserId = request.user.sub;
|
|
_subDistrict.createdFullName = request.user.name;
|
|
_subDistrict.lastUpdateUserId = request.user.sub;
|
|
_subDistrict.lastUpdateFullName = request.user.name;
|
|
await this.subDistrictRepository.save(_subDistrict);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรายการ body เพศ
|
|
*
|
|
* @summary ORG_058 - CRUD เพศ (ADMIN) #62
|
|
*
|
|
* @param {string} id Id เพศ
|
|
*/
|
|
@Put("{id}")
|
|
async Put(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: UpdateSubDistrict,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const _subDistrict = await this.subDistrictRepository.findOne({ where: { id: id } });
|
|
if (!_subDistrict) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้");
|
|
}
|
|
const checkName = await this.subDistrictRepository.findOne({
|
|
where: { id: Not(id), name: requestBody.name },
|
|
});
|
|
if (checkName) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
|
|
_subDistrict.lastUpdateUserId = request.user.sub;
|
|
_subDistrict.lastUpdateFullName = request.user.name;
|
|
this.subDistrictRepository.merge(_subDistrict, requestBody);
|
|
await this.subDistrictRepository.save(_subDistrict);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API ลบรายการเพศ
|
|
*
|
|
* @summary ORG_058 - CRUD เพศ (ADMIN) #62
|
|
*
|
|
* @param {string} id Id เพศ
|
|
*/
|
|
@Delete("{id}")
|
|
async Delete(@Path() id: string) {
|
|
const _delSubDistrict = await this.subDistrictRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!_delSubDistrict) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้");
|
|
}
|
|
await this.subDistrictRepository.delete(_delSubDistrict.id);
|
|
return new HttpSuccess();
|
|
}
|
|
}
|