From 01d5aabc844b1d7eba4cb3cba6d651d1f95af96e Mon Sep 17 00:00:00 2001 From: L4168 <L4168@student.jamk.fi> Date: Wed, 10 Jul 2019 12:37:07 +0300 Subject: [PATCH] reworked draw services --- src/draw/draw.controller.ts | 22 ++++++++++++------- src/draw/draw.service.ts | 43 +++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/draw/draw.controller.ts b/src/draw/draw.controller.ts index 26f6cf8..39db26a 100644 --- a/src/draw/draw.controller.ts +++ b/src/draw/draw.controller.ts @@ -7,12 +7,14 @@ import { UsePipes, ValidationPipe, Body, + UseInterceptors, + ClassSerializerInterceptor, } from '@nestjs/common'; -import { AuthGuard } from '../shared/auth.guard'; import { DrawService } from './draw.service'; import { Roles, GameStates } from '../shared/guard.decorator'; -import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto'; +import { MapDrawingDTO } from './mapdrawing.dto'; +import { GamePerson } from 'src/game/gameperson.decorator'; /* DrawController @@ -29,14 +31,18 @@ export class DrawController { @Roles('admin', 'factionleader') @GameStates('CREATED', 'STARTED') @UsePipes(new ValidationPipe()) - async draw(@Param('id') gameId, @Body() data: MapDrawingDTO) { - return this.drawService.draw(gameId, data); + async draw( + @GamePerson() gameperson, + @Param('id') gameId, + @Body() data: MapDrawingDTO, + ) { + return this.drawService.draw(gameperson, gameId, data); } @Get('map/:id') - @UseGuards(new AuthGuard()) - @UsePipes(new ValidationPipe()) - async drawMap(@Param('id') id, @Body() data: ReturnDrawingsDTO) { - return this.drawService.drawMap(id, data); + @Roles('admin', 'factionleader', 'soldier', 'groupleader') + @UseInterceptors(ClassSerializerInterceptor) + async drawMap(@GamePerson() gameperson, @Param('id') gameId) { + return this.drawService.drawMap(gameperson, gameId); } } diff --git a/src/draw/draw.service.ts b/src/draw/draw.service.ts index d699af7..a046ce9 100644 --- a/src/draw/draw.service.ts +++ b/src/draw/draw.service.ts @@ -1,9 +1,9 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { MapDrawingEntity } from '../draw/coordinate.entity'; -import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto'; +import { MapDrawingDTO } from './mapdrawing.dto'; import { NotificationGateway } from 'src/notifications/notifications.gateway'; @Injectable() @@ -14,38 +14,59 @@ export class DrawService { private notificationGateway: NotificationGateway, ) {} - async draw(gameId, data: MapDrawingDTO) { + async draw(gameperson, gameId, data: MapDrawingDTO) { data['gameId'] = gameId; const drawing = await this.mapDrawingRepository.create(data); this.notificationGateway.server.emit(gameId, { type: 'drawing-update', }); + // create new instance if id is null if (data.mapDrawingId == null || data.mapDrawingId == '') { - // luo uuden instanssin. + drawing.faction = gameperson.faction; + console.log(drawing); const mapDrawing = await this.mapDrawingRepository.insert(drawing); return mapDrawing.identifiers; - } else { - //päivittää mapDrawingin + } + // get ref from db + const draw = await this.mapDrawingRepository.findOne({ + where: { mapDrawingId: data.mapDrawingId }, + relations: ['faction'], + }); + if (await draw.ownershipCheck(gameperson.faction, gameperson.role)) { + // else update the existing instance return await this.mapDrawingRepository.save(drawing); } + + throw new HttpException( + 'Drawing is not from your faction!', + HttpStatus.BAD_REQUEST, + ); } - // draw map based on game and - async drawMap(id, data: ReturnDrawingsDTO) { + // draw map based on game and gameperson faction + async drawMap(gameperson, gameId) { + // return all active drawings if admin + if (gameperson.role === 'admin') { + return await this.mapDrawingRepository.find({ + where: { gameId: gameId, drawingIsActive: true }, + relations: ['faction'], + }); + } // return mapdrawings with given faction and gameid return await this.mapDrawingRepository.find({ where: [ { - gameId: id, - faction: data.factionId, + gameId: gameId, + faction: gameperson.faction, drawingIsActive: true, }, { - gameId: id, + gameId: gameId, faction: null, drawingIsActive: true, }, ], + relations: ['faction'], }); } } -- GitLab