Exemplo n.º 1
0
        private async Task <Character> CreateCharacterAsync(Character character)
        {
            var charDto = _mapper.Map <CharacterDto>(character);

            // Check the database for a character with the same ID as the new one
            // If one exists, don't add it
            if (await _context.Characters.CountAsync(x => x.Id.Equals(charDto.Id)) > 0)
            {
                return(null);
            }

            await _context.AddAsync(charDto);

            var user = await _context.Users.FirstOrDefaultAsync(x => x.UserIdentifier == character.UserIdentifier);

            if (user == null)
            {
                await _context.AddAsync(new User { UserIdentifier = character.UserIdentifier, ActiveCharacter = charDto });
            }
            else if (character.Active)
            {
                user.ActiveCharacter = charDto;
            }

            await _context.SaveChangesAsync();

            return(_mapper.Map <Character>(charDto));
        }
Exemplo n.º 2
0
        public async Task <Attribute> CreateAttributeAsync(string name)
        {
            var attribute = new Attribute(name);

            await _context.AddAsync(attribute);

            await _context.SaveChangesAsync();

            return(attribute);
        }
Exemplo n.º 3
0
        public async Task <Effect> CreateEffectAsync(ulong ownerId, string name)
        {
            var effect = new Effect(ownerId, name);
            var dto    = _mapper.Map <EffectDto>(effect);

            await _context.Effects.AddAsync(dto);

            await _context.SaveChangesAsync();

            return(effect);
        }
Exemplo n.º 4
0
        public async Task <ServiceResponse <GetCharacterDto> > AddCharacter(AddCharacterDto newCharacter)
        {
            var response  = new ServiceResponse <GetCharacterDto>();
            var character = _mapper.Map <Character>(newCharacter);

            await _context.Character.AddAsync(character);

            await _context.SaveChangesAsync();

            var inserted = await _context.Character.FirstOrDefaultAsync(c => c.Id == character.Id);

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

            return(response);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds an entity of type T to the database
        /// asynchronously.
        /// </summary>
        public async Task AddAsync(T entity)
        {
            await _semaphore.WaitAsync();

            try
            {
                await _context.AddAsync(entity);

                await _context.SaveChangesAsync();
            }
            finally
            {
                _semaphore.Release();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds an entity of type T to the database
        /// asynchronously.
        /// </summary>
        public async Task AddAsync(T entity)
        {
            await _context.AddAsync(entity);

            await _context.SaveChangesAsync();
        }