Ejemplo n.º 1
0
        public async Task <Translation> InsertAsync(Translation translation)
        {
            var userId = await _context.UserDictionaries.Where(ud => ud.Type == UserType.owner &&
                                                               ud.DictionaryId == translation.DictionaryId)
                         .Select(ud => ud.UserId)
                         .SingleOrDefaultAsync();

            if (_authorizationManager.AuthorizeByUserId(userId, Token))
            {
                var inserted = _context.Add(translation).Entity;

                try
                {
                    await _context.SaveChangesAsync();

                    inserted = await GetByIdAsync(inserted.Id);

                    return(inserted);
                }
                catch (DbUpdateException e)
                {
                    throw new EntityInsertException(typeof(Translation), e.Message);
                }
            }
            else
            {
                throw new AuthorizationException(typeof(Translation));
            }
        }
Ejemplo n.º 2
0
        public async Task <Dictionary> InsertAsync(Dictionary dictionary)
        {
            var userId = dictionary.UserDictionaries.Where(ud => ud.Type == UserType.owner)
                         .Select(ud => ud.User.Id)
                         .SingleOrDefault();

            if (_authorizationManager.AuthorizeByUserId(userId, Token))
            {
                foreach (var ud in dictionary.UserDictionaries)
                {
                    ud.User = await _userManager.FindByIdAsync(ud.User.Id.ToString()) ?? throw new EntityNotFoundException(typeof(User));
                }

                var inserted = _context.Add(dictionary).Entity;

                try
                {
                    await _context.SaveChangesAsync();

                    inserted = await GetByIdAsync(inserted.Id);

                    return(inserted);
                }
                catch (DbUpdateException e)
                {
                    throw new EntityInsertException(typeof(Dictionary), e.Message);
                }
            }
            else
            {
                throw new AuthorizationException(typeof(Dictionary));
            }
        }
Ejemplo n.º 3
0
        public async Task <Attribute> InsertAsync(Attribute attribute)
        {
            if (_authorizationManager.AuthorizeByUserId(attribute.User.Id, Token))
            {
                attribute.User = await _userManager.FindByIdAsync(attribute.User.Id.ToString()) ?? throw new EntityNotFoundException(typeof(User));

                var inserted = _context.Add(attribute).Entity;

                try
                {
                    await _context.SaveChangesAsync();

                    inserted = await GetByIdAsync(inserted.Id);

                    return(inserted);
                }
                catch (DbUpdateException e)
                {
                    throw new EntityInsertException(typeof(Attribute), e.Message);
                }
            }
            else
            {
                throw new AuthorizationException(typeof(Attribute));
            }
        }
Ejemplo n.º 4
0
        public async Task DeleteAsync(Guid id)
        {
            if (_authorizationManager.AuthorizeByUserId(id, Token))
            {
                foreach (var attribute in _context.Attributes.Where(a => a.UserId == id))
                {
                    _context.Attributes.Remove(attribute);
                }

                foreach (var dictionary in _context.UserDictionaries.Include(ud => ud.Dictionary)
                         .Where(ud => ud.Type == UserType.owner &&
                                ud.UserId == id)
                         .Select(ud => ud.Dictionary))
                {
                    _context.Dictionaries.Remove(dictionary);
                }

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException e)
                {
                    throw new EntityDeleteException(typeof(User), e.Message);
                }

                var result = await _userManager.DeleteAsync(await _userManager.FindByIdAsync(id.ToString()));

                if (!result.Succeeded)
                {
                    throw new EntityDeleteException(typeof(User), result.Errors.ToString());
                }
            }
            else
            {
                throw new AuthorizationException(typeof(User));
            }
        }