public async Task <IActionResult> ChangePrice(IngredientDetailsToCreateDto price, int ingredientId) { if (ModelState.IsValid) { IServiceResult <IngredientModel> result = await _service.UpdatePriceAsync(price, ingredientId); if (result.Result == ResultType.Edited) { return(CreatedAtRoute("GetIngedientById", new { id = result.ReturnedObject.Id }, result.ReturnedObject)); } else { return(BadRequest(result.Errors)); } } else { return(BadRequest()); } }
public async Task UpdatePrice() { int idOfIngredient = 1; PizzaModel pizza = (await GetFakePizzas()).First(); IngredientDetailsToCreateDto newPrice = new IngredientDetailsToCreateDto { Size = SizeEnum.Small, Price = 5.50M }; Mock <IFoodOrderRepository <PizzaModel> > pizzaRepoMock = new Mock <IFoodOrderRepository <PizzaModel> >(); repoMock.Setup(x => x.GetByExpressionAsync(It.IsAny <Expression <Func <IngredientModel, bool> > >(), It.IsAny <Func <IQueryable <IngredientModel>, IIncludableQueryable <IngredientModel, object> > >())). Returns(GetFakeIngredientById(idOfIngredient)); repoMock.Setup(x => x.Update(expectedIngredients.ElementAt(idOfIngredient))); pizzaRepoMock.Setup(x => x.GetByExpressionAsync(It.IsAny <Expression <Func <PizzaModel, bool> > >(), It.IsAny <Func <IQueryable <PizzaModel>, IIncludableQueryable <PizzaModel, object> > >())). Returns(GetFakePizzas()); pizzaRepoMock.Setup(x => x.Update(pizza)); uowMock.Setup(x => x.Ingredients).Returns(repoMock.Object); uowMock.Setup(x => x.SaveChangesAsync()); uowMock.Setup(x => x.Pizzas).Returns(pizzaRepoMock.Object); IIngredientService ingredientService = new IngredientService(uowMock.Object); IServiceResult <IngredientModel> result = await ingredientService.UpdatePriceAsync(newPrice, idOfIngredient); uowMock.Verify(x => x.SaveChangesAsync(), Times.Exactly(2)); Assert.AreEqual(ResultType.Edited, result.Result); Assert.IsNotNull(result.ReturnedObject); Assert.AreEqual(newPrice.Price, result.ReturnedObject.IngredientDetails.Where(x => x.Size == SizeEnum.Small).SingleOrDefault().Price); }
/// <summary> /// Updates price of ingredient stored in database with id value passed by parameter /// </summary> /// <param name="price">Defines which size of ingredient should be edited and stores new price value</param> /// <param name="ingredientId">identifier of ingredient that will be updated</param> /// <returns>Ingredient with updated price or errors (depending on result type)</returns> public async Task <IServiceResult <IngredientModel> > UpdatePriceAsync(IngredientDetailsToCreateDto price, int ingredientId) { try { // find ingredient with specific id value, include all prices of it IngredientModel ingredientToUpdate = (await _repository.Ingredients.GetByExpressionAsync(x => x.Id == ingredientId, i => i.Include(id => id.IngredientDetails))).SingleOrDefault(); // get price that will be updated IngredientDetailsModel priceToUpdate = ingredientToUpdate.IngredientDetails.SingleOrDefault(x => x.Size == price.Size); if (priceToUpdate != null) { // update price priceToUpdate.Price = price.Price; // update database entry and save context changes _repository.Ingredients.Update(ingredientToUpdate); await _repository.SaveChangesAsync(); await UpdatePizzasPrices(ingredientId); return(new ServiceResult <IngredientModel>(ResultType.Edited, ingredientToUpdate)); } return(new ServiceResult <IngredientModel>(ResultType.Error, new List <string> { "Price has not been found" })); } catch (Exception e) { // catch errors and pass them to controller return(new ServiceResult <IngredientModel>(ResultType.Error, new List <string> { e.Message })); } }