179 lines
5.3 KiB
TypeScript
179 lines
5.3 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
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 { CommandSys, CreateCommandSys, UpdateCommandSys } from "../entities/CommandSys";
|
|
|
|
@Route("api/v1/org/commandSys")
|
|
@Tags("CommandSys")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
export class CommandSysController extends Controller {
|
|
private commandSysRepository = AppDataSource.getRepository(CommandSys);
|
|
|
|
/**
|
|
* API list รายการระบบคำสั่ง
|
|
*
|
|
* @summary ORG_058 - CRUD ระบบคำสั่ง (ADMIN) #62
|
|
*
|
|
*/
|
|
@Get("list")
|
|
async GetResult() {
|
|
const _commandSys = await this.commandSysRepository.find({
|
|
select: [
|
|
"id",
|
|
"sysName",
|
|
"sysDescription",
|
|
"createdAt",
|
|
"lastUpdatedAt",
|
|
"createdFullName",
|
|
"lastUpdateFullName",
|
|
],
|
|
order: { order: "ASC" },
|
|
});
|
|
return new HttpSuccess(_commandSys);
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการระบบคำสั่ง
|
|
*
|
|
* @summary ORG_058 - CRUD ระบบคำสั่ง (ADMIN) #62
|
|
*
|
|
* @param {string} id Id ระบบคำสั่ง
|
|
*/
|
|
@Get("assign")
|
|
async GetAssgin() {
|
|
const _commandSys = await this.commandSysRepository.find({
|
|
order: { order: "ASC" },
|
|
relations: ["assgins"],
|
|
});
|
|
const data = _commandSys.map((data) => ({
|
|
id: data.id,
|
|
sysName: data.sysName,
|
|
assgins: data.assgins.map((x) => ({
|
|
id: x.id,
|
|
name: x.name,
|
|
description: x.description,
|
|
})),
|
|
}));
|
|
return new HttpSuccess(data);
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการระบบคำสั่ง
|
|
*
|
|
* @summary ORG_058 - CRUD ระบบคำสั่ง (ADMIN) #62
|
|
*
|
|
* @param {string} id Id ระบบคำสั่ง
|
|
*/
|
|
@Get("{id}")
|
|
async GetById(@Path() id: string) {
|
|
const _commandSys = await this.commandSysRepository.findOne({
|
|
where: { id },
|
|
select: ["id", "sysName", "sysDescription"],
|
|
});
|
|
if (!_commandSys) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระบบคำสั่งนี้");
|
|
}
|
|
|
|
return new HttpSuccess(_commandSys);
|
|
}
|
|
|
|
/**
|
|
* API สร้างรายการ body ระบบคำสั่ง
|
|
*
|
|
* @summary ORG_058 - CRUD ระบบคำสั่ง (ADMIN) #62
|
|
*
|
|
*/
|
|
@Post()
|
|
async Post(
|
|
@Body()
|
|
requestBody: CreateCommandSys,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const _commandSys = Object.assign(new CommandSys(), requestBody);
|
|
const checkName = await this.commandSysRepository.findOne({
|
|
where: {
|
|
id: requestBody.id,
|
|
sysName: requestBody.sysName,
|
|
sysDescription: requestBody.sysDescription,
|
|
},
|
|
});
|
|
|
|
if (checkName) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
|
|
_commandSys.createdUserId = request.user.sub;
|
|
_commandSys.createdFullName = request.user.name;
|
|
_commandSys.lastUpdateUserId = request.user.sub;
|
|
_commandSys.lastUpdateFullName = request.user.name;
|
|
_commandSys.createdAt = new Date();
|
|
_commandSys.lastUpdatedAt = new Date();
|
|
await this.commandSysRepository.save(_commandSys);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรายการ body ระบบคำสั่ง
|
|
*
|
|
* @summary ORG_058 - CRUD ระบบคำสั่ง (ADMIN) #62
|
|
*
|
|
* @param {string} id Id ระบบคำสั่ง
|
|
*/
|
|
@Put("{id}")
|
|
async Put(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: UpdateCommandSys,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const _commandSys = await this.commandSysRepository.findOne({ where: { id: id } });
|
|
if (!_commandSys) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระบบคำสั่งนี้");
|
|
}
|
|
|
|
_commandSys.lastUpdateUserId = request.user.sub;
|
|
_commandSys.lastUpdateFullName = request.user.name;
|
|
_commandSys.lastUpdatedAt = new Date();
|
|
this.commandSysRepository.merge(_commandSys, requestBody);
|
|
await this.commandSysRepository.save(_commandSys);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API ลบรายการระบบคำสั่ง
|
|
*
|
|
* @summary ORG_058 - CRUD ระบบคำสั่ง (ADMIN) #62
|
|
*
|
|
* @param {string} id Id ระบบคำสั่ง
|
|
*/
|
|
@Delete("{id}")
|
|
async Delete(@Path() id: string) {
|
|
const _delCommandSys = await this.commandSysRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!_delCommandSys) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระบบคำสั่งนี้");
|
|
}
|
|
await this.commandSysRepository.delete(_delCommandSys.id);
|
|
return new HttpSuccess();
|
|
}
|
|
}
|