public async Task DeleteAsync(Guid id) { var translation = await _context.Translations .FindAsync(id) ?? throw new EntityNotFoundException(typeof(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)) { _context.Remove(translation); try { await _context.SaveChangesAsync(); } catch (DbUpdateException e) { throw new EntityDeleteException(typeof(Translation), e.Message); } } else { throw new AuthorizationException(typeof(Translation)); } }
public async Task DeleteAsync(Guid id) { var userDictionary = await _context.UserDictionaries.Include(ud => ud.Dictionary) .SingleOrDefaultAsync(ud => ud.DictionaryId == id && ud.Type == UserType.owner) ?? throw new EntityNotFoundException(typeof(Dictionary)); if (_authorizationManager.AuthorizeByUserId(userDictionary.UserId, Token)) { _context.Remove(userDictionary.Dictionary); try { await _context.SaveChangesAsync(); } catch (DbUpdateException e) { throw new EntityDeleteException(typeof(Dictionary), e.Message); } } else { throw new AuthorizationException(typeof(Dictionary)); } }
public async Task UpdateAsync(Attribute attribute) { var user = await _userManager.FindByIdAsync(attribute.User.Id.ToString()) ?? throw new EntityNotFoundException(typeof(User)); if (_authorizationManager.AuthorizeByUserId(user.Id, Token)) { attribute.User = user; if (attribute.Type == DAL.Enums.AttributeType.spinner) { foreach (var attributeValue in _context.AttributeValues .Include(av => av.Attribute) .ThenInclude(a => a.AttributeParameters) .Where(av => av.AttributeId == attribute.Id) .AsNoTracking()) { foreach (var ap in attributeValue.Attribute.AttributeParameters) { if (attributeValue.Value == ap.Value) { if (attribute.AttributeParameters.Select(nap => nap.Id).Contains(ap.Id)) { attributeValue.Value = attribute.AttributeParameters.SingleOrDefault(nap => nap.Id == ap.Id).Value; _context.Entry(attributeValue).State = EntityState.Modified; } else { attributeValue.Attribute = null; _context.Remove(attributeValue); } } } } } foreach (var ap in _context.AttributeParameters.Where(ap => ap.AttributeId == attribute.Id)) { _context.AttributeParameters.Remove(ap); } foreach (var ap in attribute.AttributeParameters) { _context.Add(ap); } _context.Entry(attribute).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateException e) { throw new EntityUpdateException(typeof(Attribute), e.Message); } } else { throw new AuthorizationException(typeof(Attribute)); } }