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>
        /// Get the surgury staff for a certain surgery
        /// </summary>
        /// <param name="surgery">sugery to get the staff for</param>
        /// <returns></returns>
        private IList<object> GetStaff(Surgery surgery)
        {
            IList<object> result = new List<object>();
            foreach (var staff in surgery.Staff)
            {
                result.Add(new
                {
                    Role = Enum.GetName(typeof(StaffRole), staff.Role),
                    Name = staff.Staff.FirstName + " " + staff.Staff.LastName

                });
            }
            return result;
        }