예제 #1
0
        public ActionResult Create([FromBody] ProductModelViewModel vm)
        {
            var productModel = vm.ToModel();
            var res          = _bo.Create(productModel);

            return(StatusCode(res.Success ? (int)HttpStatusCode.OK : (int)HttpStatusCode.InternalServerError));
        }
        public async Task <IActionResult> Edit(Guid id, ProductModelViewModel 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 = ProductModelViewModel.Parse(getOperation.Result);

                        var listBOperation = await _bbo.ListNotDeletedAsync();

                        if (!listBOperation.Success)
                        {
                            return(OperationErrorBackToIndex(listBOperation.Exception));
                        }
                        var listCOperation = await _cbo.ListNotDeletedAsync();

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

                        ViewBag.Measure = Enum.GetNames(typeof(Measure)).Select(r => new SelectListItem {
                            Text = r, Value = r
                        });;
                        var bList = new List <SelectListItem>();
                        foreach (var item in listBOperation.Result)
                        {
                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = item.Name
                            };
                            if (item.Id == vm.BrandId)
                            {
                                listItem.Selected = true;
                            }
                            bList.Add(listItem);
                        }
                        ViewBag.Brands = bList;

                        var cList = new List <SelectListItem>();
                        foreach (var item in listCOperation.Result)
                        {
                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = item.Name
                            };
                            if (item.Id == vm.BrandId)
                            {
                                listItem.Selected = true;
                            }
                            cList.Add(listItem);
                        }
                        ViewBag.Categories = cList;

                        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 = ProductModelViewModel.Parse(getOperation.Result);

                        var listBOperation = await _bbo.ListNotDeletedAsync();

                        if (!listBOperation.Success)
                        {
                            return(OperationErrorBackToIndex(listBOperation.Exception));
                        }
                        var listCOperation = await _cbo.ListNotDeletedAsync();

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

                        ViewBag.Measure = Enum.GetNames(typeof(Measure)).Select(r => new SelectListItem {
                            Text = r, Value = r
                        });;

                        var bList = new List <SelectListItem>();
                        foreach (var item in listBOperation.Result)
                        {
                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = item.Name
                            };
                            if (item.Id == vm.BrandId)
                            {
                                listItem.Selected = true;
                            }
                            bList.Add(listItem);
                        }
                        ViewBag.Brands = bList;

                        var cList = new List <SelectListItem>();
                        foreach (var item in listCOperation.Result)
                        {
                            var listItem = new SelectListItem()
                            {
                                Value = item.Id.ToString(), Text = item.Name
                            };
                            if (item.Id == vm.BrandId)
                            {
                                listItem.Selected = true;
                            }
                            cList.Add(listItem);
                        }
                        ViewBag.Categories = cList;

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

                        return(View(vm));
                    }
                    else
                    {
                        return(OperationSuccess("The record was successfuly updated"));
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Create(ProductModelViewModel vm)
        {
            var listBOperation = await _bbo.ListNotDeletedAsync();

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

            var listCOperation = await _cbo.ListNotDeletedAsync();

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


            var bList = new List <SelectListItem>();

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

            var cList = new List <SelectListItem>();

            foreach (var item in listCOperation.Result)
            {
                cList.Add(new SelectListItem()
                {
                    Value = item.Id.ToString(), Text = item.Name
                });
            }
            ViewData["Measure"] = Enum.GetNames(typeof(Measure)).Select(r => new SelectListItem {
                Text = r, Value = r
            });;

            ViewBag.Brands     = bList;
            ViewBag.Categories = cList;

            Draw("Create", "fa-plus");

            if (ModelState.IsValid)
            {
                var model           = vm.ToModel();
                var createOperation = await _bo.CreateAsync(model);

                if (!createOperation.Success)
                {
                    return(OperationErrorBackToIndex(createOperation.Exception));
                }
                if (!createOperation.Result)
                {
                    TempData["Alert"] = AlertFactory.GenerateAlert(NotificationType.Danger, createOperation.Message);

                    return(View(vm));
                }
                else
                {
                    return(OperationSuccess("The record was successfuly created"));
                }
            }
            return(View(vm));
        }