public ActionResult Create(CreateLeaveRequestViewModel model) { try { // TODO: Add insert logic here var leaveTypes = _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 (DateTime.Compare(model.StartDate, model.EndDate) > 1) { ModelState.AddModelError("", "Invalid Date selection..."); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocation = _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); var daysRequested = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You Do not have sufficien days for this request."); return(View(model)); } var leaveRequest = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = model.StartDate, EndDate = model.EndDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId }; var leaveRequst = _mapper.Map <LeaveRequest>(leaveRequest); var isSuccess = _leaveRequestRepo.Create(leaveRequst); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong"); return(View()); } return(RedirectToAction("UserLeaveRequests")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong"); return(View()); } }
public ActionResult Create(CreateLeaveRequestViewModel model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = _leaveTypeRepository.FindAll(); int daysRequested = (int)(endDate - startDate).TotalDays; var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date"); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocation = _leaveAllocationRepository.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); if (daysRequested > allocation.NumberofDays) { ModelState.AddModelError("", "You Do Not Have Sufficient Days For This Request"); } var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = _leaveRequestRepository.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with submitting your record"); return(View(model)); } return(RedirectToAction("MyLeave")); } catch { return(View()); } }
// GET: LeaveRequestController/Create public async Task <ActionResult> Create() { var leaveTypes = await _typeRepo.FindAll(); var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); var model = new CreateLeaveRequestViewModel { LeaveTypes = leaveTypeItems }; return(View(model)); }
public ActionResult Create() { var leaveTypes = _leaveTypeRepository.FindAll(); var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); var model = new CreateLeaveRequestViewModel { LeaveTypes = leaveTypeItems }; return(View(model)); }
// GET: LeaveRequestController/Create public async Task <ActionResult> Create() { var leaveTypes = await _leavetyperepo.FindAll(); var leaveTypeItems = leaveTypes.Select(x => new SelectListItem //assigns each value (x) in leaveTypes to a SelectListItem { Text = x.Name, Value = x.Id.ToString() }); var model = new CreateLeaveRequestViewModel { LeaveTypes = leaveTypeItems }; return(View(model)); }
// GET: LeaveRequests/Create public async Task <ActionResult> Create() { var leaveTypes = await _unitOfWork.LeaveTypes.FindAll(); var leaveTypeOptions = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); var model = new CreateLeaveRequestViewModel { StartDate = DateTime.Now, EndDate = DateTime.Now, LeaveTypes = leaveTypeOptions }; return(View(model)); }
private CreateLeaveRequestViewModel GetCreateLeaveRequestVM() { var leaveTypes = _mapper.Map <IList <LeaveTypeViewModel> >(_leaveTypeRepo.FindAll()); var leaveTypesModel = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); var model = new CreateLeaveRequestViewModel { StartDate = DateTime.Today, EndDate = DateTime.Today, LeaveTypes = leaveTypesModel }; return(model); }
// GET: LeaveRequestController/Create public async Task <ActionResult> Create() { ICollection <LeaveType> leaveTypes = await _leaveTypeRepo.FindAll(); IEnumerable <SelectListItem> leaveTypeItems = leaveTypes .Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); CreateLeaveRequestViewModel model = new CreateLeaveRequestViewModel { LeaveTypes = leaveTypeItems }; return(View(model)); }
// GET: LeaveRequestsController/Create public async Task <ActionResult> Create() { /* Load up the leave types from the database. * The selectlistitem is required for a dropdown * so we need to convert the leaveType types to * selectListItem. */ var leaveTypes = await _leaveTypeRepo.FindAll(); var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); var model = new CreateLeaveRequestViewModel { LeaveTypes = leaveTypeItems, StartDate = DateTime.Now, EndDate = DateTime.Now }; return(View(model)); }
// GET: LeaveRequest/Create public ActionResult Create() { var leaveTypes = _leaveTypeRepository.FindAll().ToList(); var employees = _employeeRepository.FindAll().ToList(); var leaveTypeItems = leaveTypes.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); var employeeItems = employees.Select(x => new SelectListItem { Text = x.FirstName + " " + x.LastName, Value = x.Id }); var model = new CreateLeaveRequestViewModel { LeaveTypes = leaveTypeItems, Employees = employeeItems }; return(View(model)); }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = await _leavetyperepo.FindAll(); var leaveTypeItems = leaveTypes.Select(x => new SelectListItem //assigns each value (x) in leaveTypes to a SelectListItem { Text = x.Name, Value = x.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start date has to be before or the same date as the end date."); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocation = await _leaveallocationrepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "Days requested exceeds available leave days."); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, LeaveTypeId = model.LeaveTypeId, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _leaverequestrepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } return(RedirectToAction(nameof(Index), "Home")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } }
public ActionResult Create(CreateLeaveRequestViewModel model) { try { if (!ModelState.IsValid) { var errormodel = GetCreateLeaveRequestVM(); return(View(errormodel)); } if (model.StartDate >= model.EndDate) { var errormodel = GetCreateLeaveRequestVM(); ModelState.AddModelError("", "Start date must be smaller than end date."); return(View(errormodel)); } var numberOfDays = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays; var employee = _userManager.GetUserAsync(User).Result; var allocation = _leaveAllocationRepo.GetLeaveAllocationsByEmployeeIdandLeaveType(employee.Id, model.LeaveTypeId, DateTime.Now.Year).FirstOrDefault(); if (numberOfDays > allocation.NumberofDays) { var errormodel = GetCreateLeaveRequestVM(); ModelState.AddModelError("", "Not enough remaining days."); return(View(errormodel)); } allocation.NumberofDays -= numberOfDays; var request = new LeaveRequestViewModel() { RequestingEmployeeId = employee.Id, StartDate = model.StartDate, EndDate = model.EndDate, DateRequested = DateTime.UtcNow, Approved = null, DateActioned = DateTime.UtcNow, LeaveTypeId = model.LeaveTypeId }; var op1 = _repo.Create(_mapper.Map <LeaveRequest>(request)); var op2 = _leaveAllocationRepo.Update(allocation); if (!op1 || !op2) { var errormodel = GetCreateLeaveRequestVM(); ModelState.AddModelError("", "Error during save."); return(View(errormodel)); } return(RedirectToAction(nameof(Index), "Home")); } catch { ModelState.AddModelError("", "Operation Error"); var errormodel = GetCreateLeaveRequestVM(); return(View(errormodel)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { var leaveTypes = await _unitOfWork.LeaveTypes.FindAll(); var leaveTypeOptions = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.LeaveTypes = leaveTypeOptions; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(model.StartDate, model.EndDate) > 1) { ModelState.AddModelError("", "The End Date cannot be before the Start Date."); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var period = DateTime.Now.Year; var allocation = await _unitOfWork.LeaveAllocations.Find( q => q.EmployeeId == employee.Id && q.LeaveTypeId == model.LeaveTypeId && q.Period == period ); int daysRequested = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not have enough leave time allocated for this request."); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { LeaveTypeId = model.LeaveTypeId, RequestingEmployeeId = employee.Id, StartDate = model.StartDate, EndDate = model.EndDate, Approved = null, Cancelled = false, DateRequested = DateTime.Now, DateActioned = DateTime.Now }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); await _unitOfWork.LeaveRequests.Create(leaveRequest); await _unitOfWork.Save(); return(RedirectToAction(nameof(Index), "Home")); } catch { ModelState.AddModelError("", "Something went wrong..."); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = await _typeRepo.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 (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "The start date cannot be greater than the end date!"); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var allocation = await _allocationRepo.GetAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not have sufficient leave time for this request!"); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { LeaveTypeId = model.LeaveTypeId, RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, RequestComments = model.RequestComments }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _requestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Unable to submit record! Please try another request."); return(View(model)); } return(RedirectToAction("MyLeave")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong! Please try another request."); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = await _unitOfWork.LeaveTypes.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 (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start Date cannot be further in the future than the end date"); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocations = await _unitOfWork.LeaveAllocations.Find(x => x.EmployeeId == employee.Id && x.Period == DateTime.Now.Year && x.LeaveTypeId == model.LeaveTypeId); int daysRequested = (int)(startDate - endDate).TotalDays; if (daysRequested > allocations.NumberOfDays) { ModelState.AddModelError("", "You do not have sufficient days for this request"); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id , StartDate = startDate , EndDate = endDate , Approved = null , DateRequested = DateTime.Now , DateActioned = DateTime.Now , LeaveTypeId = model.LeaveTypeId }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); await _unitOfWork.LeaveRequests.Create(leaveRequest); await _unitOfWork.Save(); return(RedirectToAction(nameof(Index), "Home")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong"); return(View()); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { DateTime startDate = Convert.ToDateTime(model.StartDate); DateTime endDate = Convert.ToDateTime(model.EndDate); ICollection <LeaveType> leaveTypes = await _leaveTypeRepo.FindAll(); IEnumerable <SelectListItem> leaveTypeItems = leaveTypes .Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "The start date may not be futher" + " in the future than the end date."); return(View(model)); } Employee employee = await _userManager.GetUserAsync(User); LeaveAllocation allocation = await _leaveAllocationRepo .GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not have sufficient days for this request."); return(View(model)); } LeaveRequestViewModel leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId, RequestComments = model.RequestComments }; LeaveRequest leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); bool isSuccess = await _leaveRequestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with submitting your record."); return(View(model)); } return(RedirectToAction("MyLeave")); } catch { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel 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 (DateTime.Compare(startDate, endDate) > 0) { ModelState.AddModelError("", "Start date cannot be farther in the future than the end date."); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(startDate - endDate).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not have enough days to make this request."); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId, RequestComments = model.RequestComments }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _leaveRequestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong trying to add your Leave Request to the database."); return(View(model)); } return(RedirectToAction(nameof(MyLeave))); } catch (Exception ex) { ModelState.AddModelError("", $"Something went wrong trying to create your Leave Request:\n {ex.Message}"); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { var startDate = DateTime.Parse(model.StartDate); var endDate = DateTime.Parse(model.EndDate); var leaveTypes = await _leaveTypeRepo.FindAll(); var leaveTypeItems = leaveTypes.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 0) { ModelState.AddModelError("", "Start date cannot be the future than the end date"); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId, }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _requestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with sumbitting your record"); return(View()); } return(RedirectToAction("MyLeave")); } catch { ModelState.AddModelError("", "Something went wrong"); return(View()); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel 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 (DateTime.Compare(startDate, endDate) > 0) { ModelState.AddModelError("", "Invalid Date"); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var allocation = await _leaveallocationrepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "Insufficient days"); return(View(model)); } var leaverequestmodel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId }; var leaverequest = _mapper.Map <LeaveRequest>(leaverequestmodel); var isSuccess = await _leaverequestrepo.Create(leaverequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } return(RedirectToAction(nameof(Index), "Home")); } catch { ModelState.AddModelError("", "Something went wrong"); return(View()); } }
public ActionResult Create(CreateLeaveRequestViewModel model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = _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 (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date"); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocation = _leaveAllocationRepo.GetLeaveAllocationByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not sufficient days for this request"); return(View(model)); } var leaveRequest = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId }; var leaveRequestModel = _mapper.Map <LeaveRequest>(leaveRequest); var isSuccess = _leaveRequestRepo.Create(leaveRequestModel); if (!isSuccess) { ModelState.AddModelError("", "Something Went Wrong..."); return(View(model)); } return(RedirectToAction(nameof(Index), "Home")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong" + ex.Message); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = await _leaveTyperepo.FindAll(); var leaveTypeItems = leaveTypes.Select(result => new SelectListItem { Text = result.Name, Value = result.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start date biggert than end date"); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var allocations = await _leaveaAllocationrepo.GetLeaveAllocationByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays; if (daysRequested > allocations.NumberOfDays) { ModelState.AddModelError("", "You dont have sufficient days for this request"); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId, RequestComments = model.RequestComments }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _leaveaRequestrepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong whit submitting your request"); return(View(model)); } return(RedirectToAction("MyLeave")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong"); return(View()); } }
public ActionResult Create(CreateLeaveRequestViewModel model) { try { var leaveTypes = _leaveTypeRepository.FindAll().ToList(); var employees = _employeeRepository.FindAll().ToList(); var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypeItems = leaveTypes.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); var employeeItems = employees.Select(x => new SelectListItem { Text = x.FirstName + " " + x.LastName, Value = x.Id }); model.LeaveTypes = leaveTypeItems; model.Employees = employeeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "End Date Can not be earlier than Start Date"); return(View(model)); } var allocation = _leaveAllocationRepository.GetLeaveAllocationByEmployeeAndLeaveType(model.EmployeeId, model.LeaveTypeId); var daysRequested = (int)(endDate - startDate).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not have enough days"); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = model.EmployeeId, StartDate = startDate, EndDate = endDate, Approved = null, RequestDate = DateTime.Now, }; var leaveRequest = new LeaveRequest { Approved = null, ApprovedBy = null, EndDate = endDate, LeaveType = _leaveTypeRepository.FindById(model.LeaveTypeId), RequestDate = DateTime.Now, RequestingEmployee = _employeeRepository.FindById(Guid.Parse(model.EmployeeId)), StartDate = startDate }; var isSuccess = _leaveRequestRepository.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with submitting the record"); return(View(model)); } if (User.IsInRole("Administrator")) { return(RedirectToAction(nameof(Index))); } return(RedirectToAction("Index", "Home")); } catch (Exception exception) { ModelState.AddModelError("", "Something Went Wrong: " + exception.Message); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { /* Loads in the leaveTypes data again in the event of * model evaluation returning a result that is not valid. * This allows us to return the view with model without * returning with missing data which would be a poor UX. */ 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)); } /* Date selection validation 1: * Checks that the end date is not before the start date * if so it returns to the view and doesn't process. */ if (DateTime.Compare(model.StartDate, model.EndDate) > 1) { ModelState.AddModelError("", "The end date cannot be before the start date."); return(View(model)); } /* load up the: * user details * allocation type * number of days requested */ var employee = _userManager.GetUserAsync(User).Result; var allocation = await _allocationsRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(model.EndDate - model.StartDate).TotalDays; /* Date selection validation 2: * Checks that the end date is not before the start date * if so it returns to the view and doesn't process. */ if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "Insufficient allocation exists to process the request"); return(View(model)); } var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, LeaveTypeId = model.LeaveTypeId, StartDate = model.StartDate, EndDate = model.EndDate, Approved = null, Cancelled = false, DateRequested = DateTime.Now.Date }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _leaveRequestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Insufficient allocation exists to process the request"); return(View(model)); } return(RedirectToAction("MyLeave")); } catch { ModelState.AddModelError("", "Error sending new leave request"); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { //Converted toDateTime to use it for validation and mapping purposes var startDate = DateTime.ParseExact(model.StartDate, "MM/dd/yyyy", null); var endDate = DateTime.ParseExact(model.EndDate, "MM/dd/yyyy", null); //has to create the SelectListItem, //becasue otherwise the model would //return empty list in case of fail var leaveTypes = await _unitOfWork.LeaveTypes.FindAll(); var leaveTypesItems = leaveTypes.Select(type => new SelectListItem { Text = type.Name, Value = type.Id.ToString() }); model.LeaveTypes = leaveTypesItems; var period = DateTime.Now.Year; //Check if valid #region Validation if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date"); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var leaveAllocation = await _unitOfWork.LeaveAllocations .Find(allocation => allocation.EmployeeId == employee.Id && allocation.LeaveTypeId == model.LeaveTypeId && allocation.Period == period); int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays; if (daysRequested > leaveAllocation.NumberOfDays) { ModelState.AddModelError("", "You have requested more days for " + leaveAllocation.LeaveType.Name + " than you have. " + "You have " + leaveAllocation.NumberOfDays.ToString() + " days left"); return(View(model)); } #endregion //Create a model that is properly mappable var leaveRequestModel = new LeaveRequestViewModel { LeaveTypeId = model.LeaveTypeId, RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); await _unitOfWork.LeaveReuqests.Create(leaveRequest); await _unitOfWork.Save(); //Redirects to home index, because leaverequest index is admin only return(RedirectToAction(nameof(Index), "Home")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } }