Ejemplo n.º 1
0
        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"));
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
0
 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));
 }
Ejemplo n.º 5
0
 public Guid AddSubCategory(NewSubCategoryDto data)
 {
     return(service.AddNewSubCategory(data));
 }