public async Task <IActionResult> Enroll(Guid?id) { if (id == null) { return(NotFound()); } var enrollment = await _enrollmentService.GetEnrollmentIncludingFieldsAndChoicesAsync(id.Value); // enrollment not found if (enrollment == null) { return(NotFound()); } var model = new EnrollingEnrollViewModel { Enrollment = enrollment, }; // enrollment not ready if (!_enrollingService.IsReadyForEnrolling(enrollment)) { return(View("EnrollmentNotReady", model)); } // exceed max count of enrollees if (await _enrollingService.ExceedsMaxCountOfEnrollees(enrollment)) { return(View("ExceedMaxCountOfEnrollees", model)); } var user = await _userSerivce.GetUserWithEmployeeInfoAsync(Guid.Parse(User.GetUserId())); if (user?.Employee != null) { model.EmployeeNo = user.Employee.No; model.EmailAddress = user.Employee.EmailAddress; model.Name = user.Employee.ChineseName; } else { model.EmailAddress = user.Username; } return(View(model)); }
public async Task <IActionResult> Enroll(EnrollingEnrollViewModel model) { var enrollment = await _enrollmentService.GetEnrollmentIncludingFieldsAndChoicesAsync(model.Enrollment.Id); if (enrollment == null) { return(NotFound()); } model.Enrollment = enrollment; // enrollment not ready if (!_enrollingService.IsReadyForEnrolling(enrollment)) { return(View("EnrollmentNotReady", model)); } // exceed max count of enrollees if (await _enrollingService.ExceedsMaxCountOfEnrollees(enrollment)) { return(View("ExceedMaxCountOfEnrollees", model)); } // already enrolled if (await _enrollingService.IsAlreadyEnrolled(model.EmployeeNo, enrollment)) { return(View("AlreadyEnrolled", model)); } // check if self-enroll only var user = await _userSerivce.GetUserWithEmployeeInfoAsync(Guid.Parse(User.GetUserId())); if (enrollment.SelfEnrollmentOnly && user.Employee.No != model.EmployeeNo) { return(View("SelfEnrollmentOnly", model)); } // check required fields var requiredFields = enrollment.ExtraFormFields.Where(f => f.IsRequired); if (requiredFields.Any()) { foreach (var field in requiredFields) { var key = $"FieldInputs.{field.Id}"; var formField = (from item in Request.Form where item.Key == key select item).FirstOrDefault(); if (string.IsNullOrWhiteSpace(formField.Value)) { ModelState.AddModelError(string.Empty, $"{field.Name} is required."); } } } if (ModelState.IsValid) { var fieldInputs = (from item in Request.Form where item.Key.StartsWith("FieldInputs.") select item).ToDictionary(item => item.Key, item => item.Value.ToString()); var enrolling = await _enrollingService.CreateAsync(enrollment.Id, model.EmployeeNo, fieldInputs, Guid.Parse(User.GetUserId()), User.GetUsername()); return(RedirectToAction("Details", new { Id = enrolling.Id })); } return(View(model)); }