153 lines
4.8 KiB
TypeScript
153 lines
4.8 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 { CreateBloodGroup, BloodGroup } from "../entities/BloodGroup";
|
|
import { Not } from "typeorm";
|
|
@Route("api/v1/org/metadata/bloodGroup")
|
|
@Tags("BloodGroup")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class BloodGroupController extends Controller {
|
|
private bloodGroupRepository = AppDataSource.getRepository(BloodGroup);
|
|
|
|
/**
|
|
* API สร้างกลุ่มเลือด
|
|
*
|
|
* @summary ORG_062 - สร้างกลุ่มเลือด (ADMIN) #66
|
|
*
|
|
*/
|
|
@Post()
|
|
async createBloodGroup(
|
|
@Body()
|
|
requestBody: CreateBloodGroup,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const checkName = await this.bloodGroupRepository.findOne({
|
|
where: { name: requestBody.name },
|
|
});
|
|
|
|
if (checkName) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
|
|
const bloodGroup = Object.assign(new BloodGroup(), requestBody);
|
|
bloodGroup.createdUserId = request.user.sub;
|
|
bloodGroup.createdFullName = request.user.name;
|
|
bloodGroup.lastUpdateUserId = request.user.sub;
|
|
bloodGroup.lastUpdateFullName = request.user.name;
|
|
await this.bloodGroupRepository.save(bloodGroup);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขกลุ่มเลือด
|
|
*
|
|
* @summary ORG_062 - แก้ไขกลุ่มเลือด (ADMIN) #66
|
|
*
|
|
* @param {string} id Id กลุ่มเลือด
|
|
*/
|
|
@Put("{id}")
|
|
async updateBloodGroup(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: CreateBloodGroup,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const bloodGroup = await this.bloodGroupRepository.findOne({ where: { id: id } });
|
|
if (!bloodGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกรุ๊ปเลือดนี้");
|
|
}
|
|
|
|
const checkName = await this.bloodGroupRepository.findOne({
|
|
where: { id: Not(id), name: requestBody.name },
|
|
});
|
|
|
|
if (checkName) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
|
|
bloodGroup.lastUpdateUserId = request.user.sub;
|
|
bloodGroup.lastUpdateFullName = request.user.name;
|
|
this.bloodGroupRepository.merge(bloodGroup, requestBody);
|
|
await this.bloodGroupRepository.save(bloodGroup);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API ลบกลุ่มเลือด
|
|
*
|
|
* @summary ORG_062 - ลบกลุ่มเลือด (ADMIN) #66
|
|
*
|
|
* @param {string} id Id กลุ่มเลือด
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteBloodGroup(@Path() id: string) {
|
|
const delBloodGroup = await this.bloodGroupRepository.findOne({
|
|
where: { id },
|
|
});
|
|
if (!delBloodGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกรุ๊ปเลือดนี้");
|
|
}
|
|
|
|
await this.bloodGroupRepository.delete({ id: id });
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการกลุ่มเลือด
|
|
*
|
|
* @summary ORG_062 - รายละเอียดรายการกลุ่มเลือด (ADMIN) #66
|
|
*
|
|
* @param {string} id Id กลุ่มเลือด
|
|
*/
|
|
@Get("{id}")
|
|
async detailBloodGroup(@Path() id: string) {
|
|
const bloodGroup = await this.bloodGroupRepository.findOne({
|
|
where: { id },
|
|
select: ["id", "name"],
|
|
});
|
|
if (!bloodGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกรุ๊ปเลือดนี้");
|
|
}
|
|
return new HttpSuccess(bloodGroup);
|
|
}
|
|
|
|
/**
|
|
* API รายการกลุ่มเลือด
|
|
*
|
|
* @summary ORG_062 - รายการกลุ่มเลือด (ADMIN) #66
|
|
*
|
|
*/
|
|
@Get()
|
|
async listBloodGroup() {
|
|
const bloodGroup = await this.bloodGroupRepository.find({
|
|
select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"],
|
|
order: { name: "ASC" },
|
|
});
|
|
|
|
// if (!bloodGroup) {
|
|
// return new HttpSuccess([]);
|
|
// }
|
|
return new HttpSuccess(bloodGroup);
|
|
}
|
|
}
|