public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newSkill) { var character = await _dBContext.Characters.Include(x => x.Weapon).Include(x => x.CharacterSkills).ThenInclude(x => x.Skill).FirstOrDefaultAsync(x => x.Id == newSkill.CharacterId); if (character == null) { return(ResponseResult.Failure <GetCharacterDto>("Character not found")); } var skill = await _dBContext.Skills.FirstOrDefaultAsync(x => x.Id == newSkill.SkillId); if (skill == null) { return(ResponseResult.Failure <GetCharacterDto>("Skill not found")); } var c_Skill = new CharacterSkill(); c_Skill.CharacterId = newSkill.CharacterId; c_Skill.SkillId = newSkill.SkillId; _dBContext.CharacterSkills.Add(c_Skill); await _dBContext.SaveChangesAsync(); var dto = _mapper.Map <GetCharacterDto>(character); return(ResponseResult.Success(dto)); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { // wrapper ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>(); try { // find character but first include the weapon, characterSkills and Skill entities // find character where id is equal to characterSkill id // and where character user id is equal to id found in token Character character = await _context.Characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills) .ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); // if no character is found if (character == null) { response.Success = false; response.Message = "Character not found"; return(response); } // find skill where id is equal to new characterskill skill id Skill skill = await _context.Skills .FirstOrDefaultAsync(S => S.Id == newCharacterSkill.SkillId); // if no skill is found if (skill == null) { response.Success = false; response.Message = "Skill not found"; return(response); } // create characterSkill obj CharacterSkill characterSkill = new CharacterSkill { Character = character, Skill = skill }; // add characterskill to db and save changes await _context.CharacterSkills.AddAsync(characterSkill); await _context.SaveChangesAsync(); // map character to GetCharacterDto response.Data = _mapper.Map <GetCharacterDto>(character); } catch (Exception ex) { response.Success = false; response.Message = "Unable to add Character Skill: " + ex.Message; } return(response); }
public async Task <IActionResult> AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { ServiceResponse <GetCharacterDto> response = await _characterSkillService.AddCharacterSkill(newCharacterSkill); if (!response.Success) { return(BadRequest(response)); } return(Ok(response)); }
public async Task <IActionResult> AddCharacterSkill(AddCharacterSkillDto characterSkillDto) { var response = await _service.AddCharacterSkill(characterSkillDto); if (!response.Success) { return(NotFound(response)); } return(Ok(response)); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>(); try { Character character = await _context.Characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue( ClaimTypes.NameIdentifier))); if (character == null) { response.Success = false; response.Message = "Character not found"; return(response); } Skill skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId); if (skill == null) { response.Success = false; response.Message = "Skill not found"; return(response); } CharacterSkill characterSkill = new CharacterSkill { Character = character, Skill = skill }; await _context.AddAsync(characterSkill); await _context.SaveChangesAsync(); response.Data = _mapper.Map <GetCharacterDto>(character); } catch (Exception e) { response.Success = false; response.Message = e.Message; } return(response); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { var response = new ServiceResponse <GetCharacterDto>(); try { var character = await _context.Characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == GetSecurityUserId()); if (character == null) { response.Success = false; response.Message = "character not found"; return(response); } var skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId); if (skill == null) { response.Success = false; response.Message = "skill not found"; return(response); } var characterSkill = new CharacterSkill { Character = character, Skill = skill }; await _context.CharacterSkills.AddAsync(characterSkill); await _context.SaveChangesAsync(); response.Data = _mapper.Map <GetCharacterDto>(character); } catch (Exception ex) { response.Success = false; response.Message = $"Cannot Add character Skill: {ex}"; } return(response); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newSkill) { try { _log.LogInformation("Start Add CharacterSkill process."); var character = await _dBContext.Characters .Include(x => x.Weapon) .Include(x => x.CharacterSkill).ThenInclude(x => x.Skill) .FirstOrDefaultAsync(x => x.Id == newSkill.CharacterId); if (character == null) { _log.LogError("Character not found."); return(ResponseResult.Failure <GetCharacterDto>("Character not found.")); } _log.LogInformation("Character found."); var skill = await _dBContext.Skills.FirstOrDefaultAsync(x => x.Id == newSkill.SkillId); if (skill == null) { _log.LogError("Skill not found."); return(ResponseResult.Failure <GetCharacterDto>("Skill not found.")); } _log.LogInformation("Skill found."); var characterSkill = new CharacterSkill { Character = character, Skill = skill }; _dBContext.CharacterSkills.Add(characterSkill); await _dBContext.SaveChangesAsync(); _log.LogInformation("Success."); var dto = _mapper.Map <GetCharacterDto>(character); _log.LogInformation("End."); return(ResponseResult.Success(dto)); } catch (System.Exception ex) { _log.LogError(ex.Message); return(ResponseResult.Failure <GetCharacterDto>(ex.Message)); } }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>(); try{ //to recieve all skills and all related weapon of the user we need to include them //we can start with the weapon, then with CharacterSkill JOINED Table .thenInclude //all the skills for the new character Character character = await _context.Characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); if (character == null) { response.Success = false; response.Message = "Character not found"; return(response); } Skill skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId); if (skill == null) { response.Success = false; response.Message = "Skill not found"; return(response); } CharacterSkill characterSkill = new CharacterSkill { Character = character, Skill = skill }; await _context.CharacterSkills.AddAsync(characterSkill); await _context.SaveChangesAsync(); response.Data = _mapper.Map <GetCharacterDto>(character); }catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>(); try { Character dbCharacter = await this._dataContext.Characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == this.GetUserId()); if (dbCharacter == null) { response.Success = false; response.Message = "Character not found."; return(response); } Skill dbSkill = await this._dataContext.Skills .FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId); if (dbSkill == null) { response.Success = false; response.Message = "Skill not found."; return(response); } CharacterSkill characterSkill = new CharacterSkill { Character = dbCharacter, Skill = dbSkill }; await this._dataContext.CharacterSkills.AddAsync(characterSkill); await this._dataContext.SaveChangesAsync(); response.Data = this._mapper.Map <GetCharacterDto>(dbCharacter); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkillDto) { ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto> (); try { var character = await _context.Characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills) .ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkillDto.CharacterId && c.UserId == _GetUserId()); if (character == null) { response.Success = false; response.Message = $"Character with id {newCharacterSkillDto.CharacterId} not found!"; return(response); } Skill skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newCharacterSkillDto.SkillId); if (skill == null) { response.Success = false; response.Message = $"Skill with id {newCharacterSkillDto.SkillId} not found!"; return(response); } CharacterSkill charSkill = _mapper.Map <CharacterSkill> (newCharacterSkillDto); charSkill.Character = character; charSkill.Skill = skill; await _context.CharacterSkills.AddAsync(charSkill); await _context.SaveChangesAsync(); response.Data = _mapper.Map <GetCharacterDto> (character); } catch (System.Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto characterSkill) { var serviceResponse = new ServiceResponse <GetCharacterDto>(); try { var character = await _context.Characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == characterSkill.CharacterId && c.User.Id == GetUserId()); var skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == characterSkill.SkillId); if (character != null && skill != null) { await _context.CharacterSkills.AddAsync(new CharacterSkill { CharacterId = characterSkill.CharacterId, SkillId = characterSkill.SkillId, Character = character, Skill = skill }); await _context.SaveChangesAsync(); serviceResponse.Data = _mapper.Map <GetCharacterDto>(character); } else { serviceResponse.Message = "incorrect relation"; serviceResponse.Success = false; } } catch (Exception e) { serviceResponse.Message = e.Message; serviceResponse.Success = false; } return(serviceResponse); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { var response = new ServiceResponse <GetCharacterDto>(); try { var character = await _context.Characters .Include(c => c.Weapon) .Include(c => c.Skills) .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == GetUserId()); if (character == null) { response.Success = false; response.Message = "Character not found."; return(response); } var skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId); if (skill == null) { response.Success = false; response.Message = "Skill not found."; return(response); } character.Skills.Add(skill); await _context.SaveChangesAsync(); response.Data = _mapper.Map <GetCharacterDto>(character); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { try { Character character = await _characterService.GetCharacterById(newCharacterSkill.CharacterId, AuthType.WithAuth); if (character == null) { throw new Exception("Character not found!"); } Skill skill = await _context.Skills.FirstAsync(sk => sk.Id == newCharacterSkill.SkillId); CharacterSkill characterSkill = new CharacterSkill { Character = character, Skill = skill }; await _context.CharacterSkills.AddAsync(characterSkill); await _context.SaveChangesAsync(); return(new ServiceResponse <GetCharacterDto> { Data = _mapper.Map <GetCharacterDto>(character) }); } catch (Exception e) { return(new ServiceResponse <GetCharacterDto> { Success = false, Message = e.Message }); } }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>(); try { //Character character = await _context.characters // .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && // c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); //To receive all skills and also the related Weapon of the user, we have to include them. //We can start with the Weapon. After _context.Characters we add .Include(c => c.Weapon). //The skills are getting a bit more interesting. //Again we add .Include(), but first we access the CharacterSkills and after that we access the child property Skill of the CharacterSkills with .ThenInclude(). // That way, we get every property from the character that is stored in the database. Character character = await _context.characters .Include(c => c.Weapon) .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); // we add the usual null-check. So, if the character is null we set the Success state and the Message and return the response. if (character == null) { response.Success = false; response.Message = "Character not found."; return(response); } //Similar to the character, if we cannot find the skill with the given SkillId, we set the ServiceResponse and return it. Skill skill = await _context.Skills .FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId); if (skill == null) { response.Success = false; response.Message = "Skill not found."; return(response); } CharacterSkill characterSkill = new CharacterSkill { Character = character, Skill = skill }; await _context.CharacterSkills.AddAsync(characterSkill); await _context.SaveChangesAsync(); response.Data = _mapper.Map <GetCharacterDto>(character); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <IActionResult> AddAcharacterSkill(AddCharacterSkillDto addCharacterSkillDto) { return(Ok(await _characterSkillService.AddCharacterSkill(addCharacterSkillDto))); }
public async Task <ServiceResponse <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { var serviceResponse = new ServiceResponse <GetCharacterDto>(); return(serviceResponse); }
public async Task <IActionResult> AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) => Ok(await _characterSkillService.AddCharacterSkill(newCharacterSkill));
public async Task <IActionResult> AddCharacterSkill(AddCharacterSkillDto newCharSkill) { return(Ok(await _charService.AddCharacterSkill(newCharSkill))); }
public async Task <ActionResult <ServiceResponse <GetCharacterDto> > > AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) { return(Ok(await _characterService.AddCharacterSkill(newCharacterSkill))); }