// // GET: /Attendance/Details/5 //public ActionResult Details(int id = 0) //{ // //Attendance attendance = db.Attendances.Find(id); // //if (attendance == null) // //{ // // return HttpNotFound(); // //} // //return View(attendance); //} public ActionResult Create(string pid, string dt) { try { MemberAttendanceForView dataToView; if (!string.IsNullOrEmpty(pid)) { dataToView = LoadAttendanceData(pid, dt); } else { ViewBag.ProjectId = GetProjectSelectList(); dataToView = new MemberAttendanceForView { Attendance = new Attendance(), MemberAttendances = new List<MemberAttendance>(), Projects = _unitOfWork.ProjectRepository.Get().ToList(), DateForCurrentWeek = DateTime.Now.StartOfWeek(DayOfWeek.Monday) }; } return View(dataToView); } catch (Exception ex) { Utility.WriteToLog("AttendanceController.Create() : " + ex, "Error"); throw; } }
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); } } }
private bool SaveAttendanceData(MemberAttendanceForView memberAttendanceData) { var projectId = 0; if (Request.QueryString != null) { int.TryParse(Request.QueryString["pid"], out projectId); } if (projectId == 0) { ModelState.AddModelError("", "Invalid project Id. Unable to save changes. Try again, and if the problem persists see your system administrator."); ViewBag.ProjectId = GetProjectSelectList(); ViewBag.DisplayWeek = GetDisplayWeek(DateTime.Now.StartOfWeek(DayOfWeek.Monday)); //return View(memberAttendanceData); return false; } var weekRequested = new DateTime(); if (Request.QueryString != null) { DateTime.TryParse(Request.QueryString["dt"], out weekRequested); } UpdateAttendance(memberAttendanceData, projectId, weekRequested); return true; }
public ActionResult Edit(MemberAttendanceForView memberAttendanceData) { if (ModelState.IsValid) { try { var status = SaveAttendanceData(memberAttendanceData); if (!status) { return View(memberAttendanceData); } return RedirectToAction("Index"); } catch (Exception ex) { Utility.WriteToLog("AttendanceController.Edit() : " + ex, "Error"); throw; } } ViewBag.ProjectId = GetProjectSelectList(memberAttendanceData.ProjectId); ViewBag.DisplayWeek = GetDisplayWeek(DateTime.Now.StartOfWeek(DayOfWeek.Monday)); return View(memberAttendanceData); }