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)); } }
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)); } }
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)); } }