コード例 #1
0
        public async Task <ServiceResponse <GetCharecterDto> > AddWeapons(AddWeaponDto addWeapon)
        {
            ServiceResponse <GetCharecterDto> response = new ServiceResponse <GetCharecterDto>();

            try
            {
                int       userid = GetUserID();
                Charecter chars  = await _context.charecters.
                                   FirstOrDefaultAsync(x => x.Id == addWeapon.CharecterId && x.Users.Id == GetUserID());

                if (chars == null)
                {
                    response.Success = false;
                    response.Message = "Charecter not found";
                    return(response);
                }
                Weapon weapon = new Weapon {
                    Name      = addWeapon.Name,
                    Damage    = addWeapon.Damage,
                    charecter = chars
                };
                await _context.Weapons.AddAsync(weapon);

                await _context.SaveChangesAsync();

                response.data = _mapper.Map <GetCharecterDto>(chars);
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = e.Message;
            }
            return(response);
        }
コード例 #2
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            var serviceResponse = new ServiceResponse <GetCharacterDto>();

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

                if (character == null)
                {
                    serviceResponse.Success = false;
                    serviceResponse.Message = "Character not found";
                    return(serviceResponse);
                }

                await _dataContext.Weapons.AddAsync(new Weapon
                {
                    Name      = newWeapon.Name,
                    Damage    = newWeapon.Damage,
                    Character = character
                });

                await _dataContext.SaveChangesAsync();

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

            return(serviceResponse);
        }
コード例 #3
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

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

                if (character == null)
                {
                    response.Success = false;
                    response.Message = "Character doesn't exist";
                    return(response);
                }
                Weapon weapon = _mapper.Map <Weapon>(newWeapon);
                weapon.Character = character;
                await _context.Weapons.AddAsync(weapon);

                await _context.SaveChangesAsync();

                //return response
                response.Message = "Successfully added";
                response.Data    = _mapper.Map <GetCharacterDto>(character);
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = e.Message;
            }

            return(response);
        }
コード例 #4
0
ファイル: WeaponService.cs プロジェクト: istorp/anders3
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

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

                if (character == null)
                {
                    response.Success = false;
                    response.Message = "Character was 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);
        }
コード例 #5
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            try
            {
                Character character = await _characterService.GetCharacterById(newWeapon.CharacterId, AuthType.WithAuth);

                Weapon weapon = _mapper.Map <Weapon>(newWeapon);
                await _context.Weapons.AddAsync(weapon);

                await _context.SaveChangesAsync();

                return(new ServiceResponse <GetCharacterDto>
                {
                    Data = _mapper.Map <GetCharacterDto>(character)
                });
            }
            catch (Exception e)
            {
                return(new ServiceResponse <GetCharacterDto>
                {
                    Success = false,
                    Message = e.Message,
                });
            }
        }
コード例 #6
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            var serviceResponse = new ServiceResponse <GetCharacterDto>();

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

                if (character != null)
                {
                    var weapon = _mapper.Map <Weapon>(newWeapon);

                    weapon.Character = character;
                    await _context.Weapons.AddAsync(weapon);

                    await _context.SaveChangesAsync();

                    serviceResponse.Data = _mapper.Map <GetCharacterDto>(character);
                }
                else
                {
                    serviceResponse.Message = "no such character";
                    serviceResponse.Success = false;
                }
            }
            catch (Exception e)
            {
                serviceResponse.Message = e.Message;
                serviceResponse.Success = false;
            }

            return(serviceResponse);
        }
コード例 #7
0
        public async Task <IActionResult> AddWeapon(AddWeaponDto newWeapon)
        {
            var response = await _service.AddWeapon(newWeapon);

            if (!response.Success)
            {
                return(NotFound(response));
            }

            return(Ok(response));
        }
コード例 #8
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

            try
            {
                //Now for the try block, we first get the correct Character from the database.
                //We access the Characters from the _context, find the first entity with the given CharacterId and also the correct User so that we know this character really belongs to the currently authorized user.


                //Just to recap, we get the Id of the current user by accessing the NameIdentifier claims value from the JSON web token.
                Character character = await _context.characters
                                      .FirstOrDefaultAsync(c => c.Id == newWeapon.CharacterId &&
                                                           c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)));



                //When the character is null, something is wrong and we return a failing response.
                if (character == null)
                {
                    response.Success = false;
                    response.Message = "Character not found.";
                    return(response);
                }


                //if we got the proper character, we can create a new Weapon instance, with the given Name and Damage value and also set the Character property of this new Weapon instance to the character object we got from the database.
                //By the way, we could have added a new mapping from the AddWeaponDto to the Weapon type, or we just set these two properties manually here
                Weapon weapon = new Weapon
                {
                    Name      = newWeapon.Name,
                    Damage    = newWeapon.Damage,
                    Character = character
                };



                //After that, we add this new weapon to the database, save the changes and return the 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);
        }
