Exemplo n.º 1
0
        public IActionResult RemoveProductFromCategory([FromBody] ProductCategoryRequest request)
        {
            var productContext  = _dbContext.Set <Product>().Include(x => x.ProductCategories);
            var categoryContext = _dbContext.Set <Category>();

            var product  = productContext.FirstOrDefault(x => x.Id == Guid.Parse(request.ProductId));
            var category = categoryContext.FirstOrDefault(x => x.Id == Guid.Parse(request.CategoryId));

            if (product == null || category == null)
            {
                return(BadRequest("Product or Category does not exist"));
            }
            var pc = product.ProductCategories?.FirstOrDefault(x => x.CategoryId == category.Id);

            if (product.ProductCategories == null || pc == null)
            {
                return(BadRequest("Product is not in the category"));
            }

            _dbContext.Remove(pc);

            var result = _dbContext.SaveChanges();

            if (result > 0)
            {
                return(Ok());
            }
            return(BadRequest("Something went wrong with database"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Update(ProductCategoryModel model)
        {
            if (model.Id <= 0)
            {
                return(RedirectToErrorPage());
            }

            var exist = await _productCategoryService.FindAsync(model.Id);

            if (exist == null)
            {
                return(RedirectToErrorPage());
            }

            var category = new ProductCategoryRequest()
            {
                Description = model.Description,
                Name        = model.Name,
                ParentId    = model.ParentId,
                Id          = model.Id,
                UpdatedById = LoggedUserId
            };

            await _productCategoryService.UpdateAsync(category);

            return(RedirectToAction(nameof(Detail), new { id = category.Id }));
        }
        public async Task <bool> ActiveAsync(ProductCategoryRequest request)
        {
            await _productCategoryRepository.Get(x => x.Id == request.Id)
            .Set(x => x.StatusId, (int)ProductCategoryStatus.Actived)
            .Set(x => x.UpdatedById, request.UpdatedById)
            .Set(x => x.UpdatedDate, DateTimeOffset.UtcNow)
            .UpdateAsync();

            return(true);
        }
        public async Task <bool> UpdateAsync(ProductCategoryRequest category)
        {
            await _productCategoryRepository.Get(x => x.Id == category.Id)
            .Set(x => x.Description, category.Description)
            .Set(x => x.Name, category.Name)
            .Set(x => x.ParentId, category.ParentId)
            .Set(x => x.UpdatedById, category.UpdatedById)
            .Set(x => x.UpdatedDate, DateTimeOffset.UtcNow)
            .UpdateAsync();

            return(true);
        }
Exemplo n.º 5
0
        public IActionResult AddProductToCategory([FromBody] ProductCategoryRequest request)
        {
            var productContext  = _dbContext.Set <Product>().Include(x => x.ProductCategories);
            var categoryContext = _dbContext.Set <Category>()
                                  .Include(x => x.ParentCategory).ThenInclude(x => x.ProductCategories)
                                  .Include(x => x.ChildCategories).ThenInclude(x => x.ProductCategories);

            var product  = productContext.FirstOrDefault(x => x.Id == Guid.Parse(request.ProductId));
            var category = categoryContext.FirstOrDefault(x => x.Id == Guid.Parse(request.CategoryId));

            if (product == null || category == null)
            {
                return(BadRequest("Product or Category does not exist"));
            }

            if (product.ProductCategories == null)
            {
                product.ProductCategories = new List <ProductCategory>();
            }

            if (product.ProductCategories.FirstOrDefault(x =>
                                                         x.CategoryId == category.Id && x.ProductId == product.Id) != null)
            {
                return(BadRequest("This product is already in this category"));
            }

            if (category.ParentCategory?.ProductCategories?.FirstOrDefault(x =>
                                                                           x.CategoryId == category.Id && x.ProductId == product.Id) != null)
            {
                return(BadRequest("This product is already in parent category"));
            }
            if (category.ChildCategories?.Any(y => y.ProductCategories?.FirstOrDefault(x =>
                                                                                       x.CategoryId == category.Id && x.ProductId == product.Id) != null) == true)
            {
                return(BadRequest("This product is already in child category"));
            }
            var pcContext = _dbContext.Set <ProductCategory>();

            pcContext.Add(new ProductCategory()
            {
                CategoryId = Guid.Parse(request.CategoryId),
                ProductId  = Guid.Parse(request.ProductId)
            });

            var result = _dbContext.SaveChanges();

            if (result == 0)
            {
                return(BadRequest("Something went wrong with database"));
            }

            return(Ok());
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Post(ProductCategoryRequest request)
        {
            try
            {
                ProductCategory productCategory = _mapper.Map(request);

                productCategory = await _repository.AddAsync(productCategory);

                return(CreatedAtAction(nameof(GetById), new { id = productCategory.Id }, _mapper.Map(productCategory)));
            }

            catch (DataStoreException e)
            {
                _logger.LogError(e.Message, e, request);
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
        public async Task <int> CreateAsync(ProductCategoryRequest category)
        {
            var newCategory = new ProductCategory()
            {
                Description = category.Description,
                Name        = category.Name,
                ParentId    = category.ParentId,
                CreatedById = category.CreatedById,
                UpdatedById = category.UpdatedById,
                UpdatedDate = DateTimeOffset.UtcNow,
                CreatedDate = DateTimeOffset.UtcNow,
                StatusId    = ProductCategoryStatus.Actived.GetCode()
            };

            var id = await _productCategoryRepository.AddWithInt32EntityAsync(newCategory);

            return(id);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Validate request
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        public void Validate(ProductCategoryRequest request, out ProductCategoryResponse response)
        {
            response = new ProductCategoryResponse();

            if (request.Action != null && request.Action.Equals(ClinicEnums.Action.DELETE.ToString()))
            {
                ValidateForDelete(request, out response);
            }
            else
            {
                bool isHavePrivilege = true;

                if (request.Data.Name == null || String.IsNullOrWhiteSpace(request.Data.Name))
                {
                    errorFields.Add("ProductCategory Name");
                }

                if (errorFields.Any())
                {
                    response.Status  = false;
                    response.Message = string.Format(Messages.ValidationErrorFields, String.Join(",", errorFields));
                }

                if (request.Data.Id == 0)
                {
                    isHavePrivilege = IsHaveAuthorization(ADD_PRIVILEGE_NAME, request.Data.Account.Privileges.PrivilegeIDs);
                }
                else
                {
                    isHavePrivilege = IsHaveAuthorization(EDIT_PRIVILEGE_NAME, request.Data.Account.Privileges.PrivilegeIDs);
                }

                if (!isHavePrivilege)
                {
                    response.Status  = false;
                    response.Message = Messages.UnauthorizedAccess;
                }

                if (response.Status)
                {
                    response = new ProductCategoryHandler(_unitOfWork).CreateOrEdit(request);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Delete validation
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        private void ValidateForDelete(ProductCategoryRequest request, out ProductCategoryResponse response)
        {
            response = new ProductCategoryResponse();

            if (request.Action == ClinicEnums.Action.DELETE.ToString())
            {
                bool isHavePrivilege = IsHaveAuthorization(DELETE_PRIVILEGE_NAME, request.Data.Account.Privileges.PrivilegeIDs);
                if (!isHavePrivilege)
                {
                    response.Status  = false;
                    response.Message = Messages.UnauthorizedAccess;
                }
            }

            if (response.Status)
            {
                response = new ProductCategoryHandler(_unitOfWork).RemoveData(request);
            }
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Create(ProductCategoryModel model)
        {
            var exist = _productCategoryService.FindByName(model.Name);

            if (exist != null)
            {
                return(RedirectToErrorPage());
            }

            var category = new ProductCategoryRequest
            {
                Description = model.Description,
                Name        = model.Name,
                ParentId    = model.ParentId,
                UpdatedById = LoggedUserId,
                CreatedById = LoggedUserId
            };

            var id = await _productCategoryService.CreateAsync(category);

            return(RedirectToAction(nameof(Detail), new { id }));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Put(Guid id, ProductCategoryRequest request)
        {
            try
            {
                ProductCategory productCategory = await _repository.GetByIdAsync(id);

                if (productCategory == null)
                {
                    return(NotFound());
                }

                _mapper.Map(productCategory, request);

                await _repository.UpdateAsync(productCategory);

                return(Ok());
            }

            catch (DataStoreException e)
            {
                _logger.LogError(e.Message, e, request);
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Exemplo n.º 12
0
        public async Task <string> AddProductCategory(ProductCategoryRequest request)
        {
            try
            {
                using var sqlCon = new SqlConnection(_options.connectionString);
                await sqlCon.OpenAsync();

                var dynamicParameters = new DynamicParameters();
                dynamicParameters.Add("@productId", request.ProductId);
                dynamicParameters.Add("@categoryId", request.CategoryId);
                var res = new ProductCategoryResponse();
                await sqlCon.ExecuteAsync(
                    "spAddProductCategory",
                    dynamicParameters,
                    commandType : CommandType.StoredProcedure);

                return(res.Messages = Enumhelper.GetEnumDescription(StatusHandlersEnum.StatusHandle.CreateSuccess));
            }
            catch (Exception ex)
            {
                var res = new ProductCategoryResponse();
                return(res.Messages = ex.Message);
            }
        }
Exemplo n.º 13
0
 public async Task <bool> ActiveAsync(ProductCategoryRequest farmType)
 {
     return(await _productCategoryRepository.ActiveAsync(farmType));
 }
 public async Task <string> DeleteProductCategory([FromBody] ProductCategoryRequest request)
 {
     return(await _productCategoryService.DeleteProductCategory(request.ProductId, request.CategoryId));
 }
Exemplo n.º 15
0
 public async Task <int> CreateAsync(ProductCategoryRequest request)
 {
     return(await _productCategoryRepository.CreateAsync(request));
 }
Exemplo n.º 16
0
 public async Task <bool> UpdateAsync(ProductCategoryRequest request)
 {
     return(await _productCategoryRepository.UpdateAsync(request));
 }
 public async Task <string> UpdateProductCategory([FromBody] ProductCategoryRequest request)
 {
     return(await _productCategoryService.UpdateProductCategory(request));
 }