public async Task <IActionResult> AddSubCategory([FromRoute] int categoryId, [FromBody] NewSubCategoryDto newSubCategory)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!await _categoryRepository.CategoryExistsAsync(categoryId))
                {
                    return(NotFound());
                }

                if (await _categoryRepository.IsDuplicateSubCategoryAsync(categoryId, newSubCategory))
                {
                    ModelState.AddModelError("subCategory", "Subcategory already exists");
                    return(BadRequest(ModelState));
                }

                var subCategoryId = await _categoryRepository.AddSubCategoryAsync(categoryId, newSubCategory);

                if (subCategoryId > 0)
                {
                    return(Ok(subCategoryId));
                }

                return(StatusCode(500, "An error ocurred in server"));
            }
            catch (Exception e)
            {
                _logger.LogCritical($"POST {Route}/{categoryId}/subcategories - {e.GetType().Name} - {e.Message} - {e.StackTrace}");
                return(StatusCode(500, "An error ocurred in server"));
            }
        }
        public async Task <int> AddSubCategoryAsync(int categoryId, NewSubCategoryDto newSubCategory)
        {
            var subCategory = new SubCategory()
            {
                Name       = newSubCategory.Name,
                IsActive   = true,
                CategoryId = categoryId
            };

            await _dbContext.SubCategories.AddAsync(subCategory);

            if (await _dbContext.SaveChangesAsync() > 0)
            {
                return(subCategory.Id);
            }

            return(0);
        }
Beispiel #3
0
        public Guid AddNewSubCategory(NewSubCategoryDto data)
        {
            var categories  = cacheManager.GetOrCreate <List <CategoryDto> >(nameof(Category), () => FetchCategories());
            var hasCategory = categories.Any(f => f.Id == data.CategoryId);

            if (!hasCategory)
            {
                throw new InvalidOperationException("Category not found");
            }
            var sc = new SubCategory
            {
                Id         = Guid.NewGuid(),
                CategoryId = data.CategoryId,
                Created    = DateTime.Now,
                Updated    = DateTime.Now,
                IsActive   = true,
                IsDeleted  = false,
                ImageUrl   = null,
                Name       = data.Name,
                CreatedBy  = claims.Session.Id,
                UpdatedBy  = claims.Session.Id,
            };

            context.SubCategories.Add(sc);
            var resultValue = context.SaveChanges();

            if (resultValue > 0)
            {
                var dto = new SubCategoryDto
                {
                    Id     = sc.Id,
                    ImgUrl = sc.ImageUrl,
                    Name   = sc.Name
                };
                cacheManager.Update <List <SubCategoryDto> >(nameof(SubCategory), (cachedData) =>
                {
                    cachedData.Add(dto);
                });
                return(sc.Id);
            }
            return(Guid.Empty);
        }
 public async Task <bool> IsDuplicateSubCategoryAsync(int categoryId, NewSubCategoryDto subCategory)
 {
     return(await _dbContext.SubCategories.AnyAsync(c =>
                                                    c.Name.Equals(subCategory.Name, StringComparison.InvariantCultureIgnoreCase) && c.IsActive && c.CategoryId == categoryId));
 }
 public Guid AddSubCategory(NewSubCategoryDto data)
 {
     return(service.AddNewSubCategory(data));
 }