private MemberAttendanceForView GetMemberAttendanceForViewByProjectId(int projectId, DateTime weekRequested) { MemberAttendanceForView dataToView; var weekSought = weekRequested.GetWeek(); var yearSought = weekRequested.Year; var attendances = _unitOfWork.AttendanceRepository.Get(filter: x => x.ProjectId == projectId && x.Week == weekSought && x.Year == yearSought).ToList(); //if there is no data then pick the data from memberToProject repository for first time use if (!attendances.Any()) { dataToView = CreateAttendanceDataForWeekAndProject(projectId, weekRequested); } else { var attendance = attendances[0]; //attendaceId = attendance.AttendanceId; dataToView = GetMemberAttendanceForViewById(attendance, weekRequested); //plus add any newly added member this week from memberToProject table var newMemberAttendanceNotYetSavedForWeekSought = CreateAttendanceDataForWeekAndProject(projectId, weekRequested); //filter down all member who have already registered for the week sought foreach (var item in newMemberAttendanceNotYetSavedForWeekSought.MemberAttendances) { if (!dataToView.MemberAttendances.Exists(x => x.MemberId == item.MemberId)) { dataToView.MemberAttendances.Add(item); } } } return dataToView; }
private void UpdateAttendance(MemberAttendanceForView memberAttendanceData, int projectId, DateTime weekRequested) { var weekSought = weekRequested.GetWeek(); var yearSought = weekRequested.Year; bool isInsert = memberAttendanceData.AttendanceId == -1; Attendance attendance; if (isInsert) //that means its an INSERT { attendance = new Attendance { ProjectId = projectId, CreatedBy = User.Identity.Name, CreatedOn = DateTime.UtcNow, Year = yearSought, Week = weekSought, WeekString = GetDisplayWeek(weekRequested.StartOfWeek(DayOfWeek.Monday)) }; using (var scope = new TransactionScope()) { try { _unitOfWork.AttendanceRepository.Insert(attendance); _unitOfWork.Save(); foreach (var item in memberAttendanceData.MemberAttendances) { var newMembAtt = GetNewMemberAttendance(attendance.AttendanceId, item); _unitOfWork.MemberAttendanceRepository.Insert(newMembAtt); } _unitOfWork.Save(); scope.Complete(); } catch (Exception ex) { scope.Dispose(); //Log the error (uncomment dex variable name after DataException and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); Utility.WriteToLog("AttendanceController.UpdateAttendance() : " + ex, "Error"); throw new Exception(ex.Message); } } } else { attendance = _unitOfWork.AttendanceRepository.GetById(memberAttendanceData.AttendanceId); attendance.ModifiedBy = User.Identity.Name; attendance.ModifiedOn = DateTime.UtcNow; try { _unitOfWork.AttendanceRepository.Update(attendance); _unitOfWork.Save(); foreach (var item in memberAttendanceData.MemberAttendances) { var item1 = item; var memberAttendance = _unitOfWork.MemberAttendanceRepository.Get(filter: x => x.MemberId == item1.Member.MemberId && x.AttendanceId == attendance.AttendanceId).ToList(); if (memberAttendance.Any()) { var dataToUpdate = memberAttendance[0]; UpdateMemberAttendanceData(item1, dataToUpdate); _unitOfWork.MemberAttendanceRepository.Update(dataToUpdate); } } _unitOfWork.Save(); } catch (Exception ex) { //scope.Dispose(); //Log the error (uncomment dex variable name after DataException and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); Utility.WriteToLog("AttendanceController.UpdateAttendance() : " + ex, "Error"); throw new Exception(ex.Message); } } }
public static IEnumerable<DateTime> EveryNWeeks(DateTime begin, int num) { if (num < 1) return new DateTime[0]; return EveryCore(begin.GetWeek(), now => now.AddDays(7 * num)); }