public async Task <ServiceResponse <GetCharacterDTO> > AddCharacterSkill(AddCharacterSkillDTO newCharacterSkill)
        {
            ServiceResponse <GetCharacterDTO> serviceResponse = new ServiceResponse <GetCharacterDTO>();

            try
            {
                Character character = await _dataContext.Characters
                                      .Include(x => x.Weapon)
                                      .Include(x => x.CharacterSkills).ThenInclude(x => x.Skill)
                                      .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)));

                if (character == null)
                {
                    serviceResponse.Success = false;
                    serviceResponse.Message = "Character Not Found";
                    return(serviceResponse);
                }
                Skill skill = await _dataContext.Skills.FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId);

                if (skill == null)
                {
                    serviceResponse.Success = false;
                    serviceResponse.Message = "Skill not found!";
                    return(serviceResponse);
                }

                CharacterSkill characterSkill = new CharacterSkill()
                {
                    Character = character,
                    Skill     = skill
                };

                await _dataContext.CharacterSkills.AddAsync(characterSkill);

                await _dataContext.SaveChangesAsync();

                serviceResponse.Data = _mapper.Map <GetCharacterDTO>(character);
            }
            catch (Exception ex)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = ex.Message;
            }
            return(serviceResponse);
        }
        public async Task <ServiceResponse <CharacterDTO> > AddCharacterSkill(AddCharacterSkillDTO newCharacterSkill)
        {
            ServiceResponse <CharacterDTO> response = new ServiceResponse <CharacterDTO>();

            try
            {
                Character character = await _context.Characters.Include(c => c.CharacterSkills).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();
            }

            catch (Exception ex)
            {
                response.Success = false;
                // response.Message = ex.message;
            }

            throw new System.NotImplementedException();
        }
예제 #3
0
        public async Task <ServiceResponse <GetCharacterDTO> > AddCharacterSkill(AddCharacterSkillDTO newSkill)
        {
            ServiceResponse <GetCharacterDTO> resp = new ServiceResponse <GetCharacterDTO>();

            try
            {
                Character c = await _context.Characters
                              .Include(c => c.Weapon)
                              .Include(c => c.CharacterSkills)
                              .ThenInclude(cs => cs.Skills)
                              .FirstOrDefaultAsync(c => c.Id == newSkill.CharacterId &&
                                                   c.User.Id == int.Parse(_httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)));

                if (c == null)
                {
                    throw new System.Exception("Character not found");
                }
                Skills s = await _context.Skills
                           .FirstOrDefaultAsync(s => s.Id == newSkill.SkillId);

                if (s == null)
                {
                    throw new System.Exception("Skill not found");
                }
                CharacterSkill cs = new CharacterSkill
                {
                    Character = c,
                    Skills    = s
                };
                await _context.CharacterSkills.AddAsync(cs);

                await _context.SaveChangesAsync();

                resp.Data = _mapper.Map <GetCharacterDTO>(c);
            }
            catch (System.Exception ex)
            {
                resp.Success = false;
                resp.Message = ex.Message;
            }
            return(resp);
        }
예제 #4
0
 public async Task <IActionResult> AddCharacterSkill(AddCharacterSkillDTO newCS)
 {
     return(Ok(await _csService.AddCharacterSkill(newCS)));
 }