/// <summary> /// get specific game from db /// </summary> /// <param name="id"></param> /// <returns>returns selected game</returns> public async Task <GamesDTO> GetGame(int id) { var games = await _context.Games .Where(x => x.Id == id) .Include(x => x.GameGuilds) .ThenInclude(x => x.Guild) .FirstOrDefaultAsync(); List <GuildsDTO> guilds = new List <GuildsDTO>(); foreach (GameGuilds gameGuild in games.GameGuilds) { guilds.Add(new GuildsDTO() { Id = gameGuild.GuildId, Name = gameGuild.Guild.Name }); } GamesDTO gamesDTO = new GamesDTO() { Id = games.Id, Name = games.Name, Guilds = guilds }; foreach (var guild in games.GameGuilds) { guild.Game = null; } return(gamesDTO); }
public async void CanGetGameFromDatabase() { DbContextOptions <GuildAPIDbContext> options = new DbContextOptionsBuilder <GuildAPIDbContext>() .UseInMemoryDatabase("CanGetGameFromDatabase") .Options; using GuildAPIDbContext context = new GuildAPIDbContext(options); GamesService service = new GamesService(context); GamesDTO dto = new GamesDTO() { Name = "Bryants Deluxe Game Edition" }; Assert.Equal(0, context.Games.CountAsync().Result); var result = await service.Create(dto); Assert.Equal(1, context.Games.CountAsync().Result); var get = service.GetGame(result.Id); Assert.Equal(1, get.Result.Id); Assert.IsType <GamesDTO>(get.Result); Assert.Equal("Bryants Deluxe Game Edition", get.Result.Name); // Assert.IsType<Games>(actual); }
//O método de inserir recebe os dados via DTO //e envia para o banco de dados através da classe AcessoBancoDados public void Inserir(GamesDTO medDto) { //Antes de criar o comando aqui, teste no PhpMyAdmin ou Worckbench string inserir = $"insert into {tabela} values(null,'{medDto.Games}','{medDto.Perifericos}', '{medDto.Videogames}')"; conexao.ExecutarComando(inserir); }
public void CanGetGuildFromDatabase() { DbContextOptions <GuildAPIDbContext> options = new DbContextOptionsBuilder <GuildAPIDbContext>() .UseInMemoryDatabase("CanGetGuildFromDatabase") .Options; using GuildAPIDbContext context = new GuildAPIDbContext(options); GamesService gameService = new GamesService(context); GamesDTO gamesDTO = new GamesDTO() { Name = "Odins Game" }; var createdGame = gameService.Create(gamesDTO); GuildService guildService = new GuildService(context, gameService); Assert.Equal(1, context.Games.CountAsync().Result); GuildsDTO guild = new GuildsDTO() { Name = "Odin Slayers" }; var creation = guildService.Create(guild); var association = gameService.AddGameGuild(createdGame.Result.Id, creation.Result.Id); var actual = guildService.GetGuild(creation.Result.Id).Result; Assert.Equal(1, context.Guilds.CountAsync().Result); Assert.IsType <GuildsDTO>(actual); Assert.Equal(1, actual.Id); Assert.Equal("Odin Slayers", actual.Name); }
public async void CanCreateUpdateAndSaveToDatabase() { DbContextOptions <GuildAPIDbContext> options = new DbContextOptionsBuilder <GuildAPIDbContext>() .UseInMemoryDatabase("CanCreateUpdateAndSaveToDatabase") .Options; using GuildAPIDbContext context = new GuildAPIDbContext(options); GamesService service = new GamesService(context); GamesDTO dto = new GamesDTO() { Name = "Bryants Deluxe Game Edition" }; Assert.Equal(0, context.Games.CountAsync().Result); var result = await service.Create(dto); GamesDTO gamesDTO = new GamesDTO() { Id = 1, Name = "Legends of Lesley" }; var update = service.Update(gamesDTO); var actual = context.Games.FindAsync(update.Result.Id).Result; Assert.Equal(1, context.Games.CountAsync().Result); // Assert.IsType<Games>(actual); Assert.Equal(1, update.Id); Assert.Equal("Legends of Lesley", actual.Name); }
public void CanDeleteGuildAndSaveToDatabase() { DbContextOptions <GuildAPIDbContext> options = new DbContextOptionsBuilder <GuildAPIDbContext>() .UseInMemoryDatabase("CanDeleteGuildAndSaveToDatabase") .Options; using GuildAPIDbContext context = new GuildAPIDbContext(options); GamesService gameService = new GamesService(context); GamesDTO gamesDTO = new GamesDTO() { Name = "Odins Game" }; var createdGame = gameService.Create(gamesDTO); GuildService guildService = new GuildService(context, gameService); Assert.Equal(1, context.Games.CountAsync().Result); GuildsDTO guild = new GuildsDTO() { Name = "Odin Slayers" }; var creation = guildService.Create(guild); var association = gameService.AddGameGuild(createdGame.Result.Id, creation.Result.Id); Assert.Equal(1, context.Guilds.CountAsync().Result); var delete = guildService.Delete(creation.Result.Id); Assert.Equal(0, context.Guilds.CountAsync().Result); }
public ActionResult GameForms(GamesDTO gamesList, string request, int[] formGenres) { try { var game = gamesList.SelectedGame; switch (request) { case ("Create"): storage.CreateGame(game, formGenres); break; case ("Edit"): storage.UpdateGame(game, formGenres); break; case ("Delete"): storage.DeleteGame(game, formGenres); break; default: throw new Exception("Akcja przycisku niezdefiniowana!"); } return(RedirectToAction("Index", new { page = gamesList.Page })); } catch (Exception ex) { return(RedirectToAction("Index", new { page = 1, errorMessage = ex.Message })); } }
public void GetterForGame() { GamesDTO gamesDTO = new GamesDTO(); gamesDTO.Name = "Bade"; gamesDTO.Id = 1; Assert.Equal("Bade", gamesDTO.Name); Assert.Equal(1, gamesDTO.Id); }
/// <summary> /// updates games db /// </summary> /// <param name="game"></param> /// <returns>successfully updated db</returns> public async Task <GamesDTO> Update(GamesDTO gamesDTO) { var games = await _context.Games.FindAsync(gamesDTO.Id); games.Name = gamesDTO.Name; _context.Entry(games).State = EntityState.Modified; await _context.SaveChangesAsync(); return(gamesDTO); }
public async Task <IActionResult> PutGames(int id, GamesDTO games) { if (id != games.Id) { return(BadRequest()); } await _games.Update(games); return(Ok()); }
/// <summary> /// adds to the game db /// </summary> /// <param name="games"></param> /// <returns>returns created game</returns> public async Task <GamesDTO> Create(GamesDTO gamesDTO) { Games games = new Games() { Name = gamesDTO.Name }; _context.Entry(games).State = EntityState.Added; await _context.SaveChangesAsync(); gamesDTO.Id = games.Id; return(gamesDTO); }
public void Excluir(GamesDTO medDto) { string excluir = $"delete from {tabela} where id = '{medDto.Id}';"; conexao.ExecutarComando(excluir); }
public void Editar(GamesDTO medDto) { string editar = $"update {tabela} set games = '{medDto.Games}', perifericos = '{medDto.Perifericos}', videogames = '{medDto.Videogames}' where id = '{medDto.Id}';"; conexao.ExecutarComando(editar); }
public async Task <ActionResult <GamesDTO> > PostGames(GamesDTO games) { await _games.Create(games); return(CreatedAtAction("GetGames", new { id = games.Id }, games)); }