From 2fc46a052188e3e91a1f048a8ba64f0f4c2eadf5 Mon Sep 17 00:00:00 2001
From: Ronnie Friman <L4168@student.jamk.fi>
Date: Sat, 6 Jul 2019 14:44:24 +0300
Subject: [PATCH] added delete-task path&service

---
 src/task/task.controller.ts | 23 ++++++++++++++++++++---
 src/task/task.service.ts    | 15 ++++++++++++++-
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/src/task/task.controller.ts b/src/task/task.controller.ts
index acadbda..8283ba5 100644
--- a/src/task/task.controller.ts
+++ b/src/task/task.controller.ts
@@ -1,7 +1,15 @@
-import { Controller, Post, Body, UsePipes, Get, Param } from '@nestjs/common';
+import {
+  Controller,
+  Post,
+  Body,
+  UsePipes,
+  Get,
+  Param,
+  Delete,
+} from '@nestjs/common';
 
 import { TaskService } from './task.service';
-import { CreateTaskDTO, EditTaskDTO } from './task.dto';
+import { CreateTaskDTO, EditTaskDTO, DeleteTaskDTO } from './task.dto';
 import { Roles } from '../shared/guard.decorator';
 import { ValidationPipe } from '../shared/validation.pipe';
 import { User } from '../user/user.decorator';
@@ -28,7 +36,16 @@ export class TaskController {
     return this.taskService.editTask(data);
   }
 
-  // lists all the tasks for the game if user has game_person entry
+  // deletes a created task if the user has admin role in the game
+  // :id is the id of the game
+  @Delete('delete-task/:id')
+  @Roles('admin')
+  @UsePipes(new ValidationPipe())
+  async deleteTask(@Param('id') id: string, @Body() data: DeleteTaskDTO) {
+    return this.taskService.deleteTask(data);
+  }
+
+  // lists all the tasks for the game if the user has game_person entry
   // :id is the id of the game
   @Get('get-tasks/:id')
   @Roles('soldier', 'factionleader', 'admin')
diff --git a/src/task/task.service.ts b/src/task/task.service.ts
index 7719ebe..cf4fe3f 100644
--- a/src/task/task.service.ts
+++ b/src/task/task.service.ts
@@ -3,7 +3,7 @@ import { Repository } from 'typeorm';
 import { InjectRepository } from '@nestjs/typeorm';
 
 import { TaskEntity } from './task.entity';
-import { CreateTaskDTO, EditTaskDTO } from './task.dto';
+import { CreateTaskDTO, EditTaskDTO, DeleteTaskDTO } from './task.dto';
 import { FactionEntity } from '../faction/faction.entity';
 import { Game_PersonEntity } from '../game/game.entity';
 import { NotificationGateway } from '../notifications/notifications.gateway';
@@ -69,6 +69,18 @@ export class TaskService {
     };
   }
 
+  async deleteTask(data: DeleteTaskDTO) {
+    const task = await this.taskRepository.findOne({ taskId: data.taskId });
+    if (task) {
+      await this.taskRepository.delete({ taskId: task.taskId });
+      return {
+        code: 200,
+        message: 'Task deleted',
+      };
+    }
+    throw new HttpException('Task not found', HttpStatus.BAD_REQUEST);
+  }
+
   async fetchTasks(user, taskGame) {
     const gamePerson = await this.gamePersonRepository.findOne({
       where: {
@@ -95,6 +107,7 @@ export class TaskService {
             faction: null,
           },
         ],
+        order: { taskIsActive: 'DESC' },
       });
     }
   }
-- 
GitLab