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

cleaing up code, added gameGroupDTO

parent a9f76d3f
No related branches found
No related tags found
4 merge requests!59Development to master,!31Development,!26Piirto2,!25Dto service
......@@ -8,7 +8,7 @@ import {
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
} from './game.entity';
import { GameDTO, FlagboxEventDTO } from './game.dto';
import { GameDTO, FlagboxEventDTO, GameGroupDTO } from './game.dto';
import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from './group.entity';
import { FactionEntity } from './faction.entity';
......@@ -39,22 +39,18 @@ export class GameService {
// create a new game
async createNewGame(personId: PersonEntity, gameData: GameDTO) {
// checks if a game with the same name exists already
const { name } = gameData;
if (await this.gameRepository.findOne({ where: { name } })) {
if (await this.gameRepository.findOne({ name: gameData.name })) {
throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST);
}
// else add the game to the database
const game = await this.gameRepository.create({
...gameData,
factions: gameData.factions,
});
const game = await this.gameRepository.create(gameData);
await this.gameRepository.insert(game);
// add gamePerson with role admin to the game
const gamePerson = await this.game_PersonRepository.create({
faction: null,
game: game,
person: personId,
role: 'admin',
});
gamePerson['role'] = 'admin';
await this.game_PersonRepository.insert(gamePerson);
return {
message: 'New game added',
......@@ -64,16 +60,13 @@ export class GameService {
// edit already created game
async editGame(id: string, gameData: GameDTO) {
// checks if a game with the same name exists already
const { name } = gameData;
if (await this.gameRepository.findOne({ name: name, id: Not(id) })) {
if (
await this.gameRepository.findOne({ name: gameData.name, id: Not(id) })
) {
throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST);
}
// update game entry in db
const updatedGame = await this.gameRepository.create({
...gameData,
factions: null,
objective_points: null,
});
const updatedGame = await this.gameRepository.create(gameData);
updatedGame['id'] = id;
const gameId = await this.gameRepository.save(updatedGame);
......@@ -131,7 +124,7 @@ export class GameService {
}
// checks the password, creates an entry in GamePerson table with associated role&faction
async createGroup(person, gameId, groupData) {
async createGroup(person, gameId, groupData: GameGroupDTO) {
// check if the person already is in a group in this game
const checkDuplicate = await this.game_PersonRepository.findOne({
person: person,
......@@ -280,3 +273,4 @@ export class GameService {
}
}
}

\ No newline at end of file
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