Beispiel #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));
            }
        }
Beispiel #2
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));
            }
        }
Beispiel #3
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));
            }
        }
Beispiel #4
0
        public async Task UpdateAsync(User user)
        {
            if (_authorizationManager.AuthorizeByUserId(user.Id, Token))
            {
                var identityUser = await _userManager.FindByIdAsync(user.Id.ToString())
                                   ?? throw new EntityNotFoundException(typeof(User));

                identityUser.UserName = user.UserName;
                identityUser.Email    = user.Email;
                identityUser.Role     = user.Role;

                var result = (await _userManager.UpdateAsync(identityUser));

                if (!result.Succeeded)
                {
                    throw new EntityUpdateException("A felhasználónév már foglalt");
                }
            }
            else
            {
                throw new AuthorizationException(typeof(User));
            }
        }