public ActionResult GetAllAuditFiles() { AuditFileModel AF = null; List <AuditFileModel> AFList = new List <AuditFileModel>(); try { using (var con = db.Gdb()) { string sql = $"SELECT * FROM AuditFile"; SqlCommand cmd = new SqlCommand(sql, con); cmd.CommandType = CommandType.Text; con.Open(); cmd.ExecuteNonQuery(); SqlDataReader rd = cmd.ExecuteReader(); while (rd.Read()) { AF = new AuditFileModel(); AF.timeSheetKey = Convert.ToString(rd["timeSheetKey"]); AF.day = Convert.ToString(rd["day"]); AF.field = Convert.ToString(rd["field"]); AF.oldValue = Convert.ToString(rd["oldValue"]); AF.newValue = Convert.ToString(rd["newValue"]); AF.dateOfChange = Convert.ToDateTime(rd["dateOfChange"]); AF.changedBy = Convert.ToString(rd["changedBy"]); AFList.Add(AF); } con.Close(); } } catch (Exception) { throw; } return(Ok(AFList)); }
public IActionResult EditUsersTimeSheet(List <EditTimeSheetModel> modelList) { using (var con = db.Gdb()) { string startTime = null; string endTime = null; string breaks = null; AuditFileModel auditFile = null; List <AuditFileModel> auditFileList = new List <AuditFileModel>(); foreach (EditTimeSheetModel item in modelList) { SqlCommand cmd = new SqlCommand("dbo.spGetTimeSheetRecordById", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", item.id); con.Open(); SqlDataReader rd = cmd.ExecuteReader(); while (rd.Read()) { startTime = Convert.ToString(rd["start_time"]); endTime = Convert.ToString(rd["end_time"]); breaks = Convert.ToString(rd["breaks"]); if (startTime != item.startTime && item.startTime != null) { auditFile = new AuditFileModel(); auditFile.timeSheetKey = item.timeSheetKey; auditFile.day = item.day; auditFile.field = "Start Time"; auditFile.oldValue = startTime; auditFile.newValue = item.startTime; auditFile.dateOfChange = DateTime.Now; auditFile.changedBy = item.changedBy; auditFileList.Add(auditFile); } if (endTime != item.endTime && item.endTime != null) { auditFile = new AuditFileModel(); auditFile.timeSheetKey = item.timeSheetKey; auditFile.day = item.day; auditFile.field = "End Time"; auditFile.oldValue = endTime; auditFile.newValue = item.endTime; auditFile.dateOfChange = DateTime.Now; auditFile.changedBy = item.changedBy; auditFileList.Add(auditFile); } if (breaks != item.breaks && item.breaks != null) { auditFile = new AuditFileModel(); auditFile.timeSheetKey = item.timeSheetKey; auditFile.day = item.day; auditFile.field = "Break"; auditFile.oldValue = breaks; auditFile.newValue = item.breaks; auditFile.dateOfChange = DateTime.Now; auditFile.changedBy = item.changedBy; auditFileList.Add(auditFile); } } con.Close(); DateTime StartTime = item.startTime != "-1" ? Convert.ToDateTime(item.startTime) : default; DateTime EndTime = item.endTime != "-1" ? Convert.ToDateTime(item.endTime) : default; var WorkTime = EndTime.Subtract(StartTime); var conv = WorkTime.ToString(@"h\:mm"); cmd = new SqlCommand("dbo.spEditTimeSheetById", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", item.id); cmd.Parameters.AddWithValue("@startTime", item.startTime); cmd.Parameters.AddWithValue("@endTime", item.endTime); cmd.Parameters.AddWithValue("@breaks", item.breaks); cmd.Parameters.AddWithValue("@workTime", conv); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } if (auditFileList.Count > 0) { foreach (AuditFileModel item in auditFileList) { SqlCommand cmd = new SqlCommand("dbo.spCreateAuditFile", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@timeSheetKey", item.timeSheetKey); cmd.Parameters.AddWithValue("@day", item.day); cmd.Parameters.AddWithValue("@field", item.field); cmd.Parameters.AddWithValue("@oldValue", item.oldValue); cmd.Parameters.AddWithValue("@newValue", item.newValue); cmd.Parameters.AddWithValue("@dateOfChange", item.dateOfChange); cmd.Parameters.AddWithValue("@changedBy", item.changedBy); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } return(Ok(auditFileList)); } }