|
| 1 | +import { NotFoundError } from "routing-controllers"; |
1 | 2 | import { Inject, Service, Token } from "typedi"; |
2 | 3 | import { Repository } from "typeorm"; |
3 | 4 | import { IService } from "."; |
|
8 | 9 | convertBetweenEntityAndDTO, |
9 | 10 | } from "../controllers/dto"; |
10 | 11 | import { User } from "../entities/user"; |
| 12 | +import { UserRole } from "../entities/user-role"; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * An interface describing user handling. |
@@ -72,9 +74,23 @@ export class ProjectService implements IProjectService { |
72 | 74 | * @param project The project to update |
73 | 75 | */ |
74 | 76 | public async updateProject(project: Project, user: User): Promise<Project> { |
75 | | - // TODO |
76 | | - await this.checkPermission(project, user); |
77 | | - // TODO allow changing allowRating only if admin |
| 77 | + const existing = await this._projects.findOneBy({ id: project.id }); |
| 78 | + |
| 79 | + if (!existing) { |
| 80 | + throw new NotFoundError(); |
| 81 | + } |
| 82 | + |
| 83 | + await this.checkPermission(existing, user); |
| 84 | + |
| 85 | + existing.title = project.title; |
| 86 | + existing.description = project.description; |
| 87 | + |
| 88 | + // Only admins may change allowRating |
| 89 | + if (user.role === UserRole.Root) { |
| 90 | + existing.allowRating = project.allowRating; |
| 91 | + } |
| 92 | + |
| 93 | + return this._projects.save(existing); |
78 | 94 | } |
79 | 95 |
|
80 | 96 | /** |
|
0 commit comments