From 5cb6126b39dd5230a817aba9911b987da177cc7d Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 23 Feb 2024 15:15:11 +0700 Subject: [PATCH] crud profileSalary --- src/controllers/ProfileSalaryController.ts | 165 +++++++++++++++++++++ src/entities/ProfileSalary.ts | 73 +++++---- 2 files changed, 212 insertions(+), 26 deletions(-) create mode 100644 src/controllers/ProfileSalaryController.ts diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts new file mode 100644 index 00000000..e657a2c5 --- /dev/null +++ b/src/controllers/ProfileSalaryController.ts @@ -0,0 +1,165 @@ +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 { ProfileSalary, CreateProfileSalary, UpdateProfileSalary } from "../entities/ProfileSalary"; +import { Profile } from "../entities/Profile"; +import { Not } from "typeorm"; + +@Route("api/v1/org/profileSalary") +@Tags("ProfileSalary") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class ProfileSalaryController extends Controller { + + private profileSalaryRepository = AppDataSource.getRepository(ProfileSalary); + private profileRepository = AppDataSource.getRepository(Profile); + + /** + * API Create profile salary + * + * @summary Create profile salary + * + */ + @Post() + async CreateProfileSalary( + @Body() + requestBody: CreateProfileSalary, + @Request() request: { user: Record }, + ) { + const profileSalary = Object.assign(new ProfileSalary(), requestBody); + if(!profileSalary){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const profile = await this.profileRepository.findOne({ + where: {id : profileSalary.profileId} + }) + if(!profile){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: "+ profileSalary.profileId); + } + + profileSalary.createdUserId = request.user.sub; + profileSalary.createdFullName = request.user.name; + profileSalary.lastUpdateUserId = request.user.sub; + profileSalary.lastUpdateFullName = request.user.name; + await this.profileSalaryRepository.save(profileSalary); + return new HttpSuccess(); + + } + + /** + * API Update profile salary + * + * @summary Update profile salary + * + * @param {string} id Id ProfileSalaryId + */ + @Put("{id}") + async UpdateProfileSalary( + @Path() id: string, + @Body() + requestBody: UpdateProfileSalary, + @Request() request: { user: Record }, + ) { + const profileSalary = await this.profileSalaryRepository.findOne({ where: { id: id } }); + if (!profileSalary) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); + } + + profileSalary.lastUpdateUserId = request.user.sub; + profileSalary.lastUpdateFullName = request.user.name; + this.profileSalaryRepository.merge(profileSalary, requestBody); + await this.profileSalaryRepository.save(profileSalary); + return new HttpSuccess(); + + } + + /** + * API Delete profile salary + * + * @summary Delete profile salary + * + * @param {string} id Id ProfileSalaryId + */ + @Delete("{id}") + async DeleteProfileSalary(@Path() id: string) { + const delprofileSalary = await this.profileSalaryRepository.findOne({ + where: { id }, + }); + if (!delprofileSalary) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); + } + try { + await this.profileSalaryRepository.delete({ id: id }); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API GetById ProfileSalary + * + * @summary GetById ProfileSalary + * + * @param {string} id Id ProfileSalaryId + */ + @Get("{id}") + async GetById(@Path() id: string) { + const profileSalary = await this.profileSalaryRepository.findOne({ + where: { id }, + select: ["id", "date", "amount", "positionSalaryAmount", "mouthSalaryAmount", "profileId"], + }); + if (!profileSalary) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(profileSalary); + } catch (error) { + return error; + } + } + + /** + * API GetLists profile salary + * + * @summary GetLists profile salary + * + */ + @Get() + async GetLists() { + const profileSalary = await this.profileSalaryRepository.find({ + select: ["id", "date", "amount", "positionSalaryAmount", "mouthSalaryAmount", "profileId"], + // order: { createdAt: "ASC" }, + }); + + if (!profileSalary) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(profileSalary); + } catch (error) { + return error; + } + } +} diff --git a/src/entities/ProfileSalary.ts b/src/entities/ProfileSalary.ts index a76a5d01..efa243f9 100644 --- a/src/entities/ProfileSalary.ts +++ b/src/entities/ProfileSalary.ts @@ -38,6 +38,7 @@ export class ProfileSalary extends EntityBase { @Column({ length: 40, comment: "ไอดีโปรไฟล์", + type: "uuid" }) profileId: string; @@ -45,38 +46,58 @@ export class ProfileSalary extends EntityBase { @JoinColumn({ name: "profileId" }) profile: Profile; +} + +export class CreateProfileSalary { + + @Column({ + type: "datetime" + }) + date: Date; + + @Column({ + type: "double" + }) + amount: Double; + + @Column({ + type: "double" + }) + positionSalaryAmount: Double; + + @Column({ + type: "double" + }) + mouthSalaryAmount: Double; + + @Column({ + type: "uuid" + }) + profileId: string; } -// export class CreateProfileSalary { +export class UpdateProfileSalary { -// @Column() -// date: Date; + @Column({ + type: "datetime" + }) + date: Date; -// @Column() -// amount: Double; + @Column({ + type: "double" + }) + amount: Double; -// @Column() -// positionSalaryAmount: Double; + @Column({ + type: "double" + }) + positionSalaryAmount: Double; -// @Column() -// mouthSalaryAmount: Double; + @Column({ + type: "double" + }) + mouthSalaryAmount: Double; -// } - -// export class UpdateProfileSalary { - -// @Column() -// date: Date; - -// @Column() -// amount: Double; - -// @Column() -// positionSalaryAmount: Double; - -// @Column() -// mouthSalaryAmount: Double; - -// } +}