88 JsonController ,
99 NotAcceptableError ,
1010 NotFoundError ,
11+ Param ,
1112 Post ,
1213 Put ,
1314} from "routing-controllers" ;
@@ -39,7 +40,14 @@ import {
3940 IDsRequestDTO ,
4041 QuestionDTO ,
4142 StoreAnswersRequestDTO ,
43+ SuccessResponseDTO ,
44+ TeamDTO ,
45+ TeamRequestDTO ,
46+ TeamResponseDTO ,
47+ TeamUpdateDTO ,
4248} from "./dto" ;
49+ import { ITeamService , TeamServiceToken } from "../services/team-service" ;
50+ import { Team } from "../entities/team" ;
4351
4452@JsonController ( "/application" )
4553export class ApplicationController {
@@ -48,6 +56,8 @@ export class ApplicationController {
4856 private readonly _application : IApplicationService ,
4957 @Inject ( UserServiceToken )
5058 private readonly _users : IUserService ,
59+ @Inject ( TeamServiceToken )
60+ private readonly _teams : ITeamService ,
5161 ) { }
5262
5363 /**
@@ -232,4 +242,109 @@ export class ApplicationController {
232242
233243 await this . _application . checkIn ( user ) ;
234244 }
245+
246+ /**
247+ * Gets all existing teams.
248+ */
249+ @Get ( "/team" )
250+ @Authorized ( UserRole . User )
251+ public async getAllTeams ( ) : Promise < readonly TeamDTO [ ] > {
252+ const teams = await this . _teams . getAllTeams ( ) ;
253+ return teams . map ( ( team ) => convertBetweenEntityAndDTO ( team , TeamDTO ) ) ;
254+ }
255+
256+ /**
257+ * Creates a team.
258+ */
259+ @Post ( "/team" )
260+ @Authorized ( UserRole . User )
261+ public async createTeam (
262+ @Body ( ) { data : teamDTO } : { data : TeamRequestDTO } ,
263+ ) : Promise < TeamDTO > {
264+ const team = convertBetweenEntityAndDTO ( teamDTO , Team ) ;
265+ const createdTeam = await this . _teams . createTeam ( team ) ;
266+ return convertBetweenEntityAndDTO ( createdTeam , TeamDTO ) ;
267+ }
268+
269+ /**
270+ * Update a team.
271+ */
272+ @Put ( "/team" )
273+ @Authorized ( UserRole . User )
274+ public async updateTeam (
275+ @Body ( ) { data : teamDTO } : { data : TeamUpdateDTO } ,
276+ @CurrentUser ( ) user : User ,
277+ ) : Promise < TeamDTO > {
278+ const team = convertBetweenEntityAndDTO ( teamDTO , Team ) ;
279+ const updateTeam = await this . _teams . updateTeam ( team , user ) ;
280+ return convertBetweenEntityAndDTO ( updateTeam , TeamDTO ) ;
281+ }
282+
283+ /**
284+ * Request to join a team.
285+ * @param teamId The id of the team
286+ */
287+ @Post ( "/team/:id/request" )
288+ @Authorized ( UserRole . User )
289+ public async requestToJoinTeam (
290+ @Param ( "id" ) teamId : number ,
291+ @CurrentUser ( ) user : User ,
292+ ) : Promise < SuccessResponseDTO > {
293+ await this . _teams . requestToJoinTeam ( teamId , user ) ;
294+ const response = new SuccessResponseDTO ( ) ;
295+ response . success = true ;
296+ return response ;
297+ }
298+
299+ /**
300+ * Accept a user to a team.
301+ * @param teamId The id of the team
302+ * @param userId The id of the user
303+ */
304+ @Put ( "/team/:teamId/accept/:userId" )
305+ @Authorized ( UserRole . User )
306+ public async acceptUserToTeam (
307+ @Param ( "teamId" ) teamId : number ,
308+ @Param ( "userId" ) userId : number ,
309+ @CurrentUser ( ) user : User ,
310+ ) : Promise < SuccessResponseDTO > {
311+ await this . _teams . acceptUserToTeam ( teamId , userId , user ) ;
312+ const response = new SuccessResponseDTO ( ) ;
313+ response . success = true ;
314+ return response ;
315+ }
316+
317+ /**
318+ * Get team by id.
319+ * @param id The id of the team
320+ */
321+ @Get ( "/team/:id" )
322+ @Authorized ( UserRole . User )
323+ public async getTeamByID (
324+ @Param ( "id" ) teamId : number ,
325+ ) : Promise < TeamResponseDTO > {
326+ const team = await this . _teams . getTeamByID ( teamId ) ;
327+
328+ if ( team == null ) {
329+ throw new NotFoundError ( `no team with id ${ teamId } ` ) ;
330+ }
331+
332+ return team ;
333+ }
334+
335+ /**
336+ * Delete a team by id
337+ * @param id The id of the team
338+ */
339+ @Delete ( "/team/:id" )
340+ @Authorized ( UserRole . User )
341+ public async deleteTeamByID (
342+ @Param ( "id" ) teamId : number ,
343+ @CurrentUser ( ) user : User ,
344+ ) : Promise < SuccessResponseDTO > {
345+ await this . _teams . deleteTeamByID ( teamId , user ) ;
346+ const response = new SuccessResponseDTO ( ) ;
347+ response . success = true ;
348+ return response ;
349+ }
235350}
0 commit comments