Exemplo n.º 1
0
        public ActionResult Create([FromBody] EstablishmentViewModel vm)
        {
            var establishment = vm.ToModel();
            var res           = _bo.Create(establishment);

            return(StatusCode(res.Success ? (int)HttpStatusCode.OK : (int)HttpStatusCode.InternalServerError));
        }
        public async Task<IActionResult> Create(EstablishmentViewModel vm)
        {
            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);

                    var listCOperation = await _cbo.ListNotDeletedAsync();
                    if (!listCOperation.Success) return OperationErrorBackToIndex(listCOperation.Exception);
                    var cList = new List<SelectListItem>();
                    foreach (var item in listCOperation.Result)
                    {
                        cList.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.Name });
                    }

                    var listROperation = await _rbo.ListNotDeletedAsync();
                    if (!listROperation.Success) return OperationErrorBackToIndex(listROperation.Exception);
                    var rList = new List<SelectListItem>();
                    foreach (var item in listROperation.Result)
                    {
                        rList.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.Name });
                    }

                    ViewBag.Regions = rList;
                    ViewBag.Companies = cList;
                    Draw("Create", "fa-plus");
                    return View();
                }
                else return OperationSuccess("The record was successfuly created");
            }
            return View(vm);
        }
        public async Task<IActionResult> Edit(Guid id, EstablishmentViewModel 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();

                        var listROperation = await _rbo.ListNotDeletedAsync();
                        if (!listROperation.Success) return OperationErrorBackToIndex(listROperation.Exception);
                        var rList = new List<SelectListItem>();
                        foreach (var item in listROperation.Result)
                        {
                            var listItem = new SelectListItem() { Value = item.Id.ToString(), Text = item.Name };
                            if (item.Id == vm.RegionId) listItem.Selected = true;
                            rList.Add(listItem);
                        }
                        ViewBag.Regions = rList;

                        var listCOperation = await _cbo.ListNotDeletedAsync();
                        if (!listCOperation.Success) return OperationErrorBackToIndex(listCOperation.Exception);
                        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.CompanyId) listItem.Selected = true;
                            cList.Add(listItem);
                        }
                        ViewBag.Companies = cList;

                        vm = EstablishmentViewModel.Parse(getOperation.Result);

                        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();
                        var listROperation = await _rbo.ListNotDeletedAsync();
                        if (!listROperation.Success) return OperationErrorBackToIndex(listROperation.Exception);
                        var rList = new List<SelectListItem>();
                        foreach (var item in listROperation.Result)
                        {
                            var listItem = new SelectListItem() { Value = item.Id.ToString(), Text = item.Name };
                            if (item.Id == vm.RegionId) listItem.Selected = true;
                            rList.Add(listItem);
                        }
                        ViewBag.Regions = rList;

                        var listCOperation = await _cbo.ListNotDeletedAsync();
                        if (!listCOperation.Success) return OperationErrorBackToIndex(listCOperation.Exception);
                        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.CompanyId) listItem.Selected = true;
                            cList.Add(listItem);
                        }
                        ViewBag.Companies = cList;

                        vm = EstablishmentViewModel.Parse(getOperation.Result);

                        Draw("Edit", "fa-edit");
                        return View(vm);
                    }
                    else return OperationSuccess("The record was successfuly updated");
                }
            }
            return RedirectToAction(nameof(Index));
        }