diff --git a/src/app.module.ts b/src/app.module.ts index 91f8bcb4a12aad77ab1d506b941bf71f7278a2a6..cb69a91fd60f1fb0fccb3b37452bb28f988e4cf5 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -11,6 +11,7 @@ import { LoggingInterceptor } from './shared/logging.interceptor'; import { NotificationModule } from './notifications/notifications.module'; import { GameModule } from './game/game.module'; import { RolesGuard } from './shared/roles.guard'; +import { TaskModule } from './task/task.module'; @Module({ imports: [ @@ -18,6 +19,7 @@ import { RolesGuard } from './shared/roles.guard'; UserModule, GameModule, NotificationModule, + TaskModule, ], controllers: [AppController], providers: [ diff --git a/src/task/task.controller.ts b/src/task/task.controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..23c1593699525d4848649feea787af2105bb5e95 --- /dev/null +++ b/src/task/task.controller.ts @@ -0,0 +1,41 @@ +import { + Controller, + Post, + Body, + UsePipes, + Get, + UseGuards, + Param, +} from '@nestjs/common'; +import { TaskService } from './task.service'; +import { Roles } from '../shared/roles.decorator'; +import { ValidationPipe } from '../shared/validation.pipe'; +import { TaskDTO } from './task.dto'; + +@Controller('task') +export class TaskController { + constructor(private taskService: TaskService) {} + + /* @Post('new') + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + async newGame(@User('id') person, @Body() body: GameDTO) { + return this.gameservice.createNewGame(person, body); + } + + @Put(':id') + @Roles('admin') + @UsePipes(new ValidationPipe()) + async editGame(@Param('id') id: string, @Body() body: GameDTO) { + return this.gameservice.editGame(id, body); + } */ + + // creates a new task if the user has admin role in the game + // :id is the id of the game + @Post('new-task/:id') + @Roles('admin') + @UsePipes(new ValidationPipe()) + async newTask(@Param('id') id: string, @Body() task: TaskDTO) { + return this.taskService.newTask(task); + } +} diff --git a/src/task/task.dto.ts b/src/task/task.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..37e4fa5cb636df0cba28d03e6ca457366bf8d4c9 --- /dev/null +++ b/src/task/task.dto.ts @@ -0,0 +1,22 @@ +import { IsString, Length, IsNumber, IsBoolean } from 'class-validator'; +import { FactionEntity } from 'src/game/faction.entity'; + +export class TaskDTO { + @IsString() + @Length(3, 31) + taskName: string; + @IsString() + @Length(0, 255) + taskDescription: string; + @IsNumber() + @Length(1, 3) + taskScore: number; + @IsString() + @Length(3, 31) + taskWinner?: string; + @IsBoolean() + taskIsActive: boolean; + // faction unique id + @IsString() + faction: FactionEntity; +} diff --git a/src/task/task.entity.ts b/src/task/task.entity.ts new file mode 100644 index 0000000000000000000000000000000000000000..1e7470102800e44bb60d68d73340eedb67e8b93e --- /dev/null +++ b/src/task/task.entity.ts @@ -0,0 +1,15 @@ +import { PrimaryGeneratedColumn, Column, Entity, ManyToOne } from 'typeorm'; +import { FactionEntity } from 'src/game/faction.entity'; + +@Entity('Task') +export class TaskEntity { + @PrimaryGeneratedColumn('uuid') taskId: string; + @Column({ type: 'text' }) taskName: string; + @Column({ type: 'text' }) taskDescription: string; + @Column({ type: 'float' }) taskScore: number; + @Column({ type: 'text' }) taskWinner: string; + @Column({ type: 'bool' }) taskIsActive: boolean; + + @ManyToOne(type => FactionEntity, faction => faction.factionId) + faction: FactionEntity; +} diff --git a/src/task/task.module.ts b/src/task/task.module.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea321484169a3868f1f5d8d49f62574ab2253be8 --- /dev/null +++ b/src/task/task.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + +import { TaskService } from './task.service'; +import { TaskController } from './task.controller'; +import { TaskEntity } from './task.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([TaskEntity])], + controllers: [TaskController], + providers: [TaskService], +}) +export class TaskModule {} diff --git a/src/task/task.service.ts b/src/task/task.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..830e6156222be1039e16c44cd50de1b453628991 --- /dev/null +++ b/src/task/task.service.ts @@ -0,0 +1,21 @@ +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { Repository } from 'typeorm'; +import { InjectRepository } from '@nestjs/typeorm'; +import { TaskEntity } from './task.entity'; +import { TaskDTO } from './task.dto'; + +@Injectable() +export class TaskService { + constructor( + @InjectRepository(TaskEntity) + private taskRepository: Repository<TaskEntity>, + ) {} + + async newTask(task: TaskDTO) { + const createdTask = await this.taskRepository.create(task); + await this.taskRepository.insert(createdTask); + return { + message: 'Task added', + }; + } +}