Skip to content
Snippets Groups Projects
Commit 3eafdd7f authored by L4168's avatar L4168
Browse files

updated newTask and created editTask service

parent d640fafc
No related branches found
No related tags found
4 merge requests!59Development to master,!31Development,!25Dto service,!24Faction task edit
......@@ -2,20 +2,58 @@ 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';
import { CreateTaskDTO, EditTaskDTO } from './task.dto';
import { FactionEntity } from '../game/faction.entity';
@Injectable()
export class TaskService {
constructor(
@InjectRepository(TaskEntity)
private taskRepository: Repository<TaskEntity>,
@InjectRepository(FactionEntity)
private factionRepository: Repository<FactionEntity>,
) {}
async newTask(task: TaskDTO) {
async newTask(task: CreateTaskDTO) {
// check if is not null, check that the faction exists in the game
if (
task.faction != null &&
!(await this.factionRepository.findOne({
factionId: task.faction.toString(),
game: task.taskGame,
}))
) {
throw new HttpException('Faction not found', HttpStatus.BAD_REQUEST);
}
const createdTask = await this.taskRepository.create(task);
console.log(createdTask);
await this.taskRepository.insert(createdTask);
return {
message: 'Task added',
};
}
async editTask(data: EditTaskDTO) {
const task = await this.taskRepository.findOne(data.taskId);
console.log(task);
// checks if task is already closed
if (!task.taskIsActive) {
throw new HttpException('Task is not active', HttpStatus.BAD_REQUEST);
}
// checks if faction is valid
if (
!(await this.factionRepository.findOne({
factionId: data.taskWinner.toString(),
game: data.taskGame,
}))
) {
throw new HttpException('Faction not found', HttpStatus.BAD_REQUEST);
}
task.taskWinner = data.taskWinner;
task.taskIsActive = false;
await this.taskRepository.save(task);
return {
message: 'Task updated and closed',
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment