|
| 1 | +import { |
| 2 | + Authorized, |
| 3 | + Delete, |
| 4 | + Get, |
| 5 | + JsonController, |
| 6 | + Param, |
| 7 | + Put, |
| 8 | + Post, |
| 9 | + Body, |
| 10 | +} from "routing-controllers"; |
| 11 | +import { Inject } from "typedi"; |
| 12 | +import { UserRole } from "../entities/user-role"; |
| 13 | +import { |
| 14 | + CriterionServiceToken, |
| 15 | + ICriterionService, |
| 16 | +} from "../services/criterion-service"; |
| 17 | +import { |
| 18 | + CriterionDTO, |
| 19 | + SuccessResponseDTO, |
| 20 | + convertBetweenEntityAndDTO, |
| 21 | +} from "./dto"; |
| 22 | +import { Criterion } from "../entities/criterion"; |
| 23 | + |
| 24 | +@JsonController("/criteria") |
| 25 | +export class CriterionController { |
| 26 | + public constructor( |
| 27 | + @Inject(CriterionServiceToken) |
| 28 | + private readonly _criterion: ICriterionService, |
| 29 | + ) {} |
| 30 | + |
| 31 | + /** |
| 32 | + * Get all criteria. |
| 33 | + */ |
| 34 | + @Get("/") |
| 35 | + @Authorized(UserRole.User) |
| 36 | + public async getAllCriteria(): Promise<CriterionDTO[]> { |
| 37 | + const criteria = await this._criterion.getAllCriteria(); |
| 38 | + return criteria.map((c) => convertBetweenEntityAndDTO(c, CriterionDTO)); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Create a criterion. |
| 43 | + */ |
| 44 | + @Post("/") |
| 45 | + @Authorized(UserRole.Root) |
| 46 | + public async createCriterion( |
| 47 | + @Body() { data: criterionDTO }: { data: CriterionDTO }, |
| 48 | + ): Promise<CriterionDTO> { |
| 49 | + const criterion = convertBetweenEntityAndDTO(criterionDTO, Criterion); |
| 50 | + const createdCriterion = await this._criterion.createCriterion(criterion); |
| 51 | + return convertBetweenEntityAndDTO(createdCriterion, CriterionDTO); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Update criteria. |
| 56 | + */ |
| 57 | + @Put("/:id") |
| 58 | + @Authorized(UserRole.Root) |
| 59 | + public async updateCriterion( |
| 60 | + @Param("id") criterionId: number, |
| 61 | + @Body() { data: criterionDTO }: { data: CriterionDTO }, |
| 62 | + ): Promise<CriterionDTO> { |
| 63 | + const criterion = convertBetweenEntityAndDTO( |
| 64 | + { ...criterionDTO, id: criterionId }, |
| 65 | + Criterion, |
| 66 | + ); |
| 67 | + const updateCriterion = await this._criterion.updateCriterion(criterion); |
| 68 | + return convertBetweenEntityAndDTO(updateCriterion, CriterionDTO); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Delete criteria. |
| 73 | + */ |
| 74 | + @Delete("/:id") |
| 75 | + @Authorized(UserRole.Root) |
| 76 | + public async deleteCriterion( |
| 77 | + @Param("id") criterionId: number, |
| 78 | + ): Promise<SuccessResponseDTO> { |
| 79 | + await this._criterion.deleteCriterionByID(criterionId); |
| 80 | + const response = new SuccessResponseDTO(); |
| 81 | + response.success = true; |
| 82 | + return response; |
| 83 | + } |
| 84 | +} |
0 commit comments