แก้apiข้อมูลหลัก
This commit is contained in:
parent
73e07dfed6
commit
6b78a365fa
26 changed files with 1816 additions and 988 deletions
153
src/controllers/DistrictController.ts
Normal file
153
src/controllers/DistrictController.ts
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
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 { District, CreateDistrict, UpdateDistrict } from "../entities/District";
|
||||
import { Not } from "typeorm";
|
||||
|
||||
@Route("api/v1/org/metadata/district")
|
||||
@Tags("District")
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class DistrictController extends Controller {
|
||||
private districtRepository = AppDataSource.getRepository(District);
|
||||
|
||||
/**
|
||||
* API list รายการเขต
|
||||
*
|
||||
* @summary ORG_058 - CRUD เขต (ADMIN) #62
|
||||
*
|
||||
*/
|
||||
@Get()
|
||||
async GetResult() {
|
||||
const _district = await this.districtRepository.find({
|
||||
select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"],
|
||||
order: { createdAt: "ASC" },
|
||||
});
|
||||
return new HttpSuccess(_district);
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายละเอียดรายการเขต
|
||||
*
|
||||
* @summary ORG_058 - CRUD เขต (ADMIN) #62
|
||||
*
|
||||
* @param {string} id Id เขต
|
||||
*/
|
||||
@Get("{id}")
|
||||
async GetById(@Path() id: string) {
|
||||
const _district = await this.districtRepository.findOne({
|
||||
where: { id },
|
||||
select: ["id", "name"],
|
||||
relations: { subDistricts: true },
|
||||
});
|
||||
if (!_district) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
|
||||
}
|
||||
|
||||
return new HttpSuccess(_district);
|
||||
}
|
||||
|
||||
/**
|
||||
* API สร้างรายการ body เขต
|
||||
*
|
||||
* @summary ORG_058 - CRUD เขต (ADMIN) #62
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
async Post(
|
||||
@Body()
|
||||
requestBody: CreateDistrict,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const _district = Object.assign(new District(), requestBody);
|
||||
if (!_district) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
|
||||
}
|
||||
|
||||
const checkName = await this.districtRepository.findOne({
|
||||
where: { name: requestBody.name },
|
||||
});
|
||||
|
||||
if (checkName) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
|
||||
_district.createdUserId = request.user.sub;
|
||||
_district.createdFullName = request.user.name;
|
||||
_district.lastUpdateUserId = request.user.sub;
|
||||
_district.lastUpdateFullName = request.user.name;
|
||||
await this.districtRepository.save(_district);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขรายการ body เขต
|
||||
*
|
||||
* @summary ORG_058 - CRUD เขต (ADMIN) #62
|
||||
*
|
||||
* @param {string} id Id เขต
|
||||
*/
|
||||
@Put("{id}")
|
||||
async Put(
|
||||
@Path() id: string,
|
||||
@Body()
|
||||
requestBody: UpdateDistrict,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const _district = await this.districtRepository.findOne({ where: { id: id } });
|
||||
if (!_district) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
|
||||
}
|
||||
const checkName = await this.districtRepository.findOne({
|
||||
where: { id: Not(id), name: requestBody.name },
|
||||
});
|
||||
if (checkName) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
|
||||
_district.lastUpdateUserId = request.user.sub;
|
||||
_district.lastUpdateFullName = request.user.name;
|
||||
this.districtRepository.merge(_district, requestBody);
|
||||
await this.districtRepository.save(_district);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบรายการเขต
|
||||
*
|
||||
* @summary ORG_058 - CRUD เขต (ADMIN) #62
|
||||
*
|
||||
* @param {string} id Id เขต
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async Delete(@Path() id: string) {
|
||||
const _delDistrict = await this.districtRepository.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
if (!_delDistrict) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
|
||||
}
|
||||
await this.districtRepository.delete(_delDistrict.id);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue