hrms-api-development/src/controllers/DevelopmentController.ts

172 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-04-02 17:53:45 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Query,
Example
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { In, Not, MoreThan, Brackets, Like, MoreThanOrEqual, } from "typeorm";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Development, CreateDevelopment, UpdateDevelopment } from "../entities/Development";
@Route("api/v1/development/main")
@Tags("Development")
@Security("bearerAuth")
export class DevelopmentController extends Controller {
private developmentRepository = AppDataSource.getRepository(Development);
/**
* API /
*
* @summary DEV_001 - /#1
*
*/
@Post()
@Example({
name: "",
year: 2024,
})
async CreateDevelopment(
@Body() requestBody: CreateDevelopment,
@Request() request: { user: Record<string, any> },
) {
const development = Object.assign(new Development(), requestBody);
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chk_name = await this.developmentRepository.find({
where: {
name: requestBody.name,
year: requestBody.year
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " + requestBody.name + " ปีงบประมาณ: " + requestBody.year + " มีอยู่ในระบบแล้ว",
);
}
development.createdUserId = request.user.sub;
development.createdFullName = request.user.name;
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
return new HttpSuccess(development.id);
}
/**
* API /
*
* @summary DEV_002 - / #2
*
* @param {string} id Id
*/
@Put("{id}")
async UpdateDevelopment(
@Path() id: string,
@Body() requestBody: UpdateDevelopment,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({ where: { id } });
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
const chk_name = await this.developmentRepository.find({
where: {
name: requestBody.name,
year: requestBody.year,
id: Not(id)
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " + requestBody.name + " ปีงบประมาณ: " + requestBody.year + " มีอยู่ในระบบแล้ว",
);
}
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
this.developmentRepository.merge(development, requestBody);
await this.developmentRepository.save(development);
return new HttpSuccess(development.id);
}
/**
* API /
*
* @summary DEV_003 - / #3
*
* @param {string} id Id
*/
@Delete("{id}")
async DeleteDevelopment(@Path() id: string) {
const delDevelopment = await this.developmentRepository.findOne({ where: { id } });
if (!delDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
await this.developmentRepository.remove(delDevelopment);
return new HttpSuccess();
}
/**
* API /
*
* @summary DEV_004 - / #4
*
*/
@Get()
async GetDevelopmentLists(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
@Query("year") year: number = 2024,
) {
const [development, total] = await AppDataSource.getRepository(Development)
.createQueryBuilder("development")
.andWhere(year != 0 ? "development.year LIKE :year" : "1=1", { year: `${year}` })
.orWhere("development.name LIKE :keyword", { keyword: `${keyword}` })
.select([
"development.id",
"development.name",
"development.year",
])
.orderBy("development.year", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ data: development, total });
}
/**
* API /
*
* @summary DEV_005 - / #5
*
* @param {string} id Id
*/
@Get("{id}")
async GetDevelopemtById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
select: ["id", "name", "year"],
where: { id: id },
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
return new HttpSuccess(getDevelopment);
}
}