crud profileSalary
This commit is contained in:
parent
e147412ea4
commit
5cb6126b39
2 changed files with 212 additions and 26 deletions
165
src/controllers/ProfileSalaryController.ts
Normal file
165
src/controllers/ProfileSalaryController.ts
Normal file
|
|
@ -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<string, any> },
|
||||
) {
|
||||
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<string, any> },
|
||||
) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue