Example #1
0
        public async Task <IActionResult> Index()
        {
            try
            {
                IEnumerable <LeaveRequest> leaveRequests = (await leaveRequestService.GetLeaveRequests()).Select(lvlRequst =>
                {
                    lvlRequst.LeveRequestEncryptedId = protector.Protect(lvlRequst.Id.ToString());
                    return(lvlRequst);
                });

                var leaveTypes = await leaveTypeService.GetLeaveTypes();

                var employees = await employeeService.GetEmployees();

                LeaveRequestListViewModel model = new LeaveRequestListViewModel
                {
                    Employees     = employees,
                    LeaveTypes    = leaveTypes,
                    LeaveRequests = leaveRequests
                };
                return(View(model));
            }
            catch (Exception ex)
            {
                ViewBag.ErrorTitle   = "Resource not available:";
                ViewBag.ErrorMessage = ex.Message;
                return(View("CustomError"));
            }
        }
        public ActionResult RequestForLeave()
        {
            LeaveRequestListViewModel viewModel = new LeaveRequestListViewModel();

            LeaveRequestRepository leaveRequestRepository = new LeaveRequestRepository();
            LeaveRepository        leaveRepository        = new LeaveRepository();

            foreach (var leaveRequest in leaveRequestRepository.GetAll())
            {
                LeaveRequestListItemViewModel listItemViewModel = new LeaveRequestListItemViewModel();

                listItemViewModel.Id         = leaveRequest.Id;
                listItemViewModel.TotalCount = leaveRequest.TotalCount;
                listItemViewModel.LeaveId    = leaveRequest.Leave?.Id;

                listItemViewModel.LeaveList.Add(new SelectListItem()
                {
                    Value = null,
                    Text  = "",
                });

                listItemViewModel.LeaveList.AddRange(leaveRepository.GetAll().Select(s => new SelectListItem()
                {
                    Value    = s.Id.ToString(),
                    Text     = String.Format("{0}-{1}", s.LeaveType, s.Employee?.FullName),
                    Selected = s.Id == leaveRequest.Leave?.Id,
                    Disabled = false
                }));

                viewModel.LeaveRequests.Add(listItemViewModel);
            }

            LeaveRequestListItemViewModel listItemViewModel2 = new LeaveRequestListItemViewModel()
            {
                Id = 0
            };

            listItemViewModel2.LeaveList.Add(new SelectListItem()
            {
                Value = null,
                Text  = "",
            });

            listItemViewModel2.LeaveList.AddRange(leaveRepository.GetAll().Select(s => new SelectListItem()
            {
                Value    = s.Id.ToString(),
                Text     = String.Format("{0}-{1}", s.LeaveType, s.Employee?.FullName),
                Selected = false,
                Disabled = false
            }));

            viewModel.LeaveRequests.Add(listItemViewModel2);

            return(View(viewModel));
        }
        public ActionResult RequestForLeave(FormCollection fc)
        {
            LeaveRequestListViewModel leaveRequestListViewModel = new LeaveRequestListViewModel();

            TryUpdateModel(leaveRequestListViewModel);

            if (ModelState.IsValid)
            {
                LeaveRequestRepository repository      = new LeaveRequestRepository();
                LeaveRepository        leaveRepository = new LeaveRepository();

                foreach (var itemViewModel in leaveRequestListViewModel.LeaveRequests)
                {
                    LeaveRequest leaveRequest = repository.GetById(itemViewModel.Id);
                    Leave        leave        = leaveRepository.GetById(itemViewModel.LeaveId ?? 0);

                    if (leaveRequest == null)
                    {
                        //insert
                        if (itemViewModel.TotalCount > 0 && itemViewModel.Id == 0)
                        {
                            leaveRequest = new LeaveRequest(leave, itemViewModel.TotalCount);
                            repository.SaveOrUpdate(leaveRequest);
                        }
                    }
                    else
                    {
                        //update
                        leaveRequest.Leave      = leave;
                        leaveRequest.TotalCount = itemViewModel.TotalCount;
                        repository.SaveOrUpdate(leaveRequest);
                    }
                }
                return(RedirectToAction("RequestForLeave"));
            }

            return(RequestForLeave());
        }