public async Task <ActionResult <PlayerTypeDto> > PostPlayerType(PlayerTypeDto playerTypeDto) { if (await PlayerTypeExists(playerTypeDto.Id)) { return(Conflict()); } if (!await ValidatePlayerTypeDto(playerTypeDto)) { return(BadRequest()); } var playerType = _mapper.Map <PlayerType>(playerTypeDto); foreach (var availableSkillCategory in playerType.AvailableSkillCategories) { availableSkillCategory.Id = 0; } foreach (var availableSkill in playerType.StartingSkills) { availableSkill.Id = 0; } _context.PlayerType.Add(playerType); await _context.SaveChangesAsync(); var newPlayerType = await FindPlayerTypeDto(playerType.Id); return(CreatedAtAction("GetPlayerType", new { id = newPlayerType.Id }, newPlayerType)); }
public async Task <IActionResult> PutPlayerType(int id, PlayerTypeDto playerTypeDto) { if (!await PlayerTypeExists(id)) { return(NotFound()); } if (id != playerTypeDto.Id || !await ValidatePlayerTypeDto(playerTypeDto)) { return(BadRequest()); } var playerType = _mapper.Map <PlayerType>(playerTypeDto); _context.SetModified(playerType); var existingStaringSkills = await _context.StartingSkill.Where(c => c.PlayerTypeId == playerType.Id).ToListAsync(); // Delete Existing StaringSkill Records foreach (var existingStartingSkill in existingStaringSkills) { if (!playerType.StartingSkills.Exists(s => s.SkillId == existingStartingSkill.SkillId)) { _context.StartingSkill.Remove(existingStartingSkill); } } // Add Or Update mew StartingSkill Records foreach (var startingSkill in playerType.StartingSkills) { var existingStartingSkill = await _context.StartingSkill.FindAsync(startingSkill.Id); if (existingStartingSkill == null) { await _context.StartingSkill.AddAsync(existingStartingSkill); } else { existingStartingSkill.PlayerTypeId = playerType.Id; existingStartingSkill.SkillId = startingSkill.SkillId; _context.SetModified(existingStartingSkill); } } var existingAvailableSkillCategories = await _context.AvailableSkillCategory.Where(c => c.PlayerTypeId == playerType.Id).ToListAsync(); // Delete Existing AvailableSkill Records foreach (var existingAvailableSkillCategory in existingAvailableSkillCategories) { if (!playerType.AvailableSkillCategories.Exists(s => s.Id == existingAvailableSkillCategory.Id)) { _context.AvailableSkillCategory.Remove(existingAvailableSkillCategory); } } // Delete Existing AvailableSkill Records foreach (var availableSkillCatagory in playerType.AvailableSkillCategories) { var existingAvailableSkillCategory = await _context.AvailableSkillCategory.FindAsync(availableSkillCatagory.Id); if (existingAvailableSkillCategory == null) { await _context.AvailableSkillCategory.AddAsync(existingAvailableSkillCategory); } else { existingAvailableSkillCategory.PlayerTypeId = playerType.Id; existingAvailableSkillCategory.LevelUpTypeId = availableSkillCatagory.LevelUpTypeId; existingAvailableSkillCategory.SkillCategoryId = availableSkillCatagory.SkillCategoryId; _context.SetModified(existingAvailableSkillCategory); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!await PlayerTypeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private async Task <bool> ValidatePlayerTypeDto(PlayerTypeDto playerTypeDto) { return(ValidateAvailableSkillCategoryDtos(playerTypeDto.AvailableSkillCategories) && await ValidateSkillDtos(playerTypeDto.StartingSkills)); }