//Filtering for Normal Appointments
 public void filterRecord(int length, string property, string value, string value2, ref PatientDiagnosisHistoryMaster[] diagnosisHistoryMaster)
 {
     /* Fields that can be filter
      * Patient First Name
      * Patient Middle Name
      * Patient Last Name
      * Appointment Date
      * Appointment From Time
      * Appointment To Time
      */
     //Filter for a specific patient
     int fetch;
     diagnosisHistoryMaster = null;
     if (property.Equals("AppointmentDate"))
     {
         StringManipulation strManipulate = new StringManipulation(value, value2, "Date");
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.ScheduleMaster.Date >= strManipulate.dateValue && pdhm.Appointment.ScheduleMaster.Date <= strManipulate.dateValue2)
                                                               && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster.UserInformation)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.ScheduleMaster.Date >= strManipulate.dateValue && pdhm.Appointment.ScheduleMaster.Date <= strManipulate.dateValue2)
                                                     && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleMaster.Date).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     else if (property.Equals("ScheduledFromTime"))
     {
         StringManipulation strManipulate = new StringManipulation(value, value2, "Time");
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.ScheduleDetail.FromTime >= strManipulate.timeValue && pdhm.Appointment.ScheduleDetail.ToTime <= strManipulate.timeValue2)
                                                               && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.ScheduleDetail.FromTime >= strManipulate.timeValue && pdhm.Appointment.ScheduleDetail.ToTime <= strManipulate.timeValue2)
                                                     && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleMaster.Date).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     else if (property.Equals("ScheduledToTime"))
     {
         StringManipulation strManipulate = new StringManipulation(value, value2, "Time");
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.ScheduleDetail.FromTime >= strManipulate.timeValue && pdhm.Appointment.ScheduleDetail.ToTime <= strManipulate.timeValue2)
                                                               && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster.UserInformation)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.ScheduleDetail.FromTime >= strManipulate.timeValue && pdhm.Appointment.ScheduleDetail.ToTime <= strManipulate.timeValue2)
                                                     && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleMaster.Date).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     else if (property.Equals("PatientFirstName"))
     {
         value = value.ToLower();
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.User.UserInformations.FirstOrDefault().FirstName.ToLower().Contains(value)
                                                   || pdhm.User.UserInformations.FirstOrDefault().FirstName.ToLower().Equals(value))
                                                   && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.User.UserInformations.FirstOrDefault().FirstName.ToLower().Contains(value)
                                 || pdhm.Appointment.User.UserInformations.FirstOrDefault().FirstName.ToLower().Equals(value))
                                 && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleDetail.FromTime).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     else if (property.Equals("PatientLastName"))
     {
         value = value.ToLower();
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.User.UserInformations.FirstOrDefault().LastName.ToLower().Contains(value)
                                                   || pdhm.Appointment.User.UserInformations.FirstOrDefault().LastName.ToLower().Equals(value))
                                                   && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.User.UserInformations.FirstOrDefault().LastName.ToLower().Contains(value)
                                 || pdhm.Appointment.User.UserInformations.FirstOrDefault().LastName.ToLower().Equals(value))
                                 && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleDetail.FromTime).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     else if (property.Equals("PatientMiddleName"))
     {
         value = value.ToLower();
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.User.UserInformations.FirstOrDefault().MiddleName.ToLower().Contains(value)
                                                   || pdhm.Appointment.User.UserInformations.FirstOrDefault().MiddleName.ToLower().Equals(value))
                                                   && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster.UserInformation)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.User.UserInformations.FirstOrDefault().MiddleName.ToLower().Contains(value)
                                 || pdhm.Appointment.User.UserInformations.FirstOrDefault().MiddleName.ToLower().Equals(value))
                                 && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleDetail.FromTime).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     else if (property.Equals("DentistFirstName"))
     {
         value = value.ToLower();
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.ScheduleMaster.UserInformation.FirstName.ToLower().Contains(value)
                                                   || pdhm.Appointment.ScheduleMaster.UserInformation.FirstName.ToLower().Equals(value))
                                                   && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.ScheduleMaster.UserInformation.FirstName.ToLower().Contains(value)
                                 || pdhm.Appointment.ScheduleMaster.UserInformation.FirstName.ToLower().Equals(value))
                                 && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleDetail.FromTime).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     else if (property.Equals("DentistLastName"))
     {
         value = value.ToLower();
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.ScheduleMaster.UserInformation.LastName.ToLower().Contains(value)
                                                   || pdhm.Appointment.ScheduleMaster.UserInformation.LastName.ToLower().Equals(value))
                                                   && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.ScheduleMaster.UserInformation.LastName.ToLower().Contains(value)
                                 || pdhm.Appointment.ScheduleMaster.UserInformation.LastName.ToLower().Equals(value))
                                 && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleDetail.FromTime).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     //Dentist Middle Name
     else
     {
         value = value.ToLower();
         var records = db.PatientDiagnosisHistoryMasters.Where(pdhm => (pdhm.Appointment.ScheduleMaster.UserInformation.MiddleName.ToLower().Contains(value)
                                                   || pdhm.Appointment.ScheduleMaster.UserInformation.MiddleName.ToLower().Equals(value))
                                                   && pdhm.Status != 0).Count();
         if (records > length)
         {
             if ((records - length) > pageSize)
                 fetch = pageSize;
             else
                 fetch = records - length;
             var getDiagnosisHistoryMaster = db.PatientDiagnosisHistoryMasters
                 .Include(pdhm => pdhm.User.UserInformations)
                 .Include(pdhm => pdhm.Appointment.ScheduleMaster)
                 .Include(pdhm => pdhm.Appointment.ScheduleDetail)
                 .Where(pdhm => (pdhm.Appointment.ScheduleMaster.UserInformation.MiddleName.ToLower().Contains(value)
                                 || pdhm.Appointment.ScheduleMaster.UserInformation.MiddleName.ToLower().Equals(value))
                                 && pdhm.Status != 0).OrderByDescending(pdhm => pdhm.Appointment.ScheduleDetail.FromTime).Skip((length)).Take(fetch).ToArray();
             diagnosisHistoryMaster = getDiagnosisHistoryMaster;
         }
     }
     if (diagnosisHistoryMaster != null)
     {
         for (int i = 0; i < diagnosisHistoryMaster.Length; i++)
         {
             diagnosisHistoryMaster[i].User.Appointments = null;
             diagnosisHistoryMaster[i].User.Appointments = null;
             diagnosisHistoryMaster[i].User.Messages = null;
             diagnosisHistoryMaster[i].User.Messages1 = null;
             diagnosisHistoryMaster[i].User.Notifications = null;
             diagnosisHistoryMaster[i].User.PatientDentalHistories = null;
             diagnosisHistoryMaster[i].User.PatientDiagnosisHistoryMasters = null;
             diagnosisHistoryMaster[i].User.PatientMedicalHistories = null;
             diagnosisHistoryMaster[i].User.UserType = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleMaster.Appointments = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleMaster.ScheduleDetails = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleDetail.Appointments = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleDetail.ScheduleMaster = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleMaster.UserInformation.PatientMouths = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleMaster.UserInformation.ScheduleMasters = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleMaster.UserInformation.CivilStatu = null;
             diagnosisHistoryMaster[i].Appointment.ScheduleMaster.UserInformation.User = null;
             foreach (var ui in diagnosisHistoryMaster[i].User.UserInformations)
             {
                 ui.CivilStatu = null;
                 ui.PatientMouths = null;
                 ui.ScheduleMasters = null;
             }
         }
     }
 }
        public IHttpActionResult PostPatientDiagnosisHistoryMaster(PatientDiagnosisHistoryMaster patientDiagnosisHistoryMaster)
        {
            response.status = "FAILURE";
            if (!ModelState.IsValid)
            {
                response.message = "Bad request.";
                return Ok(response);
            }
            try
            {
                db.PatientDiagnosisHistoryMasters.Add(patientDiagnosisHistoryMaster);
                db.SaveChanges();
                response.status = "SUCCESS";
                response.objParam1 = patientDiagnosisHistoryMaster;
            }
            catch (Exception e)
            {
                response.message = e.InnerException.InnerException.Message.ToString();
            }

            return Ok(response);
        }
        public IHttpActionResult PutPatientDiagnosisHistoryMaster(int id, PatientDiagnosisHistoryMaster patientDiagnosisHistoryMaster)
        {
            response.status = "FAILURE";
            if (!ModelState.IsValid || id != patientDiagnosisHistoryMaster.Id)
            {
                response.message = "Bad request.";
                return Ok(response);
            }

            db.Entry(patientDiagnosisHistoryMaster).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
                response.status = "SUCCESS";
                response.objParam1 = patientDiagnosisHistoryMaster;
            }
            catch (Exception e)
            {
                if (!PatientDiagnosisHistoryMasterExists(id))
                {
                    response.message = "Diagnosis History doesn't exist.";
                }
                else
                {
                    response.message = e.InnerException.InnerException.Message.ToString();
                }
            }

            return Ok(response);
        }
 //Filtering for a specif user's Appointments
 public IHttpActionResult GetPatientDiagnosisHistoryMasters(int length, string property, string value, string value2)
 {
     PatientDiagnosisHistoryMaster[] diagnosisHistoryMaster = new PatientDiagnosisHistoryMaster[pageSize];
     this.filterRecord(length, property, value, value2, ref diagnosisHistoryMaster);
     if (diagnosisHistoryMaster != null)
         return Ok(diagnosisHistoryMaster);
     else
         return Ok();
 }