public ActionResult Index(Employee employee, string command) { if (ModelState.IsValid) { InOutLogs logs = homeService.GetAllLogs(employee.EmployeeId); if (command.Equals("Clock In") && logs.logInList.Count() == logs.logOutList.Count()) { homeService.LogIn(DateTime.Now, employee.EmployeeId); employee.LastLog = homeService.CollectLastLog(employee.EmployeeId); return(RedirectToAction("ShowLastLog", "Home", new { employeeId = employee.EmployeeId })); } if (command.Equals("Clock Out") && logs.logInList.Count() > logs.logOutList.Count()) { homeService.LogOut(DateTime.Now, employee.EmployeeId); employee.LastLog = homeService.CollectLastLog(employee.EmployeeId); return(RedirectToAction("ShowLastLog", "Home", new { employeeId = employee.EmployeeId })); } else { ModelState.AddModelError(string.Empty, "You have not loged in or loged out."); ModelState.Remove("EmployeeId"); return(View()); } } else { return(View()); } }
public InOutLogs CollectAllLogs(int?eployeeId) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; conn.Open(); SqlCommand collectDataCommand = new SqlCommand("SELECT * FROM ClockIn WHERE EmployeeId = @employeeId", conn); collectDataCommand.Parameters.Add(new SqlParameter("employeeId", eployeeId)); InOutLogs inOutLogs = new InOutLogs(); inOutLogs.logInList = new List <DateTime>(); inOutLogs.logOutList = new List <DateTime>(); using (SqlDataReader reader = collectDataCommand.ExecuteReader()) { while (reader.Read()) { if (!reader.IsDBNull(0)) { DateTime log = reader.GetDateTime(0); inOutLogs.logInList.Add(log); } if (!reader.IsDBNull(1)) { DateTime log = reader.GetDateTime(1); inOutLogs.logOutList.Add(log); } } return(inOutLogs); } } }
public DateTime CollectLastLog(int?employeeId) { InOutLogs logs = dataBaseConnector.CollectAllLogs(employeeId); List <DateTime> list = new List <DateTime>(); list.AddRange(logs.logInList); list.AddRange(logs.logOutList); return(list.Max()); }
public TimeSpan CountHoursWorked(Employee employee) { TimeSpan timeWorked = TimeSpan.Zero; InOutLogs logs = dataBaseConnector.CollectLogs(employee.PeriodStart, employee.PeriodEnd, employee.EmployeeId); foreach (var logIn in logs.logInList) { List <DateTime> logsOut = logs.logOutList.Where(m => m > logIn).ToList(); if (logsOut.Count() > 0) { DateTime logOut = logsOut.Min(); timeWorked += logOut - logIn; } } return(timeWorked); }