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

removed try catches

parent 1e059b72
No related branches found
No related tags found
3 merge requests!59Development to master,!31Development,!21Error handling
...@@ -38,98 +38,88 @@ export class GameService { ...@@ -38,98 +38,88 @@ export class GameService {
// create a new game // create a new game
async createNewGame(personId: PersonEntity, gameData: GameDTO) { async createNewGame(personId: PersonEntity, gameData: GameDTO) {
try { // checks if a game with the same name exists already
// checks if a game with the same name exists already const { name } = gameData;
const { name } = gameData; if (await this.gameRepository.findOne({ where: { name } })) {
if (await this.gameRepository.findOne({ where: { name } })) { throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST);
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,
});
await this.gameRepository.insert(game);
const gamePerson = await this.game_PersonRepository.create({
faction: null,
game: game,
person: personId,
});
gamePerson['role'] = 'admin';
await this.game_PersonRepository.insert(gamePerson);
return {
message: 'New game added',
};
} catch (error) {
return error.message;
} }
// else add the game to the database
const game = await this.gameRepository.create({
...gameData,
factions: gameData.factions,
});
await this.gameRepository.insert(game);
const gamePerson = await this.game_PersonRepository.create({
faction: null,
game: game,
person: personId,
});
gamePerson['role'] = 'admin';
await this.game_PersonRepository.insert(gamePerson);
return {
message: 'New game added',
};
} }
// edit already created game // edit already created game
async editGame(id: string, gameData: GameDTO) { async editGame(id: string, gameData: GameDTO) {
try { // checks if a game with the same name exists already
// checks if a game with the same name exists already const { name } = gameData;
const { name } = gameData; if (await this.gameRepository.findOne({ name: name, id: Not(id) })) {
if (await this.gameRepository.findOne({ name: name, id: Not(id) })) { throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST);
throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST); }
} // update game entry in db
// update game entry in db const updatedGame = await this.gameRepository.create({
const updatedGame = await this.gameRepository.create({ ...gameData,
...gameData, factions: null,
factions: null, objective_points: null,
objective_points: null, });
updatedGame['id'] = id;
const gameId = await this.gameRepository.save(updatedGame);
// get all the factions that are associated with the game to deny duplicate entries
const factions = await this.factionRepository.find(gameId);
const factionNames = factions.map(({ factionName }) => factionName);
// add the factions to db
if (gameData.factions) {
gameData.factions.map(async faction => {
if (!Object.values(factionNames).includes(faction.factionName)) {
let name = await this.factionRepository.create({
...faction,
gameId: gameId,
});
await this.factionRepository.insert(name);
}
}); });
updatedGame['id'] = id; }
const gameId = await this.gameRepository.save(updatedGame);
// get all the factions that are associated with the game to deny duplicate entries
const factions = await this.factionRepository.find(gameId);
const factionNames = factions.map(({ factionName }) => factionName);
// add the factions to db
if (gameData.factions) {
gameData.factions.map(async faction => {
if (!Object.values(factionNames).includes(faction.factionName)) {
let name = await this.factionRepository.create({
...faction,
gameId: gameId,
});
await this.factionRepository.insert(name);
}
});
}
// get old flagboxes to deny duplicate entries // get old flagboxes to deny duplicate entries
const flagboxes = await this.objectivePointRepository.find({ const flagboxes = await this.objectivePointRepository.find({
game: gameId, game: gameId,
});
const flagboxIds = flagboxes.map(
({ objectivePointDescription }) => objectivePointDescription,
);
// insert the flagboxes to db
if (gameData.objective_points) {
gameData.objective_points.map(async flagbox => {
if (
!Object.values(flagboxIds).includes(flagbox.objectivePointDescription)
) {
let newFlagbox = await this.objectivePointRepository.create({
...flagbox,
game: gameId,
});
await this.objectivePointRepository.insert(newFlagbox);
}
}); });
const flagboxIds = flagboxes.map( }
({ objectivePointDescription }) => objectivePointDescription,
);
// insert the flagboxes to db
if (gameData.objective_points) {
gameData.objective_points.map(async flagbox => {
if (
!Object.values(flagboxIds).includes(
flagbox.objectivePointDescription,
)
) {
let newFlagbox = await this.objectivePointRepository.create({
...flagbox,
game: gameId,
});
await this.objectivePointRepository.insert(newFlagbox);
}
});
}
// TO DO: ADD FLAGBOX LOCATION TO MAPDRAWING ENTITY // TO DO: ADD FLAGBOX LOCATION TO MAPDRAWING ENTITY
return { return {
message: 'Game updated', message: 'Game updated',
}; };
} catch (error) {
return error;
}
} }
async deleteGame(id) { async deleteGame(id) {
...@@ -142,87 +132,71 @@ export class GameService { ...@@ -142,87 +132,71 @@ export class GameService {
// checks the password, creates an entry in GamePerson table with associated role&faction // checks the password, creates an entry in GamePerson table with associated role&faction
async createGroup(person, gameId, groupData) { async createGroup(person, gameId, groupData) {
try { // check if the person already is in a group in this game
// check if the person already is in a group in this game const checkDuplicate = await this.game_PersonRepository.findOne({
const checkDuplicate = await this.game_PersonRepository.findOne({ person: person,
person: person, });
}); if (checkDuplicate) {
if (checkDuplicate) { throw new HttpException(
throw new HttpException( 'You already belong to a group!',
'You already belong to a group!', HttpStatus.BAD_REQUEST,
HttpStatus.BAD_REQUEST, );
); }
}
// create a group entry and insert it to db
const group = await this.game_GroupRepository.create({
...groupData,
game: gameId,
});
const gameGroup = await this.game_GroupRepository.insert(group);
// create game_Person entry and insert it to db // create a group entry and insert it to db
const gamePerson = await this.game_PersonRepository.create({ const group = await this.game_GroupRepository.create({
role: 'soldier', ...groupData,
faction: null, game: gameId,
game: gameId, });
person: person, const gameGroup = await this.game_GroupRepository.insert(group);
leaderGroup: gameGroup.identifiers[0]['id'],
group: gameGroup.identifiers[0]['id'], // create game_Person entry and insert it to db
}); const gamePerson = await this.game_PersonRepository.create({
await this.game_PersonRepository.insert(gamePerson); role: 'soldier',
faction: null,
game: gameId,
person: person,
leaderGroup: gameGroup.identifiers[0]['id'],
group: gameGroup.identifiers[0]['id'],
});
await this.game_PersonRepository.insert(gamePerson);
return { return {
message: 'created new group', message: 'created new group',
}; };
} catch (e) {
return e;
}
} }
async showGroups() { async showGroups() {
try { return await this.game_GroupRepository.find({
return await this.game_GroupRepository.find({ relations: ['leader', 'players', 'game'],
relations: ['leader', 'players', 'game'], });
});
} catch (e) {
return e;
}
} }
async joinGroup(person, groupId) { async joinGroup(person, groupId) {
try { const gameData = await this.game_GroupRepository.findOne({
const gameData = await this.game_GroupRepository.findOne({ where: { id: groupId },
where: { id: groupId }, relations: ['players', 'game'],
relations: ['players', 'game'], });
}); const gamePerson = await this.game_PersonRepository.create({
const gamePerson = await this.game_PersonRepository.create({ role: 'soldier',
role: 'soldier', faction: null,
faction: null, game: gameData.game,
game: gameData.game, person: person,
person: person, leaderGroup: null,
leaderGroup: null, group: groupId,
group: groupId, });
}); await this.game_PersonRepository.insert(gamePerson);
await this.game_PersonRepository.insert(gamePerson); return {
return { message: 'Joined group',
message: 'Joined group', };
};
} catch (e) {
return e;
}
} }
// returns name and id of each game // returns name and id of each game
async listGames() { async listGames() {
try { const games = await this.gameRepository.find();
const games = await this.gameRepository.find(); return games.map(game => {
return games.map(game => { return game.gameObject();
return game.gameObject(); });
});
} catch (error) {
return error;
}
} }
// returns information about a game identified by id // returns information about a game identified by id
......
import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { Repository } from "typeorm"; import { Repository } from 'typeorm';
import { InjectRepository } from "@nestjs/typeorm"; import { InjectRepository } from '@nestjs/typeorm';
import { PersonEntity } from './user.entity'; import { PersonEntity } from './user.entity';
import { UserDTO } from './user.dto'; import { UserDTO } from './user.dto';
...@@ -9,56 +9,51 @@ import { GameDTO } from '../game/game.dto'; ...@@ -9,56 +9,51 @@ import { GameDTO } from '../game/game.dto';
@Injectable() @Injectable()
export class UserService { export class UserService {
constructor( constructor(
@InjectRepository(PersonEntity) @InjectRepository(PersonEntity)
private userRepository: Repository<PersonEntity>, private userRepository: Repository<PersonEntity>,
@InjectRepository(GameEntity) @InjectRepository(GameEntity)
private gameRepository: Repository<GameEntity> private gameRepository: Repository<GameEntity>,
) { } ) {}
async register(data: UserDTO) { async register(data: UserDTO) {
const { name } = data; const { name } = data;
let user = await this.userRepository.findOne({ where: { name } }); let user = await this.userRepository.findOne({ where: { name } });
if (user) { if (user) {
throw new HttpException('User already exists', HttpStatus.BAD_REQUEST); throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);
}
user = await this.userRepository.create(data);
await this.userRepository.save(user);
return user.tokenObject();
} }
user = await this.userRepository.create(data);
await this.userRepository.save(user);
return user.tokenObject();
}
async login(data: UserDTO) { async login(data: UserDTO) {
try { const { name, password } = data;
const { name, password } = data; const user = await this.userRepository.findOne({ where: { name } });
const user = await this.userRepository.findOne({ where: { name } }); if (!user || !(await user.comparePassword(password))) {
if (!user || !(await user.comparePassword(password))) { throw new HttpException('invalid password', HttpStatus.BAD_REQUEST);
throw new HttpException('invalid password', HttpStatus.BAD_REQUEST);
}
return user.tokenObject();
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
} }
return user.tokenObject();
}
// liitytään peliin // liitytään peliin
async joinGame(game: GameDTO, data: UserDTO) { async joinGame(game: GameDTO, data: UserDTO) {
try { try {
// etsi peli valinnan mukaan ja otetaan käyttäjän rooli kyseisestä pelistä talteen // etsi peli valinnan mukaan ja otetaan käyttäjän rooli kyseisestä pelistä talteen
const role = await this.gameRepository.findOne(); const role = await this.gameRepository.findOne();
return role; return role;
} catch (error) { } catch (error) {
return error.message; return error.message;
}
} }
}
// liitytään factionii // liitytään factionii
async joinFaction(game: GameDTO, data: UserDTO): Promise<boolean> { async joinFaction(game: GameDTO, data: UserDTO): Promise<boolean> {
try { try {
// tarkistetaan factionin salasana // tarkistetaan factionin salasana
return true; return true;
} catch (error) { } catch (error) {
return error; return error;
}
} }
}
} }
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