Esempio n. 1
0
        public async Task UpdateAsync(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))
            {
                foreach (var av in _context.AttributeValues.Where(av => av.TranslationId == translation.Id))
                {
                    _context.AttributeValues.Remove(av);
                }

                _context.Attach(translation).State = EntityState.Modified;

                foreach (var attributeValues in translation.AttributeValues)
                {
                    foreach (var property in _context.Entry(attributeValues).Properties)
                    {
                        if (!(property.CurrentValue is Guid))
                        {
                            property.IsModified = true;
                        }
                    }
                }

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException e)
                {
                    throw new EntityUpdateException(typeof(Translation), e.Message);
                }
            }
            else
            {
                throw new AuthorizationException(typeof(Translation));
            }
        }
Esempio n. 2
0
        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));
            }
        }