Esempio n. 1
0
        public async Task <ResponseData <List <GetCharacterDTO> > > AddNewCharacter(AddCharacterDTO newChar)
        {
            ResponseData <List <GetCharacterDTO> > response = new ResponseData <List <GetCharacterDTO> >();
            character c = _imapper.Map <character>(newChar);

            c.user = await _context.Users.FirstOrDefaultAsync(u => u.id == getUserId());

            await _context.characters.AddAsync(c);

            await _context.SaveChangesAsync();

            response.Data = await _context.characters.Where(c => c.user.id == getUserId()).Select(c => _imapper.Map <GetCharacterDTO>(c)).ToListAsync();

            return(response);
        }
        public async Task <ResponseData <GetCharacterDTO> > AddWeapon(AddWeaponDTO newWeapon)
        {
            ResponseData <GetCharacterDTO> res = new ResponseData <GetCharacterDTO>();

            try
            {
                character character = _context.characters.FirstOrDefault(c => c.Id == newWeapon.characterId && c.user.id == int.Parse(httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)));
                if (character == null)
                {
                    res.Message = "charcater not found";
                    res.success = false;
                }
                else
                {
                    Weapon weapon = new Weapon
                    {
                        Name        = newWeapon.Name,
                        Damage      = newWeapon.Damage,
                        characterId = newWeapon.characterId
                    };

                    await _context.Weapons.AddAsync(weapon);

                    await _context.SaveChangesAsync();

                    res.Data = mapper.Map <GetCharacterDTO>(character);
                }
            }
            catch (Exception ex)
            {
                res.success = false;
                res.Message = ex.Message;
            }
            return(res);
        }
        public async Task <ResponseData <GetCharacterDTO> > AddCharacterSkill(characterskillDTO newcharacterskill)
        {
            ResponseData <GetCharacterDTO> res = new ResponseData <GetCharacterDTO>();

            try
            {
                character ch = _context.characters.Include(c => c.weapon)
                               .Include(c => c.characterSkills).ThenInclude(cs => cs.skill)
                               .FirstOrDefault(c => c.Id == newcharacterskill.characterId &&
                                               c.user.id == int.Parse(httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)));
                if (ch == null)
                {
                    res.success = false;
                    res.Message = "character not found";
                }
                Skill skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newcharacterskill.SkillId);

                if (skill == null)
                {
                    res.success = false;
                    res.Message = "skill not found";
                }
                characterSkill cs = new characterSkill()
                {
                    character = ch,
                    skill     = skill
                };

                await _context.characterSkills.AddAsync(cs);

                await _context.SaveChangesAsync();

                res.Data = _imapper.Map <GetCharacterDTO>(ch);
            }
            catch (Exception ex)
            {
                res.success = false;
                res.Message = ex.Message;
            }
            return(res);
        }