コード例 #9
0
ファイル: WeaponService.cs プロジェクト: jjad14/rest-rpg-api
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            // wrapper
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

            try
            {
                // find character where the character id is equal to the newwepon character id
                // and where the user id of that character is equal to the userid in the token
                Character character = await _context.Characters
                                      .FirstOrDefaultAsync(u => u.Id == newWeapon.CharacterId &&
                                                           u.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)));

                // if no user is found
                if (character == null)
                {
                    response.Success = false;
                    response.Message = "Character not found.";
                    return(response);
                }

                // Create weapon obj
                Weapon weapon = new Weapon
                {
                    Name      = newWeapon.Name,
                    Damage    = newWeapon.Damage,
                    Character = character
                };

                // add weapon to db and save changes
                await _context.Weapons.AddAsync(weapon);

                await _context.SaveChangesAsync();

                // map character to GetCharacterDto
                response.Data = _mapper.Map <GetCharacterDto>(character);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = "Weapon could not be added: " + ex.Message;
            }

            return(response);
        }
コード例 #10
0
        async public Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

            try
            {
                Character character = await _dataContext.Characters. //.Include(c => c.User).
                                      FirstOrDefaultAsync(c => c.User.Id == GetUserId() && c.Id == newWeapon.CharacterId);

                if (character == null)
                {
                    response.Success = false;
                    response.Message = "Character not found";
                    response.Data    = _mapper.Map <GetCharacterDto>(character);

                    return(response);
                }

                // Weapon weapon = _mapper.Map<Weapon>(newWeapon);//throw error
                Weapon weapon = new Weapon
                {
                    Name      = newWeapon.Name,
                    Damage    = newWeapon.Damage,
                    Character = character
                };
                await _dataContext.Weapons.AddAsync(weapon);

                await _dataContext.SaveChangesAsync();

                response.Success = true;
                response.Message = "Weapon added succesfuly.";
                response.Data    = _mapper.Map <GetCharacterDto>(character);
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = e.Message;
            }

            return(response);
        }
コード例 #11
0
        public async Task <ServiceResponse <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon)
        {
            var character = await _dBContext.Characters.Include(x => x.Weapon).FirstOrDefaultAsync(x => x.Id == newWeapon.CharacterId);

            if (character == null)
            {
                return(ResponseResult.Failure <GetCharacterDto>("Character not found"));
            }

            var weapon = new Weapon
            {
                Name        = newWeapon.Name,
                Damage      = newWeapon.Damage,
                CharacterId = newWeapon.CharacterId
            };

            _dBContext.Weapons.Add(weapon);
            await _dBContext.SaveChangesAsync();

            var dto = _mapper.Map <GetCharacterDto>(character);

            return(ResponseResult.Success(dto));
        }
コード例 #12
0
        public async Task <ServiceResponse <GetCharacterDto> > UpdateWeapon(AddWeaponDto update)
        {
            ServiceResponse <GetCharacterDto> response = new ServiceResponse <GetCharacterDto>();

            try
            {
                Character character = await _dataContext.Characters
                                      .Include(c => c.Weapon)
                                      .FirstOrDefaultAsync(c => c.User.Id == GetUserId() && c.Id == update.CharacterId);

                if (character == null || character.Weapon == null)
                {
                    response.Success = false;
                    response.Message = "Character not found";

                    return(response);
                }


                character.Weapon.Name   = update.Name;
                character.Weapon.Damage = update.Damage;

                _dataContext.Characters.Update(character);
                await _dataContext.SaveChangesAsync();

                response.Success = true;
                response.Message = "Weapon updated succesfuly.";
                response.Data    = _mapper.Map <GetCharacterDto>(character);
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = e.Message;
            }

            return(response);
        }
コード例 #13
0
 public async Task <IActionResult> AddWeapon(AddWeaponDto newWeapon)
 {
     return(Ok(await _charService.AddWeapon(newWeapon)));
 }
コード例 #14
0
ファイル: WeaponController.cs プロジェクト: DonLuc/dotnet-rpg
 public async Task <IActionResult> AddWeapon(AddWeaponDto newWeapon) => Ok(await _weaponService.AddWeapon(newWeapon));
コード例 #15
0
 public async Task <IActionResult> UpdateWeapon(AddWeaponDto update)
 {
     return(Ok(await _weaponService.UpdateWeapon(update)));
 }
コード例 #16
0
 public async Task <IActionResult> AddWeapon(AddWeaponDto addWeapon)
 {
     return(Ok(await _weaponServices.AddWeapons(addWeapon)));
 }
コード例 #17
0
ファイル: WeaponController.cs プロジェクト: Shar1s/dotnet-rpg
 public async Task <ActionResult <ServiceResponse <GetCharacterDto> > > AddWeapon(AddWeaponDto newWeapon)
 {
     return(Ok(await _weaponService.AddWeapon(newWeapon)));
 }
コード例 #18
0
 public async Task <IActionResult> AddWeapon(AddWeaponDto newWeapon)
 {
     return(Ok(await _weapons.AddWeapon(newWeapon)));
 }