Beispiel #1
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

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

                if (character == null)
                {
                    response.Success = false;
                    response.Message = "Character not found.";
                    return(response);
                }
                Weapon weapon = new Weapon {
                    Name      = newWeapon.Name,
                    Damage    = newWeapon.Damage,
                    Character = character
                };
                await _context.Weapons.AddAsync(weapon);

                await _context.SaveChangesAsync();

                response.Data = _mapper.Map <GetCharacterDto>(character);
            }catch (Exception ex) {
                response.Success = false;
                response.Message = ex.Message;
            }
            return(response);
        }
Beispiel #2
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            var response = new ServiceResponse <GetCharacterDto>();

            var character = await _context.Characters
                            .FirstOrDefaultAsync(c => c.Id == newWeapon.CharacterId && c.User.Id == GetSecurityUserId());

            if (character == null)
            {
                response.Success = false;
                response.Message = "Cannot Add weapon.";

                return(response);
            }

            //create a new instace of weapon with mapper
            var weapon = _mapper.Map <Weapon>(newWeapon);

            weapon.Character = character; //TODO assign with automapper

            await _context.AddAsync(weapon);

            await _context.SaveChangesAsync();

            response.Data = _mapper.Map <GetCharacterDto>(character);

            return(response);
        }
Beispiel #3
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

            try
            {
                Character dbCharacter = await this._context.Characters.Include(c => c.Weapon)
                                        .FirstOrDefaultAsync(c => c.Id == newWeapon.CharacterId && c.User.Id == this.GetUserId());

                if (dbCharacter == null)
                {
                    response.Success = false;
                    response.Message = "Character not found.";
                    return(response);
                }
                Weapon weapon = new Weapon
                {
                    Name      = newWeapon.Name,
                    Damage    = newWeapon.Damage,
                    Character = dbCharacter
                };
                await this._context.Weapons.AddAsync(weapon);

                await this._context.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> > AddWeapon(AddWeaponDto newWeapon)
        {
            ServiceResponse <GetCharacterDto> serviceResponse = new ServiceResponse <GetCharacterDto>();

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

                if (character == null)
                {
                    serviceResponse.Success = false;
                    serviceResponse.Message = "Character not found.";
                }
                else
                {
                    Weapon weapon = _mapper.Map <Weapon>(newWeapon);
                    await _dataContext.Weapons.AddAsync(weapon);

                    await _dataContext.SaveChangesAsync();

                    serviceResponse.Data = _mapper.Map <GetCharacterDto>(character);
                }
            }
            catch (Exception ex)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = ex.Message;
            }
            return(serviceResponse);
        }