public IHttpActionResult CreatePatients(Patient patient)
        {
            using (var db = new WebApiDemoDb1Entities())
            {
                if (db.Dentists.Any(p => p.Email == patient.Email || p.Phone == patient.Phone))
                {
                    return(BadRequest("Patient record already exists"));
                }

                if (!ModelState.IsValid)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                var dbPatient = patient.ToDatabase();
                db.Patients.Add(dbPatient);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    throw;
                }
            }
            return(Ok("Successfully Created Patient Record!"));
        }
Example #2
0
        public IHttpActionResult UpdateDentist(int id, Dentist dentist)
        {
            using (var db = new WebApiDemoDb1Entities())
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var dbDentist = db.Dentists.SingleOrDefault(x => x.Id == id);
                if (dbDentist == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                dbDentist.FirstName = dentist.FirstName;
                dbDentist.LastName  = dentist.LastName;
                dbDentist.Email     = dentist.Email;
                dbDentist.Phone     = dentist.Phone;

                db.SaveChanges();

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    throw;
                }
            }
            return(Ok("Dentist Record Updated Successfully!"));
        }
Example #3
0
        public IHttpActionResult CreateDentist(Dentist dentist)
        {
            using (var db = new WebApiDemoDb1Entities())
            {
                if (!ModelState.IsValid)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                if (db.Dentists.Any(d => d.Phone == dentist.Phone || d.Email == dentist.Email))
                {
                    return(BadRequest("Record already exists!"));
                }

                var dbDentist = dentist.ToDatabase();
                db.Dentists.Add(dbDentist);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    throw;
                }
            }
            return(Ok("Dentist Reord Created Successfully!"));
        }
Example #4
0
        public IHttpActionResult GetDentist(int id)
        {
            using (WebApiDemoDb1Entities db = new WebApiDemoDb1Entities())
            {
                var dbDentist = db.Dentists.SingleOrDefault(d => d.Id == id);

                if (dbDentist == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                return(Ok(dbDentist.ToContract()));
            }
        }
        public IEnumerable <Patient> GetPatientsByDentistId(int id)
        {
            List <Patient> patients = new List <Patient>();

            using (WebApiDemoDb1Entities db = new WebApiDemoDb1Entities())
            {
                var dbPatient = db.Patients.Where(p => p.DentistId == id).ToList();

                if (dbPatient.Count == 0)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                foreach (var row in dbPatient)
                {
                    patients.Add(row.ToContract());
                    row.ToContract();
                }
            }
            return(patients);
        }
Example #6
0
        public IEnumerable <Dentist> GetDentists()
        {
            List <Dentist> dentists = new List <Dentist>();

            using (WebApiDemoDb1Entities db = new WebApiDemoDb1Entities())
            {
                var dbDentist = db.Dentists.ToList();

                if (dbDentist.Count == 0)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                foreach (var row in dbDentist)
                {
                    dentists.Add(row.ToContract());
                    row.ToContract();
                }

                return(dentists);
            }
        }
Example #7
0
        public IHttpActionResult DeleteDentist(int id)
        {
            using (var db = new WebApiDemoDb1Entities())
            {
                var dbDentist = db.Dentists.SingleOrDefault(d => d.Id == id);
                if (dbDentist == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                var dbPatient = db.Patients.SingleOrDefault(p => p.DentistId == id);
                if (dbPatient != null)
                {
                    return(BadRequest("Patient Record exists for the Dentist. Update or delete the patient record"));
                }

                db.Dentists.Remove(dbDentist);
                db.SaveChanges();
            }

            return(Ok("Dentist record Deleted Successfully!"));
        }