public HttpResponseMessage Update([FromBody] Customers entity) { try { using (var db = new InsuranceDBEntities()) { var customer = db.Customers.FirstOrDefault(c => c.CustomerID == entity.CustomerID); var auto = db.Autos.FirstOrDefault(c => c.AutoID == entity.AutoID); var address = db.Addresses.FirstOrDefault(c => c.AddressID == entity.AddressID); var contact = db.Contacts.FirstOrDefault(c => c.ContactID == entity.ContactID); customer.Autos = auto; customer.Addresses = address; customer.Contacts = contact; if (customer != null) { customer.First_Name = entity.First_Name; customer.Last_Name = entity.Last_Name; customer.Civility = entity.Civility; customer.Birthdate = entity.Birthdate; customer.Sexe = entity.Sexe; customer.Autos.Year = entity.Autos.Year; customer.Autos.Marque = entity.Autos.Marque; customer.Autos.Model = entity.Autos.Model; customer.Autos.Vehicle_Nbre = entity.Autos.Vehicle_Nbre; customer.Autos.IsLeqsedOrFinanced = entity.Autos.IsLeqsedOrFinanced; customer.Autos.Vehicle_Use = entity.Autos.Vehicle_Use; customer.Autos.KilometersPerYear = entity.Autos.KilometersPerYear; customer.Autos.Infraction_Nbre = entity.Autos.Infraction_Nbre; customer.Autos.Accident_Nbre = entity.Autos.Accident_Nbre; customer.Autos.Claim_Type = entity.Autos.Claim_Type; customer.Autos.Date_Of_Claim = entity.Autos.Date_Of_Claim; customer.Autos.Estimation_Per_Month = BusinessAuto.CalculAutoSoumission(entity.Autos); //Address customer.Addresses.AddressLine = entity.Addresses.AddressLine; customer.Addresses.PostalCode = entity.Addresses.PostalCode; customer.Addresses.City = entity.Addresses.City; customer.Addresses.Country = entity.Addresses.Country; //Contact customer.Contacts.Home_Phone = entity.Contacts.Home_Phone; customer.Contacts.Mobile_Phone = entity.Contacts.Mobile_Phone; customer.Contacts.Email_Address = entity.Contacts.Email_Address; customer.Contacts.Contact_Preferences = entity.Contacts.Contact_Preferences; db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } else { return(Request.CreateErrorResponse(HttpStatusCode.Conflict, "The customer name is already exist")); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
// Post client avec la police auto public ActionResult SaveAutoPolicy(ContactMapping contactMapping) { //Récuperer customer + auto via la variable session Customers customer = (Customers)Session["Customers"]; //Calculer le prix de la soumission double soumission_price = BusinessAuto.CalculAutoSoumission(customer.Autos); customer.Autos.Estimation_Per_Month = soumission_price; //Récuperer contact Contacts contacts = BusinessAuto.ContactMapping(contactMapping); //Récuperer adresse Addresses addresses = BusinessAuto.AddressesMapping(contactMapping); //Customer + auto + Adresse + Contact customer.Addresses = addresses; customer.Contacts = contacts; TempData["Price"] = soumission_price; using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://localhost:44365/api/"); var responseTask = client.PostAsJsonAsync <Customers>("customers", customer); responseTask.Wait(); var results = responseTask.Result; if (results.IsSuccessStatusCode) { var readJob = results.Content.ReadAsAsync <Customers>(); readJob.Wait(); return(RedirectToAction("Confirme")); } else { ModelState.AddModelError(string.Empty, "An error is occured during the processing"); return(RedirectToAction("SaveAutoPolicy")); } } }