private static void AssertProductsEquality(ProductWriteItemDto expectedProduct, ProductReadItemDto actualProduct) { Assert.Equal(expectedProduct.ProductName, actualProduct.ProductName); Assert.Equal(expectedProduct.QuantityPerUnit, actualProduct.QuantityPerUnit); Assert.Equal(expectedProduct.ReorderLevel, actualProduct.ReorderLevel); Assert.Equal(expectedProduct.SelectedCategoryId, actualProduct.SelectedCategoryId); Assert.Equal(expectedProduct.SelectedSupplierId, actualProduct.SelectedSupplierId); Assert.Equal(expectedProduct.UnitPrice, actualProduct.UnitPrice); Assert.Equal(expectedProduct.UnitsInStock, actualProduct.UnitsInStock); Assert.Equal(expectedProduct.UnitsOnOrder, actualProduct.UnitsOnOrder); }
public async Task ProductApi_CreateGetUpdateDeleteProduct_SuccesfullyCreatedUpdatedRetrievedDeleted() { ProductWriteItemDto expectedProduct = GetMockedProduct(); var createdProduct = await TestProductCreation(expectedProduct); var retrievedProduct = await TestGetProductById(createdProduct); await TestUpdateProduct(createdProduct, retrievedProduct); await TestDeleteProductById(retrievedProduct); await TestGetProductByIdNotExists(retrievedProduct); }
public async Task <IActionResult> Post([FromBody] ProductWriteItemDto createModel) { if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } Products product = _mapper.Map <Products>(createModel); var cereatedProduct = await _productsService.CreateProductAsync(product); var result = _mapper.Map <ProductReadItemDto>(cereatedProduct); return(CreatedAtAction("Get", new { id = cereatedProduct.ProductId }, result)); }
public async Task <IActionResult> Put(int id, [FromBody] ProductWriteItemDto updateModel) { try { Products product = _mapper.Map <Products>(updateModel); product.ProductId = id; var updatedProduct = await _productsService.UpdateProductAsync(product); var result = _mapper.Map <ProductReadItemDto>(updatedProduct); return(Ok(result)); } catch (EntityNotFoundException) { return(NotFound($"Product with id {id} was not found")); } }
private async Task <ProductReadItemDto> TestProductCreation(ProductWriteItemDto expectedProduct) { var productCreatedResponse = await _testHostFixture.Client.PostAsJsonAsync( baseUrl, expectedProduct); productCreatedResponse.EnsureSuccessStatusCode(); var actualProduct = JsonConvert.DeserializeObject <ProductReadItemDto>( await productCreatedResponse.Content.ReadAsStringAsync()); Assert.Equal(HttpStatusCode.Created, productCreatedResponse.StatusCode); Assert.NotEqual(default(int), actualProduct.ProductId); AssertProductsEquality(expectedProduct, actualProduct); return(actualProduct); }