crud development
This commit is contained in:
parent
454cda29e5
commit
9da7f47cf6
7 changed files with 393 additions and 0 deletions
171
src/controllers/DevelopmentController.ts
Normal file
171
src/controllers/DevelopmentController.ts
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue