Example #1
0
        public void TestUpdateProductUnitAsync()
        {
            ContextSeeder.Seed();
            var bo      = new ProductUnitBusinessObject();
            var resList = bo.ListAsync().Result;
            var item    = resList.Result.FirstOrDefault();

            item.IsReserved = true;
            var resUpdate = bo.UpdateAsync(item).Result;

            resList = bo.ListNotDeletedAsync().Result;
            Assert.IsTrue(resList.Success && resUpdate.Success && resList.Result.First().IsReserved == true);
        }
        public async Task <IActionResult> Edit(Guid id, ProductUnitViewModel vm)
        {
            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);

                        getOperation = await _bo.ReadAsync((Guid)id);

                        if (!getOperation.Success)
                        {
                            return(OperationErrorBackToIndex(getOperation.Exception));
                        }
                        if (getOperation.Result == null)
                        {
                            return(RecordNotFound());
                        }

                        vm = ProductUnitViewModel.Parse(getOperation.Result);

                        var listPMOperation = await _pmbo.ListNotDeletedAsync();

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

                        var pmList = new List <SelectListItem>();
                        foreach (var item in listPMOperation.Result)
                        {
                            pmList.Add(new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = item.Name
                            });
                        }
                        ViewBag.ProductModels = pmList;


                        var listEOperation = await _ebo.ListNotDeletedAsync();

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

                        var eList = new List <SelectListItem>();
                        foreach (var item in listEOperation.Result)
                        {
                            var company = await _cbo.ReadAsync(item.CompanyId);

                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = company.Result.Name + ", " + item.Address
                            };
                            if (item.Id == vm.EstablishmentId)
                            {
                                listItem.Selected = true;
                            }
                            eList.Add(listItem);
                        }
                        ViewBag.Establishments = eList;

                        var listSBOperation = await _sbbo.ListNotDeletedAsync();

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

                        var sbList = new List <SelectListItem>();
                        foreach (var item in listSBOperation.Result)
                        {
                            var profile = await _pbo.ReadAsync(item.ProfileId);

                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = profile.Result.VatNumber.ToString()
                            };
                            if (item.Id == vm.ProductModelId)
                            {
                                listItem.Selected = true;
                            }
                            sbList.Add(listItem);
                        }
                        ViewBag.ShoppingBaskets = sbList;

                        Draw("Edit", "fa-edit");

                        return(View(vm));
                    }
                    if (!updateOperation.Result)
                    {
                        TempData["Alert"] = AlertFactory.GenerateAlert(NotificationType.Danger, updateOperation.Message);

                        getOperation = await _bo.ReadAsync((Guid)id);

                        if (!getOperation.Success)
                        {
                            return(OperationErrorBackToIndex(getOperation.Exception));
                        }
                        if (getOperation.Result == null)
                        {
                            return(RecordNotFound());
                        }

                        vm = ProductUnitViewModel.Parse(getOperation.Result);

                        var listEOperation = await _ebo.ListNotDeletedAsync();

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

                        var listPMOperation = await _pmbo.ListNotDeletedAsync();

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

                        var pmList = new List <SelectListItem>();
                        foreach (var item in listPMOperation.Result)
                        {
                            pmList.Add(new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = item.Name
                            });
                        }
                        ViewBag.ProductModels = pmList;


                        var eList = new List <SelectListItem>();
                        foreach (var item in listEOperation.Result)
                        {
                            var company = await _cbo.ReadAsync(item.CompanyId);

                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = company.Result.Name + ", " + item.Address
                            };
                            if (item.Id == vm.EstablishmentId)
                            {
                                listItem.Selected = true;
                            }
                            eList.Add(listItem);
                        }
                        ViewBag.Establishments = eList;

                        var listSBOperation = await _sbbo.ListNotDeletedAsync();

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

                        var sbList = new List <SelectListItem>();
                        foreach (var item in listSBOperation.Result)
                        {
                            var profile = await _pbo.ReadAsync(item.ProfileId);

                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = profile.Result.VatNumber.ToString()
                            };
                            if (item.Id == vm.ProductModelId)
                            {
                                listItem.Selected = true;
                            }
                            sbList.Add(listItem);
                        }
                        ViewBag.ShoppingBaskets = sbList;

                        Draw("Edit", "fa-edit");

                        return(View(vm));
                    }
                    else
                    {
                        return(OperationSuccess("The record was successfuly updated"));
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }