public void RemoveProductAssignment() { var target = new CategoriesController(this._EFCategoryRepository, this._EFProductCategoryRepository, this._MockMapper); var productLinkDto = new ProductLinkDto { ProductID = 2 }; var result = target.RemoveProductAssignment(8, productLinkDto); var okResult = result as OkObjectResult; var returnResult = (bool)okResult.Value; Assert.Equal(200, okResult.StatusCode); Assert.True(returnResult); Assert.Single(this._EFProductCategoryRepository.ProductCategories.Where(pc => pc.ProductID == 2).ToList()); // Is product and category not exists productLinkDto = new ProductLinkDto { ProductID = 22 }; result = target.AssignProductCategory(8, productLinkDto); var badResult = result as BadRequestObjectResult; Assert.Equal(400, badResult.StatusCode); }
public IActionResult RemoveProductAssignment(int categoryId, [FromBody] ProductLinkDto productLinkDto) { try { if (this._ProductCategoryRepository.DelProductCategory(new ProductCategory { CategoryId = categoryId, ProductID = productLinkDto.ProductID }) > 0) { return(Ok(true)); } else { return(BadRequest(new { Message = "Fails to Remove." })); } } catch (Exception) { return(StatusCode(500, "Internal server error")); } }
public IActionResult AssignProductCategory(int categoryId, [FromBody] ProductLinkDto productLinkDto) { try { if (this._ProductCategoryRepository.SaveProductCategory(new ProductCategory { CategoryId = categoryId, ProductID = productLinkDto.ProductID }) > 0) { return(Ok(true)); } else { return(BadRequest(new { Message = new string[] { "Product fails to Assign.Category or Product does not exist", "Product assigned to the category" } })); } } catch (Exception) { return(StatusCode(500, "Internal server error")); } }