public HttpResponseMessage CreatePatient(Patient patient) { if (patient == null) { return new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest }; } else { try { // Add new patient in DataContext and save it in the database db.Patients.Add(patient); db.SaveChanges(); return new HttpResponseMessage { StatusCode = HttpStatusCode.Created }; } catch (Exception) { return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError }; } } }
public HttpResponseMessage UpdatePatient(int id, Patient patient) { if (patient == null || id != patient.Id) { return new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest }; } else { try { // retrieve the patient from the database Patient patientToUpdate = db.Patients.Where(x => x.Id == id).FirstOrDefault(); if (patientToUpdate != null) { patientToUpdate.Title = patient.Title; patientToUpdate.FirstName = patient.FirstName; patientToUpdate.LastName = patient.LastName; patientToUpdate.Age = patient.Age; patientToUpdate.NumberOfEmbryos = patient.NumberOfEmbryos; // save updated patient in the database db.SaveChanges(); return new HttpResponseMessage { StatusCode = HttpStatusCode.OK }; } else { return new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest }; } } catch (Exception) { return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError }; } } }