Example #1
0
        /// <inheritdoc />
        public async Task <Result> AddCategory(BlogCategoryDto blogCategoryDto)
        {
            await using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var picture = new Picture();

                    if (blogCategoryDto.Picture != null)
                    {
                        picture = await GetFile(blogCategoryDto.Picture);
                    }

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

                    var blogCategory = GetBlogCategory(blogCategoryDto, picture);
                    _context.Add(blogCategory);

                    SaveInformationBlogCategory2BlogCategory(parentBlogCategory, blogCategory);

                    await transaction.CommitAsync();

                    return(Result.Ok());
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new ApplicationException(e.InnerException.Message ?? e.Message);
                }
            }
        }
 private static BlogCategoryCommand ToCommmand(this BlogCategoryDto blogCategory)
 {
     return(new BlogCategoryCommand
     {
         Name = blogCategory.Name
     });
 }
Example #3
0
        public async Task RemoveCategoryFromBlog(BlogCategoryDto blogCategoryDto)
        {
            var categoryToDeleteFromBlog = await blogCategoryDal.GetByFilter(I => I.BlogId == blogCategoryDto.BlogId && I.CategoryId == blogCategoryDto.CategoryId);

            if (categoryToDeleteFromBlog != null)
            {
                await blogCategoryDal.Delete(categoryToDeleteFromBlog);
            }
        }
Example #4
0
        public async Task AddCategoryToBlog(BlogCategoryDto blogCategoryDto)
        {
            var categoryToAddToBlog = await blogCategoryDal.GetByFilter(I => I.CategoryId == blogCategoryDto.CategoryId &&
                                                                        I.BlogId == blogCategoryDto.BlogId);

            if (categoryToAddToBlog == null)
            {
                await blogCategoryDal.Create(new BlogCategory { BlogId = blogCategoryDto.BlogId, CategoryId = blogCategoryDto.CategoryId });
            }
        }
Example #5
0
 /// <summary>
 ///     Получить объект BlogCategory
 /// </summary>
 /// <param name="blogCategoryDto">ДТО для работы с категориями блога.</param>
 /// <returns>
 ///     <see cref="BlogCategory" />
 /// </returns>
 private BlogCategory GetBlogCategory(BlogCategoryDto blogCategoryDto, Picture picture)
 {
     return(new BlogCategory
     {
         Description = blogCategoryDto.Description,
         HtmlH1 = blogCategoryDto.HtmlH1,
         MetaDescription = blogCategoryDto.MetaDescription,
         MetaKeywords = blogCategoryDto.MetaKeywords,
         MetaTitle = blogCategoryDto.MetaTitle,
         Name = blogCategoryDto.Name,
         Sort = blogCategoryDto.Sort,
         Status = blogCategoryDto.Status,
         Picture = picture
     });
 }
Example #6
0
        public async Task <ActionResult <string> > PutBlogCategory([FromForm] BlogCategoryDto category)
        {
            // check if Source BogCategory existing
            var sourceCategory = await _context.BlogSourceCategoryName.Select(s => s.Id).ToListAsync();

            if (!sourceCategory.Contains(category.SourceCategoryId))
            {
                return(BadRequest(new ApiResponse(400, "the Source Id of Blog's Category not Existing")));
            }

            // check if this category existing
            var _category = await _context.BlogCategory.FirstOrDefaultAsync(c => c.SourceCategoryId == category.SourceCategoryId &&
                                                                            c.LanguageId == category.LanguageId);

            if (_category != null)
            {
                _category.Name = category.Name;

                _context.Entry(_category).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(Ok(new ApiResponse(201, $" Update {category.Name} Blog Category successfully")));
            }
            else
            {
                var blogCategory = new BlogCategory
                {
                    Name             = category.Name,
                    SourceCategoryId = category.SourceCategoryId,
                    LanguageId       = category.LanguageId
                };
                await _context.BlogCategory.AddAsync(blogCategory);

                await _context.SaveChangesAsync();

                return(Ok(new ApiResponse(201, $" Add {category.Name} Blog Category successfully")));
            }
        }
        public IActionResult UpdateReview([FromForm] BlogCategoryDto categoryDto)
        {
            var result = _blogCategoryService.UpdateCategory(categoryDto);

            return(Ok(result));
        }
        public async Task <IActionResult> AddArticle([FromForm] BlogCategoryDto categoryDto)
        {
            var result = await _blogCategoryService.AddCategory(categoryDto);

            return(Ok(result));
        }
Example #9
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);
            }
        }
Example #10
0
        public async Task <IActionResult> RemoveCategory(BlogCategoryDto blogCategoryDto)
        {
            await blogService.RemoveCategoryFromBlog(new BlogCategoryDto { BlogId = blogCategoryDto.BlogId, CategoryId = blogCategoryDto.CategoryId });

            return(Created("", blogCategoryDto));
        }
Example #11
0
        public async Task <IActionResult> AddCategory(BlogCategoryDto blogCategoryDto)
        {
            await blogService.AddCategoryToBlog(blogCategoryDto);

            return(Created("", blogCategoryDto));
        }