CRUD คำนำหน้า
This commit is contained in:
parent
065d0fd7e5
commit
271848dec0
1 changed files with 173 additions and 0 deletions
173
src/controllers/PrefixController.ts
Normal file
173
src/controllers/PrefixController.ts
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Body,
|
||||
Path,
|
||||
Request,
|
||||
SuccessResponse,
|
||||
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 { Prefixe, CreatePrefixe, UpdatePrefixe } from "../entities/Prefixe";
|
||||
import { Not } from "typeorm";
|
||||
|
||||
@Route("api/v1/org/prefix")
|
||||
@Tags("Prefix")
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class PrefixController extends Controller {
|
||||
private prefixRepository = AppDataSource.getRepository(Prefixe);
|
||||
|
||||
/**
|
||||
* API list รายการคำนำหน้า
|
||||
*
|
||||
* @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61
|
||||
*
|
||||
*/
|
||||
@Get()
|
||||
async Get() {
|
||||
const _prefix = await this.prefixRepository.find({
|
||||
select: ["id", "name"],
|
||||
});
|
||||
if (!_prefix) {
|
||||
return new HttpSuccess([]);
|
||||
}
|
||||
try {
|
||||
return new HttpSuccess(_prefix);
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายละเอียดรายการคำนำหน้า
|
||||
*
|
||||
* @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61
|
||||
*
|
||||
* @param {string} id Id คำนำหน้า
|
||||
*/
|
||||
@Get("{id}")
|
||||
async GetById(@Path() id: string) {
|
||||
const _prefix = await this.prefixRepository.findOne({
|
||||
where: { id },
|
||||
select: ["id", "name"],
|
||||
});
|
||||
if (!_prefix) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
try {
|
||||
return new HttpSuccess(_prefix);
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API สร้างรายการ body คำนำหน้า
|
||||
*
|
||||
* @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
async Post(
|
||||
@Body()
|
||||
requestBody: CreatePrefixe,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const _prefix = Object.assign(new Prefixe(), requestBody);
|
||||
if (!_prefix) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
const checkName = await this.prefixRepository.findOne({
|
||||
where: { name: requestBody.name },
|
||||
});
|
||||
|
||||
if (checkName) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
|
||||
try {
|
||||
_prefix.createdUserId = request.user.sub;
|
||||
_prefix.createdFullName = request.user.name;
|
||||
_prefix.lastUpdateUserId = request.user.sub;
|
||||
_prefix.lastUpdateFullName = request.user.name;
|
||||
await this.prefixRepository.save(_prefix);
|
||||
return new HttpSuccess();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขรายการ body คำนำหน้า
|
||||
*
|
||||
* @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61
|
||||
*
|
||||
* @param {string} id Id คำนำหน้า
|
||||
*/
|
||||
@Put("{id}")
|
||||
async Put(
|
||||
@Path() id: string,
|
||||
@Body()
|
||||
requestBody: UpdatePrefixe,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const _prefix = await this.prefixRepository.findOne({ where: { id: id } });
|
||||
if (!_prefix) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + id);
|
||||
}
|
||||
const checkName = await this.prefixRepository.findOne({
|
||||
where: { id: Not(id), name: requestBody.name },
|
||||
});
|
||||
if (checkName) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
|
||||
try {
|
||||
_prefix.lastUpdateUserId = request.user.sub;
|
||||
_prefix.lastUpdateFullName = request.user.name;
|
||||
this.prefixRepository.merge(_prefix, requestBody);
|
||||
await this.prefixRepository.save(_prefix);
|
||||
return new HttpSuccess();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบรายการคำนำหน้า
|
||||
*
|
||||
* @summary ORG_056 - CRUD คำนำหน้า (ADMIN) #61
|
||||
*
|
||||
* @param {string} id Id คำนำหน้า
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async Delete(@Path() id: string) {
|
||||
const _delPrefix = await this.prefixRepository.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
if (!_delPrefix) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + id);
|
||||
}
|
||||
try {
|
||||
await this.prefixRepository.delete(_delPrefix.id);
|
||||
return new HttpSuccess();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue