예제 #1
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWapon(AddWaponDto wapon)
        {
            ServiceResponse <GetCharacterDto> serviceResponse = new ServiceResponse <GetCharacterDto>();

            try
            {
                Characters character = await _context.Characterss
                                       .FirstOrDefaultAsync(c => c.Id == wapon.CharacterId &&
                                                            c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)));

                if (character == null)
                {
                    serviceResponse.isSuccess = true;
                    serviceResponse.message   = "The character not found";
                    return(serviceResponse);
                }
                else
                {
                    Wapon newWapon = new Wapon();
                    newWapon.Name      = wapon.Name;
                    newWapon.Damage    = wapon.Damage;
                    newWapon.Character = character;

                    await _context.Wapons.AddAsync(newWapon);

                    await _context.SaveChangesAsync();

                    serviceResponse.Data      = _mapper.Map <GetCharacterDto>(character);
                    serviceResponse.isSuccess = true;
                    serviceResponse.message   = "Wapon added to the character";
                }
            }
            catch (Exception Ex)
            {
                serviceResponse.isSuccess = false;
                serviceResponse.message   = Ex.Message;
            }

            return(serviceResponse);
        }
예제 #2
0
 public async Task <IActionResult> AddWapon(AddWaponDto newWapon)
 {
     return(Ok(await _waponService.AddWapon(newWapon)));
 }