コード例 #1
0
        /// <inheritdoc />
        public Result UpdateCategory(CategoryDto categoryDto)
        {
            var category       = _context.Categories.Find(categoryDto.Id);
            var parentCategory = _context.Categories.Find(categoryDto.ParentCategoryId);
            var picture        = GetFile(categoryDto.Pictures).Result;

            category.Description     = categoryDto.Description;
            category.HtmlH1          = categoryDto.HtmlH1;
            category.MetaDescription = categoryDto.MetaDescription;
            category.MetaKeywords    = categoryDto.MetaKeywords;
            category.MetaTitle       = categoryDto.MetaTitle;
            category.Name            = categoryDto.Name;
            category.Sort            = categoryDto.Sort;
            category.Status          = categoryDto.Status;

            _context.Update(category);
            _context.SaveChanges();

            if (parentCategory != null && parentCategory.Id != category.Id)
            {
                var categoryParent = _context.CategoryCategory.FirstOrDefault(x => x.Category1.Id == category.Id);
                if (categoryParent != null)
                {
                    _context.Remove(categoryParent);
                    _context.SaveChanges();
                }

                var categoryToCategory = new CategoryCategory
                {
                    Category1   = category,
                    Category1Id = category.Id,
                    Category2   = parentCategory,
                    Category2Id = parentCategory.Id
                };

                _context.Add(categoryToCategory);
                _context.SaveChanges();
            }

            var pictureDelete = _context.CategoryPicture.FirstOrDefault(x => x.CategoryId == category.Id);
            var pictureRemove = _context.Pictures.FirstOrDefault(x => x.Id == pictureDelete.PictureId);

            _context.Remove(pictureDelete ?? throw new ApplicationException());
            _context.Remove(pictureRemove ?? throw new ApplicationException());
            _context.SaveChanges();

            var categoryToPicture = new CategoryPicture
            {
                Category   = category,
                CategoryId = category.Id,
                Picture    = picture
            };

            _context.Add(categoryToPicture);
            _context.SaveChanges();

            return(Result.Ok());
        }
コード例 #2
0
        /// <inheritdoc />
        public Result UpdateCategory(BlogCategoryDto blogCategoryDto)
        {
            using var transaction = _context.Database.BeginTransaction();
            try
            {
                var category = _context.BlogCategory.Include(x => x.Picture)
                               .FirstOrDefault(x => x.Id == blogCategoryDto.Id);

                var file = GetFile(blogCategoryDto.Picture).Result;

                var parentCategory = _context.BlogCategory.Find(blogCategoryDto.BlogCategoryId);

                if (category != null)
                {
                    category.Picture         = file;
                    category.Description     = blogCategoryDto.Description;
                    category.HtmlH1          = blogCategoryDto.HtmlH1;
                    category.MetaDescription = blogCategoryDto.MetaDescription;
                    category.MetaKeywords    = blogCategoryDto.MetaKeywords;
                    category.MetaTitle       = blogCategoryDto.MetaTitle;
                    category.Name            = blogCategoryDto.Name;
                    category.Sort            = blogCategoryDto.Sort;
                    category.Status          = blogCategoryDto.Status;

                    _context.Update(category);

                    if (parentCategory != null)
                    {
                        var blogCategory =
                            _context.BlogCategory2BlogCategories.FirstOrDefault(x =>
                                                                                x.BlogCategory1.Id == category.Id);
                        if (blogCategory != null)
                        {
                            _context.Remove(blogCategory);
                        }

                        var newBlogCategory = new BlogCategory2BlogCategory
                        {
                            BlogCategory1   = category,
                            BlogCategory1Id = category.Id,
                            BlogCategory2Id = parentCategory.Id,
                            BlogCategory2   = parentCategory
                        };

                        _context.Add(newBlogCategory);
                        _context.SaveChanges();
                    }

                    transaction.Commit();

                    return(Result.Ok());
                }

                return(Result.Fail("Обновление категории не произошло."));
            }
            catch (Exception e)
            {
                transaction.RollbackAsync();
                throw new ApplicationException(e.InnerException.Message ?? e.Message);
            }
        }