//Update a referral
        public void Update(int DoctorId, FormCollection Collection, int ReferralId)
        {
            Referral referral;

            using (var db = new ReferralContext())
            {
                db.Configuration.ProxyCreationEnabled = false;
                referral = db.Referrals.First(x => x.Doctor.UserProfileId.Equals(WebSecurity.CurrentUserId) && x.Id.Equals(ReferralId));
                db.Entry(referral).State = EntityState.Modified;
                referral.City            = Collection["City"];
                referral.State           = Collection["State"];
                referral.Date            = DateTime.Parse(Collection["Date"]);
                referral.Note            = Collection["Notes"];
                referral.Reason          = db.Reason.Find(Convert.ToInt32(Collection["Reason"]));

                if (referral.DoctorId != DoctorId)
                {
                    referral.Doctor = db.Doctor.Where(x => x.Id.Equals(DoctorId)).First();
                    db.SaveChanges();
                }
                else
                {
                    //   db.Referrals.Attach(referral);
                    db.SaveChanges();
                }
            }
        }
        //Create a new referral
        public void Create(int DoctorId, FormCollection Collection)
        {
            Referral       referral;
            Patient        patient;
            Doctor         doctor;
            List <Patient> patients;
            string         patientEmail = Collection["PatientEmail"];

            using (var db = new ReferralContext())
            {
                db.Configuration.ProxyCreationEnabled = false;
                referral       = db.Referrals.Create();
                referral.City  = Collection["City"];
                referral.State = Collection["State"];
                referral.Date  = DateTime.Parse(Collection["Date"]);
                referral.Note  = Collection["Notes"];
                int reasonId = Convert.ToInt32(Collection["Reason"]);
                referral.Reason = db.Reason.FirstOrDefault(x => x.Id.Equals(reasonId));
                db.Entry(referral.Reason).State = EntityState.Unchanged;

                //adding patient
                patients = db.Patient.Where(x => x.Email.Equals(patientEmail) && x.UserProfileId.Equals(WebSecurity.CurrentUserId)).ToList();
                if (patients.Count < 1)
                {
                    patient                = new Patient();
                    patient.First_name     = Collection["Name"];
                    patient.Middle_initial = Collection["Middle_initial"];
                    patient.Last_name      = Collection["Last_name"];
                    patient.Email          = Collection["PatientEmail"];
                    patient.UserProfileId  = WebSecurity.CurrentUserId;
                    referral.Patient       = patient;
                }
                else
                {
                    referral.Patient = patients[0];
                    db.Entry(referral.Patient).State = EntityState.Unchanged;
                }

                doctor          = db.Doctor.Find(DoctorId);
                referral.Doctor = doctor;
                db.Referrals.Add(referral);
                db.SaveChanges();
                //   DoctorsRepository.AddReferral(DoctorId, referral);
            }
        }
Beispiel #3
0
        public void CreateReferral(int DoctorId, FormCollection Collection)
        {
            Referral       newReferral;
            Patient        Patient;
            string         patientEmail = Collection["PatientEmail"];
            List <Patient> patients;
            Doctor         TargetDoctor;
            int            reasonId;

            using (var db = new ReferralContext())
            {
                db.Configuration.ProxyCreationEnabled = false;
                reasonId    = Convert.ToInt32(Collection["Reason"]);
                Patient     = new Patient();
                newReferral = db.Referrals.Create();

                newReferral.Date  = DateTime.Parse(Collection["Date"]);
                newReferral.Note  = Collection["Note"];
                newReferral.City  = Collection["City"];
                newReferral.State = Collection["State"];
                //Adding patient
                patients = db.Patient.Where(x => x.Email.Equals(patientEmail) && x.UserProfileId.Equals(WebSecurity.CurrentUserId)).ToList();
                if (patients.Count < 1)
                {
                    Patient                = new Patient();
                    Patient.First_name     = Collection["Name"];
                    Patient.Middle_initial = Collection["Middle_initial"];
                    Patient.Last_name      = Collection["Last_name"];
                    Patient.Email          = Collection["PatientEmail"];
                    Patient.UserProfileId  = WebSecurity.CurrentUserId;
                    newReferral.Patient    = Patient;
                }
                else
                {
                    newReferral.Patient = patients[0];
                    db.Entry(newReferral.Patient).State = EntityState.Unchanged;
                }

                //Adding reason
                newReferral.ReasonId = reasonId;

                TargetDoctor = db.Doctor.Include("Referrals").First(x => x.Id.Equals(DoctorId));
                TargetDoctor.Referrals.Add(newReferral);
                db.Referrals.Add(newReferral);
                db.SaveChanges();
                //save db changes
                //AddReferral(DoctorId, newReferral);
            }
        }