public async Task <IActionResult> Update([FromBody] MenuViewModel vm)
        {
            var getResult = await _bo.ReadAsync(vm.Id);

            if (!getResult.Success)
            {
                return(InternalServerError(getResult.Exception));
            }
            var item = getResult.Result;

            if (item == null)
            {
                return(NotFound());
            }
            if (vm.CompareToModel(item))
            {
                return(NotModified());
            }
            item = vm.ToModel(item);
            var updateResult = await _bo.UpdateAsync(item);

            if (!updateResult.Success)
            {
                return(InternalServerError(updateResult.Exception));
            }
            return(Ok());
        }
Esempio n. 2
0
        public void TestUpdateMenuAsync()
        {
            ContextSeeders.Seed();
            var mbo     = new MenuBusinessObject();
            var resList = mbo.List();
            var item    = resList.Result.FirstOrDefault();

            var rbo    = new RestaurantBusinessObject();
            var mealbo = new MealBusinessObject();

            var restaurant = new Restaurant("a", "b", "c", "d", "f", 6);
            var meal       = new Meal("a", "b", "c");

            rbo.Create(restaurant);
            mealbo.Create(meal);

            var newMenu = new Menu(DateTime.Now, restaurant.Id, meal.Id);

            item.Date         = newMenu.Date;
            item.RestaurantId = newMenu.RestaurantId;
            item.MealId       = newMenu.MealId;

            var resUpdate = mbo.UpdateAsync(item).Result;

            resList = mbo.ListAsync().Result;

            Assert.IsTrue(resList.Success && resUpdate.Success &&
                          resList.Result.First().Date == newMenu.Date && resList.Result.First().RestaurantId == newMenu.RestaurantId &&
                          resList.Result.First().MealId == newMenu.MealId);
        }
Esempio n. 3
0
        public void TestUpdateMenuAsync()
        {
            RestaurantSeeder.Seed();
            var bo      = new MenuBusinessObject();
            var resList = bo.ListAsync().Result;
            var item    = resList.Result.FirstOrDefault();
            var now     = DateTime.Now;

            item.Date = now;
            var resUpdate = bo.UpdateAsync(item).Result;

            resList = bo.ListNonDeletedAsync().Result;
            Assert.IsTrue(resList.Success && resUpdate.Success && resList.Result.First().Date == now);
        }
        public async Task <IActionResult> Edit(Guid id, MenuViewModel vm)
        {
            var listRestOperation = await _rbo.ListNonDeletedAsync();

            if (!listRestOperation.Success)
            {
                return(OperationErrorBackToIndex(listRestOperation.Exception));
            }

            var listMealOperation = await _mbo.ListNonDeletedAsync();

            if (!listMealOperation.Success)
            {
                return(OperationErrorBackToIndex(listMealOperation.Exception));
            }

            var mealList = new List <SelectListItem>();

            foreach (var item in listMealOperation.Result)
            {
                mealList.Add(new SelectListItem()
                {
                    Value = item.Id.ToString(), Text = item.Name
                });
            }

            var restList = new List <SelectListItem>();

            foreach (var item in listRestOperation.Result)
            {
                restList.Add(new SelectListItem()
                {
                    Value = item.Id.ToString(), Text = item.Name
                });
            }

            ViewBag.Meals       = mealList;
            ViewBag.Restaurants = restList;

            if (ModelState.IsValid)
            {
                var getOperation = await _bo.ReadAsync(id);

                if (!getOperation.Success)
                {
                    return(OperationErrorBackToIndex(getOperation.Exception));
                }
                if (getOperation.Result == null)
                {
                    return(RecordNotFound());
                }
                var result = getOperation.Result;
                if (!vm.CompareToModel(result))
                {
                    result = vm.ToModel(result);
                    var updateOperation = await _bo.UpdateAsync(result);

                    if (!updateOperation.Success)
                    {
                        TempData["Alert"] = AlertFactory.GenerateAlert(NotificationType.Danger, updateOperation.Exception);
                        return(View(vm));
                    }
                    else
                    {
                        return(OperationSuccess("The record was successfuly updated"));
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }