validate ชื่อซ้ำ ใน DB และ CRUD Relationship

This commit is contained in:
AdisakKanthawilang 2024-02-02 16:03:22 +07:00
parent a7c6f80a7c
commit 2fa49ddfcc
2 changed files with 184 additions and 140 deletions

View file

@ -44,11 +44,17 @@ export class PosExecutiveController extends Controller {
requestBody: CreatePosExecutive, requestBody: CreatePosExecutive,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) { const checkName = await this.posExecutiveRepository.findOne({
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); where: { posExecutiveName: requestBody.posExecutiveName },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
} }
try { try {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
posExecutive.createdUserId = request.user.sub; posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name; posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateUserId = request.user.sub;
@ -78,6 +84,15 @@ export class PosExecutiveController extends Controller {
if (!posExecutive) { if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
} }
const checkName = await this.posExecutiveRepository.findOne({
where: { posExecutiveName: requestBody.posExecutiveName },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
try { try {
posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name; posExecutive.lastUpdateFullName = request.user.name;

View file

@ -1,141 +1,170 @@
import { import {
Controller, Controller,
Post, Post,
Put, Put,
Delete, Delete,
Route, Route,
Security, Security,
Tags, Tags,
Body, Body,
Path, Path,
Request, Request,
SuccessResponse, SuccessResponse,
Response, Response,
Get, Get,
} from "tsoa"; } from "tsoa";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { CreateRelationship, Relationship } from "../entities/Relationship"; import { CreateRelationship, Relationship } from "../entities/Relationship";
@Route("api/v1/org/relationship") @Route("api/v1/org/relationship")
@Tags("Relationship") @Tags("Relationship")
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
HttpStatusCode.INTERNAL_SERVER_ERROR, HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
) )
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") @SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class RelationshipController extends Controller { export class RelationshipController extends Controller {
private relationshipRepository = AppDataSource.getRepository(Relationship); private relationshipRepository = AppDataSource.getRepository(Relationship);
/** /**
* API * API
* *
* @summary ORG_060 - (ADMIN) #65 * @summary ORG_060 - (ADMIN) #65
* *
*/ */
@Post("relationship") @Post()
async createRelationship( async createRelationship(
@Body() @Body()
requestBody: CreateRelationship, requestBody: CreateRelationship,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const checkName = await this.relationshipRepository.findOne({
where: { name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
try {
const relationship = Object.assign(new Relationship(), requestBody); const relationship = Object.assign(new Relationship(), requestBody);
if (!relationship) { relationship.createdUserId = request.user.sub;
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); relationship.createdFullName = request.user.name;
} relationship.lastUpdateUserId = request.user.sub;
try { relationship.lastUpdateFullName = request.user.name;
relationship.createdUserId = request.user.sub; await this.relationshipRepository.save(relationship);
relationship.createdFullName = request.user.name; return new HttpSuccess();
relationship.lastUpdateUserId = request.user.sub; } catch (error) {
relationship.lastUpdateFullName = request.user.name; return error;
await this.relationshipRepository.save(relationship);
return new HttpSuccess();
} catch (error) {
return error;
}
} }
/**
* API
*
* @summary ORG_060 - (ADMIN) #65
*
* @param {string} id Id
*/
@Put("relationship/{id}")
async updateRelationship(
@Path() id: string,
@Body()
requestBody: CreateRelationship,
@Request() request: { user: Record<string, any> },
) {
const relationship = await this.relationshipRepository.findOne({where: {id: id}});
if (!relationship) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
try {
relationship.lastUpdateUserId = request.user.sub;
relationship.lastUpdateFullName = request.user.name;
this.relationshipRepository.merge(relationship, requestBody);
await this.relationshipRepository.save(relationship);
return new HttpSuccess();
} catch (error) {
return error;
}
}
// /**
// * API ลบอัตรากำลัง
// *
// * @summary ORG_043 - ลบตำแหน่งทางการบริหาร (ADMIN) #46
// *
// * @param {string} id Id ตำแหน่งทางการบริหาร
// */
// @Delete("executive/{id}")
// async deletePosExecutive(@Path() id: string) {
// const delPosExecutive = await this.posExecutiveRepository.findOne({
// where: { id },
// });
// if (!delPosExecutive) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
// }
// try {
// await this.positionRepository.delete({ posExecutiveId: id });
// await this.posExecutiveRepository.delete({ id });
// return new HttpSuccess();
// } catch (error) {
// return error;
// }
// }
// /**
// * API รายละเอียดตำแหน่งทางการบริหาร
// *
// * @summary ORG_044 - รายละเอียดตำแหน่งทางการบริหาร (ADMIN) #47
// *
// * @param {string} id Id ตำแหน่งทางการบริหาร
// */
// @Get("executive/{id}")
// async detailPosExecutive(@Path() id: string) {
// const posExecutive = await this.posExecutiveRepository.findOne({
// where: { id },
// select:[
// "id",
// "posExecutiveName",
// "posExecutivePriority"
// ]
// });
// if (!posExecutive) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
// }
// try {
// return new HttpSuccess(posExecutive);
// } catch (error) {
// return error;
// }
// }
} }
/**
* API
*
* @summary ORG_060 - (ADMIN) #65
*
* @param {string} id Id
*/
@Put("{id}")
async updateRelationship(
@Path() id: string,
@Body()
requestBody: CreateRelationship,
@Request() request: { user: Record<string, any> },
) {
const relationship = await this.relationshipRepository.findOne({ where: { id: id } });
if (!relationship) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
const checkName = await this.relationshipRepository.findOne({
where: { name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
try {
relationship.lastUpdateUserId = request.user.sub;
relationship.lastUpdateFullName = request.user.name;
this.relationshipRepository.merge(relationship, requestBody);
await this.relationshipRepository.save(relationship);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_060 - (ADMIN) #65
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteRelationship(@Path() id: string) {
const delRelationship = await this.relationshipRepository.findOne({
where: { id },
});
if (!delRelationship) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
}
try {
await this.relationshipRepository.delete({ id: id });
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_060 - (ADMIN) #65
*
* @param {string} id Id
*/
@Get("{id}")
async detailRelationship(@Path() id: string) {
const relationship = await this.relationshipRepository.findOne({
where: { id },
select: ["id", "name"],
});
if (!relationship) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
try {
return new HttpSuccess(relationship);
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_060 - (ADMIN) #65
*
* @param {string} id Id
*/
@Get()
async listRelationship() {
const relationship = await this.relationshipRepository.find({
select: ["id", "name"],
});
if (!relationship) {
return new HttpSuccess([]);
}
try {
return new HttpSuccess(relationship);
} catch (error) {
return error;
}
}
}