public void Update_InvalidObjectPassed_ReturnsNotFoundRequest() { var owner1 = new Models.ProductOwnerDto { OwnerId = 1, OwnerName = "owner 1", Address = "owner address 1", Email = "*****@*****.**", Phone = "213421412341" }; var producer = new ProducerDto() { ProducerId = 1, ProducerName = "Manufacturer 1", ProducerAddress = "Manufacturer address 1" }; var product = new Models.ProductDto() { ProductId = 9, ProductName = "product name updated", Description = "product test updated", Price = 231.33424m, Owner = owner1, Producer = producer }; // Act var createdResponse = _controller.Update(product.ProductId, product); // Assert Assert.IsType <NotFoundResult>(createdResponse.Result); }
public void Add_ValidObjectPassed_ReturnsCreatedResponse() { // Arrange var owner1 = new ProductOwnerDto { OwnerId = 1, OwnerName = "owner 1", Address = "owner address 1", Email = "*****@*****.**", Phone = "213421412341" }; var producer = new ProducerDto() { ProducerId = 1, ProducerName = "Manufacturer 1", ProducerAddress = "Manufacturer address 1" }; var category = new CategoryDto() { CategoryId = 1, Name = "Category 1", Description = "Category 1", Parent = null, ParentId = null }; var product = new Models.ProductDto() { ProductId = 7, ProductName = "product name test", Description = "product test description", Price = 231.33424m, Owner = owner1, Producer = producer, Category = category }; // Act var createdResponse = _controller.Create(product).Result; // Assert Assert.IsType <CreatedAtRouteResult>(createdResponse); }
public async Task <IActionResult> Create([FromBody] Models.ProductDto item) { try { if (item.IsObjectNull()) { return(BadRequest("Product object is null.")); } if (!ModelState.IsValid) { return(BadRequest("Invalid product object sent from client.")); } var currentItem = _mapper.Map <Models.Product>(item); await _service.Add(currentItem); return(CreatedAtRoute("GetProduct", new { Controller = "Products", id = item.ProductId }, item)); } catch (Exception ex) { Log.Error(ex, $"{GetType().Name}.{System.Reflection.MethodBase.GetCurrentMethod().Name} exception"); return(StatusCode((int)HttpStatusCode.InternalServerError, "Internal server error")); } }
public void Add_ValidObjectPassed_ReturnedResponseHasCreatedItem() { // Arrange var owner1 = new ProductOwnerDto { OwnerId = 1, OwnerName = "owner 1", Address = "owner address 1", Email = "*****@*****.**", Phone = "213421412341" }; var producer = new ProducerDto() { ProducerId = 1, ProducerName = "Manufacturer 1", ProducerAddress = "Manufacturer address 1" }; var category = new CategoryDto() { CategoryId = 1, Name = "Category 1", Description = "Category 1", Parent = null, ParentId = null }; var product = new Models.ProductDto() { ProductId = 7, ProductName = "product name test special", Description = "product test description", Price = 231.33424m, Owner = owner1, Producer = producer, Category = category }; if (!(_controller.GetById(product.ProductId).Result as NotFoundResult != null)) { var okResponse = _controller.Delete(product.ProductId); // Assert Assert.IsType <OkResult>(okResponse.Result); } // Act var t = _controller.Create(product).Result; var createdResponse = _controller.Create(product).Result as CreatedAtRouteResult; var item = createdResponse.Value as Models.ProductDto; // Assert Assert.IsType <Models.ProductDto>(item); Assert.Equal("product name test special", item.ProductName); }
public static Models.ProductDto Map(this Equipment equipment) { if (equipment == null) { return(null); } var p = new Models.ProductDto(); p.Id = equipment.Id; p.Name = equipment.Name; p.TranslationKey = equipment.TranslationKey; p.Type = equipment.Type.ToString("f"); p.ImageUrl = equipment.PictureUrl; p.Description = equipment.Description; return(p); }
public void Update_InvalidObjectPassed_ReturnsBadRequest() { var owner1 = new ProductOwnerDto { OwnerId = 1, OwnerName = "owner 1", Address = "owner address 1", Email = "*****@*****.**", Phone = "213421412341" }; var producer = new ProducerDto() { ProducerId = 1, ProducerName = "Manufacturer 1", ProducerAddress = "Manufacturer address 1" }; var category = new CategoryDto() { CategoryId = 1, Name = "Category 1", Description = "Category 1", Parent = null, ParentId = null }; var product = new Models.ProductDto() { ProductId = 9, ProductName = "product name updated", Description = "product test updated", Price = 231.33424m, Owner = owner1, Producer = producer, Category = category }; _controller.ModelState.AddModelError("ProductName", "Required"); // Act var createdResponse = _controller.Update(product.ProductId, product); // Assert Assert.IsType <BadRequestObjectResult>(createdResponse.Result); }
public async Task <IActionResult> Update(int id, [FromBody] Models.ProductDto item) { try { if (item.IsObjectNull()) { return(BadRequest("Product object is null.")); } if (!ModelState.IsValid) { return(BadRequest("Invalid product object sent from client.")); } var product = await _service.Find(id); if (product.IsObjectNull()) { return(NotFound()); } var currentItem = _mapper.Map <Models.Product>(item); await _service.Update(currentItem); //return NoContent(); return(Ok()); } catch (DbUpdateConcurrencyException ex) { return(Conflict(ex)); } catch (Exception ex) { Log.Error(ex, $"{GetType().Name}.{System.Reflection.MethodBase.GetCurrentMethod().Name} exception"); return(StatusCode((int)HttpStatusCode.InternalServerError, "Internal server error")); } }