Пример #1
0
        public async Task UpdateAsync(Dictionary dictionary)
        {
            var userId = await _context.UserDictionaries.Where(ud => ud.Type == UserType.owner &&
                                                               ud.DictionaryId == dictionary.Id)
                         .Select(ud => ud.UserId)
                         .SingleOrDefaultAsync();

            if (_authorizationManager.AuthorizeByUserId(userId, Token))
            {
                foreach (var ud in _context.UserDictionaries.Where(tm => tm.DictionaryId == dictionary.Id))
                {
                    _context.UserDictionaries.Remove(ud);
                }

                foreach (var dl in _context.DictionaryLanguages.Where(dl => dl.DictionaryId == dictionary.Id))
                {
                    _context.DictionaryLanguages.Remove(dl);
                }

                if (!dictionary.IsPublic)
                {
                    dictionary.UserDictionaries = dictionary.UserDictionaries
                                                  .Where(ud => ud.Type == UserType.owner)
                                                  .ToList();
                }

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

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException e)
                {
                    throw new EntityUpdateException(typeof(Dictionary), e.Message);
                }
            }
            else
            {
                throw new AuthorizationException(typeof(Dictionary));
            }
        }
Пример #2
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));
            }
        }