160 lines
5.1 KiB
TypeScript
160 lines
5.1 KiB
TypeScript
|
|
// import {
|
||
|
|
// Controller,
|
||
|
|
// Get,
|
||
|
|
// Post,
|
||
|
|
// Put,
|
||
|
|
// Delete,
|
||
|
|
// 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 HttpError from "../interfaces/http-error";
|
||
|
|
// import { Like, Not } from "typeorm";
|
||
|
|
// import HttpStatusCode from "../interfaces/http-status";
|
||
|
|
// import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink";
|
||
|
|
// @Route("api/v1/kpi/link")
|
||
|
|
// @Tags("kpiLink")
|
||
|
|
// @Security("bearerAuth")
|
||
|
|
// @Response(
|
||
|
|
// HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||
|
|
// "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||
|
|
// )
|
||
|
|
// @SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||
|
|
// export class kpiLinkController extends Controller {
|
||
|
|
// private kpiLinkRepository = AppDataSource.getRepository(KpiLink);
|
||
|
|
|
||
|
|
// /**
|
||
|
|
// * API สร้างเชื่อมโยง
|
||
|
|
// * @param requestBody
|
||
|
|
// * @returns
|
||
|
|
// */
|
||
|
|
// @Post()
|
||
|
|
// @Example({
|
||
|
|
// nameLinkKPI: "string", //ชื่อเชื่อมโยง
|
||
|
|
// })
|
||
|
|
// async createKpiLink(
|
||
|
|
// @Body() requestBody: createKpiLink,
|
||
|
|
// @Request() request: { user: Record<string, any> },
|
||
|
|
// ) {
|
||
|
|
// const kpiLink = Object.assign(new KpiLink(), requestBody);
|
||
|
|
// const chkkpinameLink = await this.kpiLinkRepository.findOne({
|
||
|
|
// where: {
|
||
|
|
// nameLinkKPI: requestBody.nameLinkKPI,
|
||
|
|
// },
|
||
|
|
// });
|
||
|
|
// if (chkkpinameLink) {
|
||
|
|
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว");
|
||
|
|
// }
|
||
|
|
// kpiLink.createdUserId = request.user.sub;
|
||
|
|
// kpiLink.createdFullName = request.user.name;
|
||
|
|
// kpiLink.lastUpdateUserId = request.user.sub;
|
||
|
|
// kpiLink.lastUpdateFullName = request.user.name;
|
||
|
|
// await this.kpiLinkRepository.save(kpiLink);
|
||
|
|
// return new HttpSuccess(kpiLink.id);
|
||
|
|
// }
|
||
|
|
|
||
|
|
// /**
|
||
|
|
// * API แก้ไขชื่อเชื่อมโยง
|
||
|
|
// * @param id ไอดีของเชื่อมโยง
|
||
|
|
// */
|
||
|
|
// @Put("{id}")
|
||
|
|
// async updateKpiLink(
|
||
|
|
// @Path() id: string,
|
||
|
|
// @Body() requestBody: updateKpiLink,
|
||
|
|
// @Request() request: { user: Record<string, any> },
|
||
|
|
// ) {
|
||
|
|
// const kpiLink = await this.kpiLinkRepository.findOne({
|
||
|
|
// where: { id: id },
|
||
|
|
// });
|
||
|
|
// if (!kpiLink) {
|
||
|
|
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้");
|
||
|
|
// }
|
||
|
|
|
||
|
|
// const chkkpinameLink = await this.kpiLinkRepository.findOne({
|
||
|
|
// where: {
|
||
|
|
// nameLinkKPI: requestBody.nameLinkKPI,
|
||
|
|
// },
|
||
|
|
// });
|
||
|
|
// if (chkkpinameLink) {
|
||
|
|
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว");
|
||
|
|
// }
|
||
|
|
// this.kpiLinkRepository.merge(kpiLink, requestBody);
|
||
|
|
// kpiLink.lastUpdateUserId = request.user.sub;
|
||
|
|
// kpiLink.lastUpdateFullName = request.user.name;
|
||
|
|
// await this.kpiLinkRepository.save(kpiLink);
|
||
|
|
// return new HttpSuccess(id);
|
||
|
|
// }
|
||
|
|
|
||
|
|
// /**
|
||
|
|
// * API ชื่อเชื่อมโยง
|
||
|
|
// * @param id
|
||
|
|
// */
|
||
|
|
// @Get("{id}")
|
||
|
|
// @Example({
|
||
|
|
// nameLinkKPI: "string", //ชื่อเชื่อมโยง
|
||
|
|
// })
|
||
|
|
// async KpiLinkById(@Path() id: string) {
|
||
|
|
// const kpiLink = await this.kpiLinkRepository.findOne({
|
||
|
|
// where: { id: id },
|
||
|
|
// select: ["nameLinkKPI"],
|
||
|
|
// });
|
||
|
|
// if (!kpiLink) {
|
||
|
|
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้");
|
||
|
|
// }
|
||
|
|
// return new HttpSuccess(kpiLink);
|
||
|
|
// }
|
||
|
|
|
||
|
|
// /**
|
||
|
|
// * API ลบเชื่อมโยง
|
||
|
|
// * @param id
|
||
|
|
// */
|
||
|
|
// @Delete("{id}")
|
||
|
|
// async deleteKpiLink(@Path() id: string) {
|
||
|
|
// const kpiLink = await this.kpiLinkRepository.findOne({
|
||
|
|
// where: { id: id },
|
||
|
|
// });
|
||
|
|
// if (!kpiLink) {
|
||
|
|
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้");
|
||
|
|
// }
|
||
|
|
|
||
|
|
// await this.kpiLinkRepository.remove(kpiLink);
|
||
|
|
// return new HttpSuccess();
|
||
|
|
// }
|
||
|
|
|
||
|
|
// /**
|
||
|
|
// * API list เชื่อมโยง
|
||
|
|
// * @param page
|
||
|
|
// * @param pageSize
|
||
|
|
// */
|
||
|
|
// @Get()
|
||
|
|
// async listKpiLink(
|
||
|
|
// @Query("page") page: number = 1,
|
||
|
|
// @Query("pageSize") pageSize: number = 10,
|
||
|
|
// @Query("keyword") keyword?: string,
|
||
|
|
// ) {
|
||
|
|
// let whereClause: any = {};
|
||
|
|
|
||
|
|
// if (keyword !== undefined && keyword !== "") {
|
||
|
|
// whereClause = {
|
||
|
|
// where: [{ nameLinkKPI: Like(`%${keyword}%`) }],
|
||
|
|
// };
|
||
|
|
// }
|
||
|
|
|
||
|
|
// const [kpiLink, total] = await this.kpiLinkRepository.findAndCount({
|
||
|
|
// ...whereClause,
|
||
|
|
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
||
|
|
// });
|
||
|
|
|
||
|
|
// return new HttpSuccess({ data: kpiLink, total });
|
||
|
|
// }
|
||
|
|
// }
|