Skip to content

Commit 81ac6a0

Browse files
authored
Merge pull request #113 from hackaburg/copilot/fix-notfound-error-replacement
Split RatingController into RatingController and CriteriaController; fix ProjectController bugs
2 parents 23669a8 + 0bd1f79 commit 81ac6a0

4 files changed

Lines changed: 88 additions & 63 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {
2+
Authorized,
3+
Delete,
4+
JsonController,
5+
CurrentUser,
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 { RatingServiceToken, IRatingService } from "../services/rating-service";
14+
import {
15+
CriteriaDTO,
16+
SuccessResponseDTO,
17+
convertBetweenEntityAndDTO
18+
} from "./dto";
19+
import { User } from "../entities/user";
20+
import { Criteria } from "../entities/criteria";
21+
22+
@JsonController("/criteria")
23+
export class CriteriaController {
24+
public constructor(
25+
@Inject(RatingServiceToken) private readonly _ratings: IRatingService,
26+
) {}
27+
28+
/**
29+
* Create criteria.
30+
*/
31+
@Post("/")
32+
@Authorized(UserRole.Root)
33+
public async createCriteria(
34+
@Body() { data: criteriaDTO }: { data: CriteriaDTO },
35+
): Promise<CriteriaDTO> {
36+
const criteria = convertBetweenEntityAndDTO(criteriaDTO, Criteria);
37+
const createdCriteria = await this._ratings.createCriteria(criteria);
38+
return convertBetweenEntityAndDTO(createdCriteria, CriteriaDTO);
39+
}
40+
41+
/**
42+
* Update criteria.
43+
*/
44+
@Put("/:id")
45+
@Authorized(UserRole.Root)
46+
public async updateCriteria(
47+
@Param("id") criteriaId: number,
48+
@Body() { data: criteriaDTO }: { data: CriteriaDTO },
49+
): Promise<CriteriaDTO> {
50+
// TODO There is a TeamUpdateDTO. CriteriaUpdateDTO?
51+
const criteria = convertBetweenEntityAndDTO(criteriaDTO, Criteria);
52+
const updateCriteria = await this._ratings.updateCriteria(criteria);
53+
return convertBetweenEntityAndDTO(updateCriteria, CriteriaDTO);
54+
}
55+
56+
/**
57+
* Delete criteria.
58+
*/
59+
@Delete("/:id")
60+
@Authorized(UserRole.Root)
61+
public async deleteCriteria(
62+
@Param("id") criteriaId: number,
63+
@CurrentUser() user: User,
64+
): Promise<SuccessResponseDTO> {
65+
await this._ratings.deleteCriteriaByID(criteriaId, user);
66+
const response = new SuccessResponseDTO();
67+
response.success = true;
68+
return response;
69+
}
70+
}

backend/src/controllers/project-controller.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { Authorized, Delete, JsonController, NotFoundError } from "routing-controllers";
1+
import { Authorized, Delete, JsonController, NotFoundError, Put, Param, Body, CurrentUser } from "routing-controllers";
22
import { Inject } from "typedi";
33
import { UserRole } from "../entities/user-role";
44
import { IProjectService, ProjectServiceToken } from "../services/project-service";
5+
import { ProjectDTO, convertBetweenEntityAndDTO } from "./dto";
6+
import { Project } from "../entities/project";
7+
import { User } from "../entities/user";
58

69
// TODO for every team, add a new project automatically with the correct teamId
710

@@ -19,13 +22,17 @@ export class ProjectController {
1922
public async updateProject(
2023
@Param("id") projectId: number,
2124
@Body() { data: projectDTO }: { data: ProjectDTO },
22-
): Promise<TeamDTO> {
25+
@CurrentUser() user: User,
26+
): Promise<ProjectDTO> {
2327
// TODO ProjectUpdateDTO?
24-
const project = convertBetweenEntityAndDTO(projectDTO, Project);
28+
const existing = await this._projects.getProjectByID(projectId);
2529

26-
// TODO how to make actual not found errors for incorrect ids?
30+
if (existing == null) {
31+
throw new NotFoundError();
32+
}
2733

28-
const updateProject = await this._ratings.updateProject(project, user);
29-
return convertBetweenEntityAndDTO(updateProject, ProjectDTO);
34+
const project = convertBetweenEntityAndDTO(projectDTO, Project);
35+
const updatedProject = await this._projects.updateProject(project, user);
36+
return convertBetweenEntityAndDTO(updatedProject, ProjectDTO);
3037
}
3138
}
Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import {
22
Authorized,
3-
Delete,
43
JsonController,
5-
ForbiddenError,
64
CurrentUser,
7-
Param,
8-
Put,
95
Post,
106
Body
117
} from "routing-controllers";
@@ -15,16 +11,11 @@ import { SettingsServiceToken, ISettingsService } from "../services/settings-ser
1511
import { RatingServiceToken, IRatingService } from "../services/rating-service";
1612
import {
1713
RatingDTO,
18-
CriteriaDTO,
19-
SuccessResponseDTO,
2014
convertBetweenEntityAndDTO
2115
} from "./dto";
2216
import { User } from "../entities/user";
23-
import { Criteria } from "../entities/criteria";
2417
import { Rating } from "../entities/rating";
2518

26-
// TODO separate rating and criteria controller?
27-
2819
@JsonController("/ratings")
2920
export class RatingController {
3021
public constructor(
@@ -69,48 +60,5 @@ export class RatingController {
6960
return convertBetweenEntityAndDTO(createdRating, RatingDTO);
7061
}
7162

72-
/**
73-
* Create criteria.
74-
*/
75-
@Post("/criteria")
76-
@Authorized(UserRole.Root)
77-
public async createCriteria(
78-
@Body() { data: criteriaDTO }: { data: CriteriaDTO },
79-
): Promise<CriteriaDTO> {
80-
const criteria = convertBetweenEntityAndDTO(criteriaDTO, Criteria);
81-
const createdCriteria = await this._ratings.createCriteria(criteria);
82-
return convertBetweenEntityAndDTO(createdCriteria, CriteriaDTO);
83-
}
84-
85-
/**
86-
* Update criteria.
87-
*/
88-
@Put("/criteria/:id")
89-
@Authorized(UserRole.Root)
90-
public async updateCriteria(
91-
@Param("id") criteriaId: number,
92-
@Body() { data: criteriaDTO }: { data: CriteriaDTO },
93-
): Promise<CriteriaDTO> {
94-
// TODO There is a TeamUpdateDTO. CriteriaUpdateDTO?
95-
const criteria = convertBetweenEntityAndDTO(criteriaDTO, Criteria);
96-
const updateCriteria = await this._ratings.updateCriteria(criteria);
97-
return convertBetweenEntityAndDTO(updateCriteria, CriteriaDTO);
98-
}
99-
100-
/**
101-
* Delete criteria.
102-
*/
103-
@Delete("/criteria/:id")
104-
@Authorized(UserRole.Root)
105-
public async deleteCriteria(
106-
@Param("id") criteriaId: number,
107-
@CurrentUser() user: User,
108-
): Promise<SuccessResponseDTO> {
109-
await this._ratings.deleteCriteriaByID(criteriaId, user);
110-
const response = new SuccessResponseDTO();
111-
response.success = true;
112-
return response;
113-
}
114-
11563
// TODO write test that all the root endpoints are not accessible by users
11664
}

backend/src/services/rating-service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ForbiddenError } from "routing-controllers";
1+
import { ForbiddenError, NotFoundError } from "routing-controllers";
22
import { Inject, Service, Token } from "typedi";
33
import { Repository } from "typeorm";
44
import { IService } from ".";
@@ -109,7 +109,7 @@ export class RatingService implements IRatingService {
109109
// TODO only if user matches
110110
await this.checkPermission(rating, user);
111111
if (!originRating) {
112-
throw new ForbiddenError("Rating not found");
112+
throw new NotFoundError("Rating not found");
113113
}
114114
const originRatingUser = originRating.user;
115115
if (user.id != originRatingUser.id) {
@@ -146,7 +146,7 @@ export class RatingService implements IRatingService {
146146
const rating = await this._ratings.findOneBy({ id });
147147

148148
if (!rating) {
149-
throw new ForbiddenError("Rating not found");
149+
throw new NotFoundError("Rating not found");
150150
}
151151

152152
await this.checkPermission(rating, currentUserId);
@@ -167,7 +167,7 @@ export class RatingService implements IRatingService {
167167

168168
const project = await this._projects.findOneBy({ id: rating.project.id });
169169
if (!project) {
170-
throw new ForbiddenError("Project not found");
170+
throw new NotFoundError("Project not found");
171171
}
172172
if (!project.allowRating) {
173173
// TODO test
@@ -176,7 +176,7 @@ export class RatingService implements IRatingService {
176176

177177
const team = await this._teams.findOneBy({ id: project.team.id })
178178
if (!team) {
179-
throw new ForbiddenError("Team not found");
179+
throw new NotFoundError("Team not found");
180180
}
181181
if (team.users.includes(user.id)) {
182182
// TODO test

0 commit comments

Comments
 (0)