public JsonResult AddSurgery()
        {
            try
            {
                //Repositories
                PatientRepository patientRepo = new PatientRepository();
                UserRepository userRepo = new UserRepository();
                LocationRepository locationRepo = new LocationRepository();
                SurgeryStaffRepository ssRepo = new SurgeryStaffRepository();

                //Build new objects
                Surgery surgery = new Surgery();
                Note note = new Note();

                //Get patient object
                int patientID = int.Parse(Request.Form["patientID"]);
                Patient patient = patientRepo.Get(patientID);

                //Get current open patient checkin
                var query = from checkin in patient.PatientCheckIns
                            where checkin.CheckOutTime == DateTime.MinValue
                            select checkin;
                PatientCheckIn openCheckIn = query.First<PatientCheckIn>();

                surgery.Location = locationRepo.Get(int.Parse(Request.Form["theatreNumber"]));
                surgery.StartTime = DateTime.Parse(Request.Form["startTime"]);
                surgery.EndTime = DateTime.Parse(Request.Form["endTime"]);

                //Add to checkin
                openCheckIn.Surgeries.Add(surgery);
                surgery.CheckIn = openCheckIn;
                surgery.CaseType = (CaseType)Enum.Parse(typeof(CaseType), Request.Form["caseType"]);

                //Build Note
                User author = userRepo.Get(int.Parse(Request.Form["staffID"]));
                note.Author = author;
                note.Body = Request.Form["NoteBody"];
                note.PatientCheckIns = openCheckIn;
                note.Title = "";
                note.Type = NoteType.Surgery;
                note.IsActive = true;
                note.DateCreated = DateTime.Now;
                openCheckIn.Notes.Add(note);

                UnitOfWork.CurrentSession.Flush();

                //Surgeon
                if (Request.Form["surgeon"] != "")
                {
                    SurgeryStaff surgeon = new SurgeryStaff();
                    surgeon.Staff = userRepo.Get(int.Parse(Request.Form["surgeon"]));
                    surgeon.Surgery = surgery;
                    surgeon.Role = StaffRole.Surgeon;
                    ssRepo.Add(surgeon);
                }

                //Surgeon Assistant
                if (Request.Form["surgeonAssistant"] != "")
                {
                    SurgeryStaff surgeonAssistant = new SurgeryStaff();
                    surgeonAssistant.Staff = userRepo.Get(int.Parse(Request.Form["surgeonAssistant"]));
                    surgeonAssistant.Surgery = surgery;
                    surgeonAssistant.Role = StaffRole.SurgeonAssistant;
                    ssRepo.Add(surgeonAssistant);

                }
                //Anaesthetist
                if (Request.Form["anaesthetist"] != "")
                {
                    SurgeryStaff anaesthetist = new SurgeryStaff();
                    anaesthetist.Staff = userRepo.Get(int.Parse(Request.Form["anaesthetist"]));
                    anaesthetist.Role = StaffRole.Anaesthetist;
                    anaesthetist.Surgery = surgery;
                    ssRepo.Add(anaesthetist);
                }
                //Anaesthetist Assistant
                if (Request.Form["anaesthetistAssistant"] != "")
                {
                    SurgeryStaff anaesthetistAssistant = new SurgeryStaff();
                    anaesthetistAssistant.Staff = userRepo.Get(int.Parse(Request.Form["anaesthetistAssistant"]));
                    anaesthetistAssistant.Role = StaffRole.AnaesthetistAssistant;
                    anaesthetistAssistant.Surgery = surgery;
                    ssRepo.Add(anaesthetistAssistant);
                }
                //Nurse
                if (Request.Form["nurse"] != "")
                {
                    SurgeryStaff nurse = new SurgeryStaff();
                    nurse.Staff = userRepo.Get(int.Parse(Request.Form["nurse"]));
                    nurse.Role = StaffRole.Nurse;
                    nurse.Surgery = surgery;
                    ssRepo.Add(nurse);
                }
                //Consultant
                if (Request.Form["consultant"] != "")
                {
                    SurgeryStaff consultant = new SurgeryStaff();
                    consultant.Staff = userRepo.Get(int.Parse(Request.Form["consultant"]));
                    consultant.Role = StaffRole.Consultant;
                    consultant.Surgery = surgery;
                    ssRepo.Add(consultant);
                }

                //Save Template
                if (Request.Form["NoteTitle"] != null)
                {
                    TemplateRepository templateRepo = new TemplateRepository();
                    NoteTemplateRepository noteRepo = new NoteTemplateRepository();
                    NoteTemplateCategory noteCat = noteRepo.Get(2);
                    Template template = new Template();
                    template.Title = Request.Form["NoteTitle"];
                    template.Staff = author;
                    template.Body = note.Body;
                    template.IsActive = true;
                    template.NoteTemplateCategory = noteCat;
                    templateRepo.Add(template);
                    return Json(new
                    {
                        Id = template.Id,
                        Name = template.Title,
                        NoteBody = note.Body,
                        error = "false"
                    });
                }
                return Json(new
                {
                    error = "false"
                });
            }
            catch (Exception e)
            {
                return Json(new
                {
                    error = "true",
                    status = e.Message
                });
            }
        }
 /// <summary>
 /// Remove a template from the database
 /// </summary>
 /// <param name="entity">template to remove</param>
 public void Remove(Template entity)
 {
     Session.Delete(entity);
 }
        public JsonResult AddNote()
        {
            try
            {
                PatientRepository patientRepo = new PatientRepository();
                UserRepository userRepo = new UserRepository();
                User staff = new User();

                if (Request.Form["StaffId"] != "")
                    staff = userRepo.Get(int.Parse(Request.Form["StaffId"]));

                Patient patient = patientRepo.Get(int.Parse(Request.Form["PatientId"]));

                Note note = new Note();

                var query = from checkin in patient.PatientCheckIns
                            where checkin.CheckOutTime == DateTime.MinValue
                            select checkin;
                PatientCheckIn openCheckIn = query.First<PatientCheckIn>();
                note.Author = staff;
                note.DateCreated = DateTime.Now;
                note.Body = Request.Form["NoteBody"]; //HttpUtility.UrlDecode(Request.Form["NoteBody"], System.Text.Encoding.Default);
                note.PatientCheckIns = openCheckIn;
                note.Title = "";
                note.Type = NoteType.General;
                note.IsActive = true;
                openCheckIn.Notes.Add(note);

                if (Request.Form["TemplateTitle"] != null && Request.Form["TemplateTitle"] != "")
                {
                    TemplateRepository templateRepo = new TemplateRepository();
                    NoteTemplateRepository noteRepo = new NoteTemplateRepository();
                    NoteTemplateCategory noteCat = noteRepo.Get(1);
                    Template template = new Template();
                    template.Title = Request.Form["TemplateTitle"];
                    template.Staff = staff;
                    template.Body = note.Body;
                    template.IsActive = true;
                    template.NoteTemplateCategory = noteCat;
                    templateRepo.Add(template);
                    return Json(new
                    {
                        templateId = template.Id,
                        templateTitle = template.Title,
                        NoteBody = note.Body,
                        error = "false"
                    });
                }
                return Json(new
                {
                    NoteBody = note.Body,
                    error = "false"
                });
            }
            catch
            {
                return Json(new
                {
                    error = "true"
                });
            }
        }
 /// <summary>
 /// Add a template to the database
 /// </summary>
 /// <param name="entity">template to add</param>
 public void Add(Template entity)
 {
     Session.Save(entity);
 }