public async Task UpdateCategoryAsync_ValidRequest_ProductUpdated()
        {
            var newCategory = new UpdateProductCategoryRequest()
            {
                Code = "cc"
            };
            var mockUserContext            = new Mock <IUserContext>();
            var mockProductContext         = new Mock <IProductCatalogueContext>();
            List <DbCategory> categoryList = new List <DbCategory>()
            {
                new DbCategory()
                {
                    Id = 1
                },
                new DbCategory()
                {
                    Id = 2
                }
            };

            mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(categoryList);
            mockUserContext.Setup(c => c.UserId).Returns(1);
            var service = new ProductCategoryService(mockProductContext.Object, mockUserContext.Object);

            var updatedProduct = await service.UpdateCategoryAsync(1, newCategory);

            Assert.Equal(newCategory.Code, updatedProduct.Code);
        }
        public async Task CreateCategoryAsync_UniqueCode_ProductWithSpecifiedCodeCreated()
        {
            const string newCode     = "cc";
            var          newCategory = new UpdateProductCategoryRequest()
            {
                Code = newCode
            };
            var mockUserContext            = new Mock <IUserContext>();
            var mockProductContext         = new Mock <IProductCatalogueContext>();
            List <DbCategory> categoryList = new List <DbCategory>()
            {
                new DbCategory()
                {
                    Id = 1
                },
                new DbCategory()
                {
                    Id = 2
                }
            };

            mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(categoryList);
            mockUserContext.Setup(c => c.UserId).Returns(1);
            var service = new ProductCategoryService(mockProductContext.Object, mockUserContext.Object);

            var product = await service.CreateCategoryAsync(newCategory);

            Assert.Equal(newCode, product.Code);
        }
        public async Task UpdateCategoryAsync_IdNotPresent_RequestedResourceNotFoundExceptionThrown()
        {
            var newCategory = new UpdateProductCategoryRequest()
            {
                Code = "bb"
            };
            var mockUserContext            = new Mock <IUserContext>();
            var mockProductContext         = new Mock <IProductCatalogueContext>();
            List <DbCategory> categoryList = new List <DbCategory>()
            {
                new DbCategory()
                {
                    Id = 1
                },
                new DbCategory()
                {
                    Id = 2
                }
            };

            mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(categoryList);
            var service = new ProductCategoryService(mockProductContext.Object, mockUserContext.Object);

            Assert.ThrowsAsync <RequestedResourceNotFoundException>(() => service.UpdateCategoryAsync(3, newCategory));
        }
        /// <inheritdoc/>
        public async Task <ProductCategory> UpdateCategoryAsync(int categoryId, UpdateProductCategoryRequest updateRequest)
        {
            var dbCategories = await _context.Categories.Where(c => c.Code == updateRequest.Code && c.Id != categoryId).ToArrayAsync();

            if (dbCategories.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            dbCategories = await _context.Categories.Where(c => c.Id == categoryId).ToArrayAsync();

            var dbCategory = dbCategories.FirstOrDefault();

            if (dbCategory == null)
            {
                throw new RequestedResourceNotFoundException();
            }

            _mapper.Map(updateRequest, dbCategory);
            dbCategory.LastUpdatedBy = _userContext.UserId;

            await _context.SaveChangesAsync();

            dbCategories = await _context.Categories.Where(c => c.Id == categoryId).ToArrayAsync();

            return(dbCategories.Select(c => _mapper.Map <ProductCategory>(c)).FirstOrDefault());
        }
Example #5
0
        public IHttpActionResult Update(UpdateProductCategoryRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var productCategory = _productCategoryService.GetById(model.ID);
                if (productCategory != null)
                {
                    productCategory.UpdateProductCategory(model);
                    var result = _productCategoryService.Update(productCategory);
                    if (result != null)
                    {
                        var productCategoryVm = Mapper.Map <ProductCategoryDetailResponse>(result);

                        return(Ok(productCategoryVm));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Website(ex);
            }

            return(BadRequest());
        }
Example #6
0
        public async Task <IActionResult> UpdateProductCategory(int id, [FromBody] UpdateProductCategoryRequest updateRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _categoryService.UpdateCategoryAsync(id, updateRequest);

            return(NoContent());
        }
Example #7
0
        public async Task <IActionResult> AddProductCategory([FromBody] UpdateProductCategoryRequest createRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var category = await _categoryService.CreateCategoryAsync(createRequest);

            var location = string.Format("/api/categories/{0}", category.Id);

            return(Created(location, category));
        }
Example #8
0
 public static void UpdateProductCategory(this ProductCategory model, UpdateProductCategoryRequest request)
 {
     model.ID              = request.ID;
     model.Name            = request.Name;
     model.Alias           = request.Alias;
     model.Description     = request.Description;
     model.DisplayOrder    = request.DisplayOrder;
     model.HotFlag         = request.HotFlag;
     model.Image           = request.Image;
     model.Status          = request.Status;
     model.UpdateBy        = request.UpdateBy;
     model.UpdateDate      = request.UpdateDate;
     model.ParentID        = request.ParentID;
     model.MetaDescription = request.MetaDescription;
     model.MetaKeyword     = request.MetaKeyword;
 }
        public async Task CreateCategoryAsync_RequestedResourceHasConflictException(
            [Frozen] Mock <IProductCatalogueContext> context,
            ProductCategoryService service,
            IFixture fixture)
        {
            var categories     = fixture.CreateMany <DbProductCategory>(5).ToList();
            var products       = fixture.CreateMany <CatalogueProduct>(5).ToList();
            var updateCategory = new UpdateProductCategoryRequest {
                Code = categories[0].Code
            };

            context.Setup(x => x.Products).ReturnsEntitySet(products);
            context.Setup(x => x.Categories).ReturnsEntitySet(categories);

            Func <Task> act = async() => await service.CreateCategoryAsync(updateCategory);

            act.Should().Throw <RequestedResourceHasConflictException>();
        }
Example #10
0
        public async Task <IHttpActionResult> AddProductCategoryAsync([FromBody] UpdateProductCategoryRequest createRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var category = await _categoryService.CreateCategoryAsync(createRequest);

                var location = string.Format("/api/categories/{0}", category.Id);
                return(Created <ProductCategory>(location, category));
            }
            catch (RequestedResourceHasConflictException)
            {
                return(Conflict());
            }
        }
        /// <inheritdoc/>
        public async Task <ProductCategory> CreateCategoryAsync(UpdateProductCategoryRequest createRequest)
        {
            var dbCategories = await _context.Categories.Where(c => c.Code == createRequest.Code).ToArrayAsync();

            if (dbCategories.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            var dbCategory = _mapper.Map <UpdateProductCategoryRequest, DbProductCategory>(createRequest);

            dbCategory.CreatedBy     = _userContext.UserId;
            dbCategory.LastUpdatedBy = _userContext.UserId;
            _context.Categories.Add(dbCategory);

            await _context.SaveChangesAsync();

            return(_mapper.Map <ProductCategory>(dbCategory));
        }
Example #12
0
        public async Task <IHttpActionResult> AddProductCategory([FromBody] UpdateProductCategoryRequest createRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var category = await _categoryService.CreateCategoryAsync(createRequest);

                var location = string.Format("/api/categories/{0}", category.Id);
                return(Created <ProductCategory>(location, category));
            }
            catch (RequestedResourceHasConflictException ex)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, ex.Message)));
            }
            catch (Exception)
            {
                return(this.InternalServerError());
            }
        }
        public async Task <IHttpActionResult> UpdateProductCategory([FromUri] int id, [FromBody] UpdateProductCategoryRequest updateRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _categoryService.UpdateCategoryAsync(id, updateRequest);

            return(ResponseMessage(Request.CreateResponse(HttpStatusCode.NoContent)));
        }
