public JsonResult ActivityHistory(int?patientId) { ActivityHistoryCurrData actHistory = null; try { actHistory = clinicService.GetCurrActivityHistory(patientId); } catch (Exception ex) { //TODO: Handle exception } return(Json(actHistory, JsonRequestBehavior.AllowGet)); }
public ActivityHistoryCurrData GetCurrActivityHistory(int?patientId) { List <ActivityHistoryItem> currMonthData = new List <ActivityHistoryItem>(); List <ActivityHistoryItem> currWeekData = null; DataSet dataset = new DataSet(); DataTable dtActHistory; DateTime monthStartDate, monthEndDate, weekStartDate, weekEndDate, currDate; ActivityHistoryCurrData response = null; currDate = DateTime.Now; weekStartDate = currDate.AddDays(-(int)currDate.DayOfWeek).Date; weekEndDate = weekStartDate.AddDays(7).AddSeconds(-1); monthStartDate = new DateTime(currDate.Year, currDate.Month, 1); monthEndDate = monthStartDate.AddMonths(1).AddDays(-1); monthEndDate = (currDate.Day <= monthEndDate.Day) ? currDate : monthEndDate; weekEndDate = (currDate.Day <= weekEndDate.Day) ? currDate : weekEndDate; using (SqlConnection conn = new SqlConnection(connString)) { SqlCommand cmd = new SqlCommand("dashboard_get_activity_history", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@fromDate", SqlDbType.DateTime).Value = monthStartDate; cmd.Parameters.Add("@toDate", SqlDbType.DateTime).Value = monthEndDate; if (patientId != null) { cmd.Parameters.Add("@patientId", SqlDbType.NVarChar).Value = patientId.Value; } SqlDataAdapter adapter = new SqlDataAdapter(cmd); cmd.Connection = conn; adapter.Fill(dataset); dtActHistory = dataset.Tables[0]; foreach (DataRow row in dtActHistory.Rows) { currMonthData.Add(new ActivityHistoryItem { TotalOpens = Convert.ToInt32(row["TotalOpen"]), TotalSent = Convert.ToInt32(row["TotalSent"]), Date = Convert.ToDateTime(row["CreatedOn"]) }); } } if (currMonthData != null) { currWeekData = currMonthData.FindAll(a => a.Date >= weekStartDate && a.Date <= weekEndDate); } response = new ActivityHistoryCurrData(); response.CurrentWeekData = new ActivityHistoryData { Data = currWeekData, StartDate = weekStartDate, EndDate = weekEndDate }; response.CurrentMonthData = new ActivityHistoryData { Data = currMonthData, StartDate = monthStartDate, EndDate = monthEndDate }; return(response); }