public ActionResult Create([FromBody] ReservedQueueViewModel vm)
        {
            var reservedQueue = vm.ToReservedQueue();
            var res           = _bo.Create(reservedQueue);

            return(StatusCode(res.Success ? (int)HttpStatusCode.OK : (int)HttpStatusCode.InternalServerError));
        }
        public async Task <IActionResult> Create(ReservedQueueViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var model           = vm.ToReservedQueue();
                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 listEstOperation = await _ebo.ListNotDeletedAsync();

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

                    var listProOperation = await _pbo.ListNotDeletedAsync();

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

                    var estList = new List <SelectListItem>();
                    foreach (var item in listEstOperation.Result)
                    {
                        estList.Add(new SelectListItem()
                        {
                            Value = item.Id.ToString(), Text = item.Address
                        });
                    }

                    var profiList = new List <SelectListItem>();
                    foreach (var item in listProOperation.Result)
                    {
                        profiList.Add(new SelectListItem()
                        {
                            Value = item.Id.ToString(), Text = item.VatNumber.ToString()
                        });
                    }
                    ViewBag.Establishments = estList;
                    ViewBag.Profiles       = profiList;

                    Draw("Create", "fa-plus");
                    return(View(vm));
                }
                else
                {
                    return(OperationSuccess("The record was successfuly created"));
                }
            }
            return(View(vm));
        }
        public async Task <IActionResult> Index()
        {
            var listOperation = await _bo.ListNotDeletedAsync();

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

            var lst = new List <ReservedQueueViewModel>();

            foreach (var item in listOperation.Result)
            {
                var limitCheck = await _bo.TwoHourLimitReserveAsync(item.Id);

                if (limitCheck.Result && limitCheck.Success)
                {
                    lst.Add(ReservedQueueViewModel.Parse(item));
                }
            }

            var listEOperation = await _ebo.ListNotDeletedAsync();

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

            var elst = new List <EstablishmentViewModel>();

            foreach (var item in listEOperation.Result)
            {
                elst.Add(EstablishmentViewModel.Parse(item));
            }

            var estList = await GetEstablishmentViewModels(listOperation.Result.Select(x => x.EstablishmentId).Distinct().ToList());

            var profiList = await GetProfileViewModels(listOperation.Result.Select(x => x.ProfileId).Distinct().ToList());

            var cList = await GetCompanyViewModels(listEOperation.Result.Select(x => x.CompanyId).Distinct().ToList());

            ViewData["Companies"]      = cList;
            ViewData["Establishments"] = estList;
            ViewData["Profiles"]       = profiList;
            ViewData["Title"]          = "Reserved Queues";
            ViewData["BreadCrumbs"]    = GetCrumbs();
            ViewData["DeleteHref"]     = GetDeleteRef();

            return(View(lst));
        }
        public ActionResult <ReservedQueueViewModel> Get(Guid id)
        {
            var res = _bo.Read(id);

            if (res.Success)
            {
                if (res.Result == null)
                {
                    return(NotFound());
                }
                var rqvm = new ReservedQueueViewModel();
                rqvm.Id = res.Result.Id;
                rqvm.EstablishmentId = res.Result.EstablishmentId;
                rqvm.ProfileId       = res.Result.ProfileId;
                return(rqvm);
            }

            else
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
        public ActionResult Update([FromBody] ReservedQueueViewModel reservedQueue)
        {
            var currentRes = _bo.Read(reservedQueue.Id);

            if (!currentRes.Success)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
            var current = currentRes.Result;

            if (current == null)
            {
                return(NotFound());
            }

            if (current.EstablishmentId == reservedQueue.EstablishmentId && current.ProfileId == reservedQueue.ProfileId)
            {
                return(StatusCode((int)HttpStatusCode.NotModified));
            }

            if (current.EstablishmentId != reservedQueue.EstablishmentId)
            {
                current.EstablishmentId = reservedQueue.EstablishmentId;
            }
            if (current.ProfileId != reservedQueue.ProfileId)
            {
                current.ProfileId = reservedQueue.ProfileId;
            }


            var updateResult = _bo.Update(current);

            if (!updateResult.Success)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
            return(Ok());
        }
        public async Task <IActionResult> Edit(Guid id, ReservedQueueViewModel 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 = ReservedQueueViewModel.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());
                        }

                        vm = ReservedQueueViewModel.Parse(getOperation.Result);
                        Draw("Edit", "fa-edit");
                        return(View(vm));
                    }
                    else
                    {
                        return(OperationSuccess("The record was successfuly updated"));
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(Guid?id)
        {
            if (id == null)
            {
                return(RecordNotFound());
            }

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

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

            var vm = ReservedQueueViewModel.Parse(getOperation.Result);
            var listEstOperation = await _ebo.ListNotDeletedAsync();

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

            var listProOperation = await _pbo.ListNotDeletedAsync();

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

            var estList = new List <SelectListItem>();

            foreach (var item in listEstOperation.Result)
            {
                var listItem = new SelectListItem()
                {
                    Value = item.Id.ToString(), Text = item.Address
                };
                if (item.Id == vm.EstablishmentId)
                {
                    listItem.Selected = true;
                }
                estList.Add(listItem);
            }

            var profiList = new List <SelectListItem>();

            foreach (var item in listProOperation.Result)
            {
                var listItem = new SelectListItem()
                {
                    Value = item.Id.ToString(), Text = item.VatNumber.ToString()
                };
                if (item.Id == vm.ProfileId)
                {
                    listItem.Selected = true;
                }
                profiList.Add(listItem);
            }

            ViewBag.Establishments = estList;
            ViewBag.Profiles       = profiList;
            Draw("Edit", "fa-edit");
            return(View(vm));
        }
        public async Task <IActionResult> Details(Guid?id)
        {
            if (id == null)
            {
                return(RecordNotFound());
            }
            var getOperation = await _bo.ReadAsync((Guid)id);

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

            var getEstOperation = await _ebo.ReadAsync(getOperation.Result.EstablishmentId);

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

            var getProOperation = await _pbo.ReadAsync(getOperation.Result.ProfileId);

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

            var getEOperation = await _ebo.ReadAsync(getOperation.Result.EstablishmentId);

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

            var getCOperation = await _cbo.ReadAsync(getEOperation.Result.CompanyId);

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

            var vm = ReservedQueueViewModel.Parse(getOperation.Result);

            Draw("Details", "fa-search");
            ViewData["Company"]       = CompanyViewModel.Parse(getCOperation.Result);
            ViewData["Establishment"] = EstablishmentViewModel.Parse(getEstOperation.Result);
            ViewData["Profile"]       = ProfileViewModel.Parse(getProOperation.Result);
            return(View(vm));
        }