Example #14
0
        public async Task <IHttpActionResult> UpdateProductCategoryAsync([FromUri] int id, [FromBody] UpdateProductCategoryRequest updateRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id < 1)
            {
                return(BadRequest($"Argument {nameof(id)} must be greater than zero."));
            }

            try
            {
                await _categoryService.UpdateCategoryAsync(id, updateRequest);

                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.NoContent)));
            }
            catch (RequestedResourceHasConflictException)
            {
                return(Conflict());
            }
            catch (RequestedResourceNotFoundException)
            {
                return(NotFound());
            }
        }
Example #15
0
        public async Task <IActionResult> UpdateProductCategory([FromRoute] Guid productCategoryId, [FromBody] UpdateProductCategoryRequest updateProductCategoryRequest)
        {
            var productCategory = await _productCategoryService
                                  .GetProductCategoryByIdAsync(productCategoryId);

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

            productCategory.ProductCategoryName = updateProductCategoryRequest.ProductCategoryName;

            var updated = await _productCategoryService
                          .UpdateProductCategoryAsync(productCategory);

            if (updated)
            {
                return(Ok(new Response <ProductCategoryResponse>(_mapper.Map <ProductCategoryResponse>(productCategory))));
            }

            return(NotFound());
        }
Example #16
0
        public async Task <IHttpActionResult> UpdateProductCategory([FromUri] int id, [FromBody] UpdateProductCategoryRequest updateRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var result = await _categoryService.UpdateCategoryAsync(id, updateRequest);

                return(this.Ok(result));
            }
            catch (RequestedResourceHasConflictException ex)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, ex.Message)));
            }
            catch (RequestedResourceNotFoundException ex)
            {
                return(this.BadRequest(ex.Message));
            }
            catch (Exception)
            {
                return(this.InternalServerError());
            }
        }