public async Task <Response <CategoryResponse> > UpdateCategory([FromRoute] Guid categoryId, [FromBody] UpdateCategoryReqest request)
 {
     return(await _categoryService.UpdateCategoryAsync(categoryId, request));
 }
Beispiel #2
0
        /// <summary>
        /// Cập nhật danh mục, thêm xóa tin tức khỏi danh mục
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <Response <CategoryResponse> > UpdateCategoryAsync(Guid categoryId, UpdateCategoryReqest request)
        {
            var category = await _newsContext.Categories.FindAsync(categoryId);

            if (category == null)
            {
                _logger.LogError("Not find category with id");
                return(new Response <CategoryResponse>(Constant.STATUS_ERROR, new List <string> {
                    "Not find category with id"
                }));
            }

            if (!string.IsNullOrEmpty(request.Name))
            {
                category.Name = request.Name;
            }

            if (request.ParentId.HasValue)
            {
                category.ParentId = request.ParentId;
            }

            //Thêm danh sách tin tức vào danh mục
            foreach (var newsId in request.AddNewsIds ?? new List <Guid> {
            })
            {
                var news = await _newsContext.News.FindAsync(newsId);

                if (news != null)
                {
                    var categoryNews = _newsContext.CategoryNews.Where(x => x.CategoryId.Equals(category.Id) && x.NewsId.Equals(news.Id));

                    if (categoryNews != null && categoryNews.Any())
                    {
                        _logger.LogError($"News already in '{category.Name}'");
                        return(new Response <CategoryResponse>(Constant.STATUS_ERROR, new List <string> {
                            $"News already in '{category.Name}'"
                        }));
                    }

                    await _newsContext.CategoryNews.AddAsync(new CategoryNews
                    {
                        Id         = Guid.NewGuid(),
                        CategoryId = category.Id,
                        NewsId     = news.Id
                    });
                }
            }

            //Xóa danh sách tin tức khỏi danh mục
            foreach (var newsId in request.RemoveNewsIds ?? new List <Guid> {
            })
            {
                var news = await _newsContext.News.FindAsync(newsId);

                if (news != null)
                {
                    var categoryNews = _newsContext.CategoryNews.Where(x => x.NewsId.Equals(newsId));
                    if (categoryNews != null && categoryNews.Any())
                    {
                        _newsContext.CategoryNews.RemoveRange(categoryNews);
                    }
                }
            }

            var dataResponse = new CategoryResponse
            {
                Id            = category.Id,
                Name          = category.Name,
                ParentId      = category.ParentId,
                SubCategories = await GetSubCategoryAsync(category.Id)
            };

            var result = await _newsContext.SaveChangesAsync();

            if (result > 0)
            {
                _logger.LogInformation("Update category success");
                return(new Response <CategoryResponse>(Constant.STATUS_SUCESS, null, dataResponse, 1));
            }

            _logger.LogError("Update category error");
            return(new Response <CategoryResponse>(Constant.STATUS_ERROR, new List <string> {
                "Error when save"
            }));
        }