feat: profile training endpoints
This commit is contained in:
parent
8ac8339a6b
commit
294411a114
1 changed files with 108 additions and 0 deletions
108
src/controllers/ProfileTrainingController.ts
Normal file
108
src/controllers/ProfileTrainingController.ts
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Patch,
|
||||
Path,
|
||||
Post,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import {
|
||||
CreateProfileTraining,
|
||||
ProfileTraining,
|
||||
UpdateProfileTraining,
|
||||
} from "../entities/ProfileTraining";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
|
||||
@Route("api/v1/org/profile/training")
|
||||
@Tags("ProfileTraining")
|
||||
@Security("bearerAuth")
|
||||
export class ProfileTrainingController extends Controller {
|
||||
private trainingRepo = AppDataSource.getRepository(ProfileTraining);
|
||||
private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory);
|
||||
|
||||
@Get("{profileId}")
|
||||
public async getTraining(@Path() profileId: string) {
|
||||
const record = await this.trainingRepo.findBy({ profileId });
|
||||
return new HttpSuccess(record);
|
||||
}
|
||||
|
||||
@Get("history/{trainingId}")
|
||||
public async trainingHistory(@Path() trainingId: string) {
|
||||
const record = await this.trainingHistoryRepo.findBy({
|
||||
profileTrainingId: trainingId,
|
||||
});
|
||||
return record;
|
||||
}
|
||||
|
||||
@Post()
|
||||
public async newTraining(@Request() req: RequestWithUser, @Body() body: CreateProfileTraining) {
|
||||
const data = new ProfileTraining();
|
||||
const history = new ProfileTrainingHistory();
|
||||
|
||||
const meta = {
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
};
|
||||
|
||||
Object.assign(data, { ...body, ...meta });
|
||||
Object.assign(history, { ...body, ...meta });
|
||||
|
||||
const result = await this.trainingRepo.save(data);
|
||||
|
||||
history.profileTrainingId = result.id;
|
||||
|
||||
await this.trainingHistoryRepo.save(history);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Patch("{trainingId}")
|
||||
public async editTraining(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: UpdateProfileTraining,
|
||||
@Path() trainingId: string,
|
||||
) {
|
||||
const record = await this.trainingRepo.findOneBy({ id: trainingId });
|
||||
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
const history = new ProfileTrainingHistory();
|
||||
|
||||
Object.assign(record, body);
|
||||
Object.assign(history, { ...body, id: undefined });
|
||||
history.profileTrainingId = trainingId;
|
||||
record.lastUpdateFullName = req.user.name;
|
||||
history.lastUpdateFullName = req.user.name;
|
||||
|
||||
await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Delete("{trainingId}")
|
||||
public async deleteTraining(@Path() trainingId: string) {
|
||||
await this.trainingHistoryRepo.delete({
|
||||
profileTrainingId: trainingId,
|
||||
});
|
||||
|
||||
const result = await this.trainingRepo.delete({ id: trainingId });
|
||||
|
||||
if (result.affected && result.affected <= 0) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue