api เครื่องราช

This commit is contained in:
Bright 2024-03-13 15:30:39 +07:00
parent 47c40f9b5f
commit 7f77a80cd5
3 changed files with 377 additions and 0 deletions

View file

@ -0,0 +1,200 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} 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 { Equal, ILike, In, IsNull, Like, Not, Brackets, Between } from "typeorm";
import { InsigniaType, CreateInsigniaType, UpdateInsigniaType } from "../entities/InsigniaType";
import { Insignia, CreateInsignias, UpdateInsignias } from "../entities/Insignia";
@Route("api/v1/org/insignia/Insignias")
@Tags("Insignia")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class InsigniaController extends Controller {
private insigniaTypeRepository = AppDataSource.getRepository(InsigniaType);
private insigniaRepository = AppDataSource.getRepository(Insignia);
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Post("")
async CreateInsignia(
@Body() requestBody: CreateInsignias,
@Request() request: { user: Record<string, any> },
) {
const insignia = Object.assign(new Insignia(), requestBody);
if (!insignia) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const insigniaType = await this.insigniaTypeRepository.findOne({
where: { id: requestBody.insigniaTypeId},
});
if (!insigniaType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับชั้นเครื่องราชอิสริยาภรณ์นี้");
}
const rowRepeated = await this.insigniaRepository.findOne({
where: {
name: requestBody.name,
isActive: requestBody.isActive,
},
});
if (rowRepeated) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
}
insignia.createdUserId = request.user.sub;
insignia.createdFullName = request.user.name;
insignia.lastUpdateUserId = request.user.sub;
insignia.lastUpdateFullName = request.user.name;
await this.insigniaRepository.save(insignia);
return new HttpSuccess(insignia.id);
}
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Put("{id}")
async UpdateInsignia(
@Path() id: string,
@Body() requestBody: UpdateInsignias,
@Request() request: { user: Record<string, any> },
) {
const insignia = await this.insigniaRepository.findOne({
where: { id: id },
});
if (!insignia) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชอิสริยาภรณ์นี้");
}
const insigniaType = await this.insigniaTypeRepository.findOne({
where: { id: requestBody.insigniaTypeId},
});
if (!insigniaType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับชั้นเครื่องราชอิสริยาภรณ์นี้");
}
const rowRepeated = await this.insigniaRepository.findOne({
where: {
id: Not(id),
name: requestBody.name,
isActive: requestBody.isActive,
},
});
if (rowRepeated) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
}
insignia.lastUpdateUserId = request.user.sub;
insignia.lastUpdateFullName = request.user.name;
this.insigniaRepository.merge(insignia, requestBody);
await this.insigniaRepository.save(insignia);
return new HttpSuccess(insignia.id);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
* @param {string} id Id
*/
@Delete("{id}")
async delete(@Path() id: string) {
const delInsignia = await this.insigniaRepository.findOne({ where: { id } });
if (!delInsignia) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชอิสริยาภรณ์นี้");
}
await this.insigniaRepository.remove(delInsignia);
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_037 - (ADMIN) #
*
* @param {string} id Id
*/
@Get("{id}")
async GetInsigniaById(@Path() id: string) {
const insignia = await this.insigniaRepository.findOne({
relations: ["insigniaType"],
select: ["id", "name", "shortName", "createdAt", "lastUpdatedAt", "lastUpdateFullName", "isActive", "note"],
where: { id: id },
});
if (!insignia) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชอิสริยาภรณ์นี้");
}
const mapInsignia = {
id: insignia.id,
name: insignia.name,
shortName: insignia.shortName,
insigniaTypeName: insignia.insigniaType == null ? null : insignia.insigniaType.name, //ลำดับชั้นเครื่องราช
createdAt: insignia.createdAt,
lastUpdatedAt: insignia.lastUpdatedAt,
lastUpdateFullName: insignia.lastUpdateFullName,
isActive: insignia.isActive,
note: insignia.note
}
return new HttpSuccess(mapInsignia);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Get("")
async GetInsignia() {
const insigniaAll = await this.insigniaRepository.find({
relations: ["insigniaType"],
select: ["id", "name", "shortName", "createdAt", "lastUpdatedAt", "lastUpdateFullName", "isActive", "note"],
});
if (!insigniaAll) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชอิสริยาภรณ์นี้");
}
const mapInsigniaAll = insigniaAll.map((item) => ({
id: item.id,
name: item.name,
shortName: item.shortName,
insigniaTypeName: item.insigniaType == null ? null : item.insigniaType.name, //ลำดับชั้นเครื่องราช
createdAt: item.createdAt,
lastUpdatedAt: item.lastUpdatedAt,
lastUpdateFullName: item.lastUpdateFullName,
isActive: item.isActive,
note: item.note
}));
return new HttpSuccess(mapInsigniaAll);
}
}

View file

@ -0,0 +1,171 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} 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 { Equal, ILike, In, IsNull, Like, Not, Brackets, Between } from "typeorm";
import { InsigniaType, CreateInsigniaType, UpdateInsigniaType } from "../entities/InsigniaType";
import { Insignia, } from "../entities/Insignia";
@Route("api/v1/org/insignia/insignia-type")
@Tags("InsigniaType")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class InsigniaTypeController extends Controller {
private insigniaTypeRepository = AppDataSource.getRepository(InsigniaType);
private insigniaRepository = AppDataSource.getRepository(Insignia);
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Post("")
async CreateInsigniaType(
@Body() requestBody: CreateInsigniaType,
@Request() request: { user: Record<string, any> },
) {
const insigniaType = Object.assign(new InsigniaType(), requestBody);
if (!insigniaType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const rowRepeated = await this.insigniaTypeRepository.findOne({
where: {
name: requestBody.name,
isActive: requestBody.isActive,
},
});
if (rowRepeated) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
}
insigniaType.createdUserId = request.user.sub;
insigniaType.createdFullName = request.user.name;
insigniaType.lastUpdateUserId = request.user.sub;
insigniaType.lastUpdateFullName = request.user.name;
await this.insigniaTypeRepository.save(insigniaType);
return new HttpSuccess(insigniaType.id);
}
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Put("{id}")
async UpdateInsigniaType(
@Path() id: string,
@Body() requestBody: UpdateInsigniaType,
@Request() request: { user: Record<string, any> },
) {
const insigniaType = await this.insigniaTypeRepository.findOne({
where: { id: id },
});
if (!insigniaType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับชั้นเครื่องราชอิสริยาภรณ์นี้");
}
const rowRepeated = await this.insigniaTypeRepository.findOne({
where: {
id: Not(id),
name: requestBody.name,
isActive: requestBody.isActive,
},
});
if (rowRepeated) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
}
insigniaType.lastUpdateUserId = request.user.sub;
insigniaType.lastUpdateFullName = request.user.name;
this.insigniaTypeRepository.merge(insigniaType, requestBody);
await this.insigniaTypeRepository.save(insigniaType);
return new HttpSuccess(insigniaType.id);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
* @param {string} id Id
*/
@Delete("{id}")
async delete(@Path() id: string) {
const delInsigniaType = await this.insigniaTypeRepository.findOne({ where: { id } });
if (!delInsigniaType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับชั้นเครื่องราชอิสริยาภรณ์นี้");
}
const Insignia = await this.insigniaRepository.find({
where: { insigniaTypeId: id }
});
if (Insignia.length > 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้ เนื่องจากพบข้อมูลที่ตารางเครื่องราชอิสริยาภรณ์");
}
await this.insigniaTypeRepository.remove(delInsigniaType);
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_037 - (ADMIN) #
*
* @param {string} id Id
*/
@Get("{id}")
async GetInsigniaTypeById(@Path() id: string) {
const insigniaType = await this.insigniaTypeRepository.findOne({
select: ["id", "name", "createdAt", "lastUpdatedAt", "lastUpdateFullName", "isActive"],
where: { id: id },
});
if (!insigniaType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับชั้นเครื่องราชอิสริยาภรณ์ นี้");
}
return new HttpSuccess(insigniaType);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Get("")
async GetInsigniaType() {
const insigniaTypeAll = await this.insigniaTypeRepository.find({
select: ["id", "name", "createdAt", "lastUpdatedAt", "lastUpdateFullName", "isActive"]
});
if (!insigniaTypeAll) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับชั้นเครื่องราชอิสริยาภรณ์ นี้");
}
return new HttpSuccess(insigniaTypeAll);
}
}

View file

@ -88,6 +88,12 @@
},
{
"name": "EmployeePosLevel", "description": "ระดับชั้นงานลูกจ้างประจำ"
},
{
"name": "InsigniaType", "description": "ลำดับชั้นเครื่องราชอิสริยาภรณ์"
},
{
"name": "Insignia", "description": "เครื่องราชอิสริยาภรณ์"
}
]
},