-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrating-controller.ts
More file actions
73 lines (67 loc) · 1.94 KB
/
rating-controller.ts
File metadata and controls
73 lines (67 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {
Authorized,
JsonController,
CurrentUser,
Get,
Post,
Body,
Param,
} from "routing-controllers";
import { Inject } from "typedi";
import { UserRole } from "../entities/user-role";
import { RatingServiceToken, IRatingService } from "../services/rating-service";
import {
RatingDTO,
ProjectRatingResultDTO,
convertBetweenEntityAndDTO,
} from "./dto";
import { User } from "../entities/user";
import { Rating } from "../entities/rating";
@JsonController("/ratings")
export class RatingController {
public constructor(
@Inject(RatingServiceToken) private readonly _ratings: IRatingService,
) {}
/**
* Get aggregated rating results grouped by project and criteria.
*/
@Get("/by-project/:id")
@Authorized(UserRole.User)
public async getUsersRatingsForProject(
@Param("id") projectId: number,
@CurrentUser() user: User,
): Promise<RatingDTO[]> {
const results = await this._ratings.getUsersRatingsForProject(
projectId,
user,
);
return results.map((r) => convertBetweenEntityAndDTO(r, RatingDTO));
}
/**
* Get aggregated rating results grouped by project and criteria.
*/
@Get("/results")
@Authorized(UserRole.Root)
public async getRatingResults(): Promise<ProjectRatingResultDTO[]> {
const results = await this._ratings.getRatingResults();
return results.map((r) =>
convertBetweenEntityAndDTO(r, ProjectRatingResultDTO),
);
}
/**
* Rate a project
*/
@Post("/rate")
@Authorized(UserRole.User)
public async rate(
@Body() { data: ratingDTO }: { data: RatingDTO },
@CurrentUser() user: User,
): Promise<RatingDTO> {
const rating = convertBetweenEntityAndDTO(ratingDTO, Rating);
// Ensure ratings cannot be cast for other users,
// write the requesting user into it.
rating.user = user;
const createdRating = await this._ratings.upsertRating(rating, user);
return convertBetweenEntityAndDTO(createdRating, RatingDTO);
}
}