public void Execute(AddGameDto request) { if (context.Games.Any(g => g.Title == request.Title)) { throw new AlreadyExistsEx("Game with this title already exists"); } if (!context.Companies.Any(c => c.Id == request.CompanyId)) { throw new NotFoundEx(); } foreach (var genreId in request.GenreIds) { var genre = context.Genres.Find(genreId); if (genre == null) { throw new NotFoundEx(); } } foreach (var consoleId in request.ConsoleIds) { var console = context.Consoles.Find(consoleId); if (console == null) { throw new NotFoundEx(); } } var game = new Game { Title = request.Title, ReleseDate = request.ReleseDate, Picture = request.Picture, Company = context.Companies.Find(request.CompanyId) }; foreach (var genreId in request.GenreIds) { context.Game_Genres.Add(new Game_Genre { Game = game, Genre = context.Genres.Find(genreId) }); } foreach (var consoleId in request.ConsoleIds) { context.Game_Consoles.Add(new Game_Console { Game = game, Console = context.Consoles.Find(consoleId) }); } context.SaveChanges(); }
public async Task AddAsync(AddGameDto addGame, CancellationToken ct = default) { _logger.LogInformation("Add game {@Game}", addGame); Game game = new Game(addGame.Id, addGame.GenreId); _unitOfWork.GameRepository.Create(game); await _unitOfWork.SaveAsync(ct); }
public IActionResult Post([FromBody] AddGameDto request) { try { createGame.Execute(request); return(StatusCode(201)); } catch (AlreadyExistsEx ex) { return(StatusCode(409, ex.Message)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public void Execute(AddGameDto req) { if (Context.Games.Any(g => g.Name == req.Name)) { throw new GameExistsException(); } if (!Context.Companies.Any(c => c.Id == req.CompanyId)) { throw new CompanyDoesntExistException(); } if (!Context.Categories.Any(c => c.Id == req.CategoryId)) { throw new CategoryDoesntExistException(); } Context.Games.Add(new Game { Name = req.Name, Description = req.Description, CategoryId = req.CategoryId, CompanyId = req.CompanyId }); Context.SaveChanges(); }
public IActionResult Post([FromBody] AddGameDto dto) { try { _addGame.Execute(dto); return(StatusCode(201)); } catch (GameExistsException) { return(StatusCode(409, "Game already exists")); } catch (CompanyDoesntExistException) { return(StatusCode(404, "Company doesn't exist")); } catch (CategoryDoesntExistException) { return(StatusCode(404, "Category doesn's exist")); } catch (Exception) { return(StatusCode(500, "Server error, try again later")); } }
protected override async Task <GetGameDto> ExecuteAsync(CreateGameCommand request, CancellationToken ct) { AddGameDto accountDto = _mapper.Map <AddGameDto>(request); return(await _gameService.AddAsync(accountDto, ct)); }
protected override async Task ExecuteAsync(CreateGameCommand request, CancellationToken ct) { AddGameDto game = _mapper.Map <AddGameDto>(request); await _gameService.AddAsync(game, ct); }