Esempio 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));
        }
Esempio n. 2
0
        public async Task <Attribute> CreateAttributeAsync(string name)
        {
            var attribute = new Attribute(name);

            await _context.AddAsync(attribute);

            await _context.SaveChangesAsync();

            return(attribute);
        }
Esempio n. 3
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();
            }
        }
Esempio n. 4
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();
        }