public ActionResult ModifyAnnualLeave(LeaveApplicationViewModel leaveApplicationViewModel, string leaveApplicationId) { if (!ModelState.IsValid) { return(View(leaveApplicationViewModel)); } try { LeaveApplication leaveApplication = _context.LeaveApplications.Find(new Guid(leaveApplicationId)); leaveApplication.StartDate = leaveApplicationViewModel.StartDateTime; leaveApplication.EndDate = leaveApplicationViewModel.EndDateTime; leaveApplication.Discription = leaveApplicationViewModel.Description; leaveApplication.Comment = leaveApplicationViewModel.Comment; User user = _context.Users.Find(HttpContext.User.Identity.GetUserId()); if (!LeaveApplicationHelper.CanApprove(user, leaveApplicationViewModel.ApproverEmail)) { throw new InvalidOperationException("请填写正确的审批人邮箱地址"); } User approver = _context.Users.Where(u => u.Email == leaveApplicationViewModel.ApproverEmail).FirstOrDefault(); leaveApplication.Approver = approver; leaveApplication.TotalDays = LeaveApplicationHelper.CaculateTotalDays(leaveApplicationViewModel.StartDateTime, leaveApplicationViewModel.EndDateTime, _context); leaveApplication.TaskState = TaskState.Applying; _leaveApplicationService.ModifyLeaveApplication(leaveApplication); return(RedirectToAction("MyAnnualLeave")); } catch (InvalidOperationException invalidException) { ModelState.AddModelError("", invalidException.Message); } catch (Exception e) { _logger.Error("Failed to create a new Annual Leave application", e); ModelState.AddModelError("", e.Message); } return(View(leaveApplicationViewModel)); }
public LeaveApplication Create(string initiatorId, DateTime startDate, DateTime endDate, string description, string approverEmail, string comment, LeaveType leaveType) { User approver = null; float totalDays; User initiator = _context.Users.Find(initiatorId); LeaveApplication leaveApplication = new LeaveApplication(); approver = _context.Users.Where(u => u.Email == approverEmail).FirstOrDefault(); if (approver == null) { throw new InvalidOperationException(@"请填写正确的直属审批人的邮箱地址"); } if (!LeaveApplicationHelper.CanApprove(initiator, approverEmail)) { throw new InvalidOperationException(@"请填写正确的直属审批人的邮箱地址"); } if (endDate <= startDate) { throw new InvalidOperationException(@"申请结束日期不可以早于等于申请开始日期"); } totalDays = LeaveApplicationHelper.CaculateTotalDays(startDate, endDate, _context); leaveApplication.Initiator = initiator; leaveApplication.StartDate = startDate; leaveApplication.EndDate = endDate; leaveApplication.Discription = description; leaveApplication.Approver = approver; leaveApplication.Id = Guid.NewGuid(); leaveApplication.Comment = comment; leaveApplication.TaskState = TaskState.Starting; leaveApplication.TotalDays = totalDays; leaveApplication.LeaveType = LeaveType.AnnualLeave; return(leaveApplication); }