// GET: LeaveRequestController/Create public async Task <ActionResult> Create() { var userId = _userManager.GetUserAsync(User).Result.Id; var EmployeeAllocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployee(userId); var list = new List <LeaveType>(); foreach (var item in EmployeeAllocation.ToList()) { list.Add(item.LeaveType); } var leaveTypeItems = list.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); var model = new CreateLeaveRequestVm { LeaveTypes = leaveTypeItems }; return(View(model)); }
public async Task <ActionResult> Create(CreateLeaveRequestVm model) { try { var StartDate = Convert.ToDateTime(model.StartDate); var EndDate = Convert.ToDateTime(model.EndDate); var leaveTypes = await _leaveTypeRepo.FindAll(); var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if ((StartDate < DateTime.Now) || (EndDate < DateTime.Now)) { ModelState.AddModelError("", "Start Date or End Date cannot be earlier than today's date"); return(View(model)); } if (DateTime.Compare(StartDate, EndDate) > 0) { ModelState.AddModelError("", "Start date cannot be greater than End date"); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int DaysRequested = (int)(EndDate.Date - StartDate.Date).TotalDays; if (DaysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "Days Requested Exceeds number of days available"); return(View(model)); } var leaveRequestModel = new LeaveRequestVM { RequestingEmployeeId = employee.Id, LeaveTypeId = model.LeaveTypeId, StartDate = StartDate, EndDate = EndDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, RequestsComments = model.RequestsComments }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _leaveRequestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something Went Wrong with the registration...."); return(View()); } return(RedirectToAction(nameof(Index), "Home")); } catch { ModelState.AddModelError("", "Something Went Wrong...."); return(View()); } }