[HttpPost("")] // ./api/customers public IActionResult Create([FromBody] CustomerCreateViewModel model) { if (model == null) { return(BadRequest()); } if (!ModelState.IsValid) { //rules failed, return a well formed error _logger.LogWarning("Customer Create failed due to validation"); return(new ValidationFailedResult(ModelState)); } var customer = new Customer { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, PhoneNumber = model.PhoneNumber, PreferredContactMethod = model.PreferredContactMethod, LastContactDate = DateTime.UtcNow }; _customerData.Add(customer); _customerData.Commit(); Response.Headers.Add("ETag", "\"" + customer.LastContactDate.ToString() + "\""); return(Ok(new CustomerDisplayViewModel(customer))); //includes new auto-assigned id }
public IActionResult Edit(CustomersEditViewModel model) { if (ModelState.IsValid) { var membership = membershipData.GetMembershipById(model.Customer.Id); model.Customer.Membership = membership; if (model.Customer.Id == 0) { model.Customer = customerData.Create(model.Customer); TempData["TempMessage"] = "New customer is created!"; } else { model.Customer = customerData.Update(model.Customer); TempData["TempMessage"] = "Data for customer is updated!"; } customerData.Commit(); return(RedirectToAction("List")); } var memberships = membershipData.GetMemberships().ToList().Select(p => new { Id = p.Id, Display = p.MembershipType }); model.Memberships = new SelectList(memberships, "Id", "Display"); model.Gender = htmlHelper.GetEnumSelectList <Gender>(); model.City = htmlHelper.GetEnumSelectList <City>(); return(View(model)); }
public IActionResult Create(CustomerDto customer) { var dbCustomer = new Customer(); dbCustomer.FirstName = customer.FirstName; dbCustomer.LastName = customer.LastName; dbCustomer.MembershipId = customer.MembershipId; customerData.Create(dbCustomer); customerData.Commit(); return(CreatedAtRoute("FetchCustomer", new { id = dbCustomer.Id }, dbCustomer)); }
public IActionResult OnPost() { if (ModelState.IsValid) { var membership = membershipData.GetMembershipById(Customer.MembershipId); Customer.Membership = membership; if (Customer.Id == 0) { Customer = customerData.Create(Customer); TempData["TempMessage"] = "New customer is created!"; } else { Customer = customerData.Update(Customer); TempData["TempMessage"] = "Data for customer is updated!"; } customerData.Commit(); return(RedirectToPage("./List")); } var memberships = membershipData.GetMemberships().ToList().Select(p => new { Id = p.Id, Display = p.MembershipType }); Memberships = new SelectList(memberships, "Id", "Display"); Gender = htmlHelper.GetEnumSelectList <Gender>(); City = htmlHelper.GetEnumSelectList <City>(); return(Page()); }
public IActionResult OnPost() { if (ModelState.IsValid) { var membership = membershipData.GetMebershipById(Customer.MembershipId); Customer.Membership = membership; if (Customer.Id == 0) { Customer = customerData.Create(Customer); TempData["Message"] = "The Object is created!"; } else { Customer = customerData.Update(Customer); TempData["Message"] = "The Object is updated!"; } customerData.Commit(); return(RedirectToPage("./List")); } var memberships = membershipData.GetMemberships().ToList().Select(p => new { Id = p.Id, Display = p.GetMembershipType() }); Memberships = new SelectList(memberships, "Id", "Display"); return(Page()); }
[HttpPost("")] // ./api/customers public IActionResult Create([FromBody] CustomerCreateViewModel model) { var customer = new Customer { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, PhoneNumber = model.PhoneNumber, PreferredContactMethod = model.PreferredContactMethod, LastContactDate = DateTime.UtcNow }; _customerData.Add(customer); _customerData.Commit(); Response.Headers.Add("ETag", "\"" + customer.LastContactDate.ToString() + "\""); return(Ok(new CustomerDisplayViewModel(customer))); }
public IActionResult Post([FromBody] Customer customer) { customerData.Add(customer); customerData.Commit(); // This does the same as our responsemessage extension return(CreatedAtRoute("GetCustomer", new { id = customer.Id }, customer)); }
public IActionResult Create(CustomerEditViewModel model) { if (ModelState.IsValid) { //the validation attributes all pass var customer = new Customer { FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, OptInNewsletter = model.OptInNewsletter, Type = model.Type }; _customerData.Add(customer); _customerData.Commit(); return(RedirectToAction(nameof(Details), new { id = customer.Id })); } //one or more validations did not pass. return(View()); }
public IActionResult OnPost(int id) { var temp = customerData.Delete(id); if (temp == null) { return(RedirectToPage("./NotFound")); } customerData.Commit(); TempData["TempMessage"] = "The customer is deleted!"; return(RedirectToPage("./List")); }
[HttpPost("")] // ./api/customers public IActionResult Create([FromBody] CustomerCreateViewModel model) { if (model == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new ValidationFailedResult(ModelState)); } var customer = new Customer { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, PhoneNumber = model.PhoneNumber, PreferredContactMethod = model.PreferredContactMethod }; _customerData.Add(customer); _customerData.Commit(); return(Ok(new CustomerDisplayViewModel(customer))); //includes new auto-assigned id }
public async Task <IActionResult> Create(CustomerModel customerModel) { if (ModelState.IsValid) { var newCustomer = new Customer(); newCustomer.Name = customerModel.Name; newCustomer.Email = customerModel.Email; newCustomer.PrimaryPhone = customerModel.PrimaryPhone; newCustomer.AlternatePhone = customerModel.AlternatePhone; newCustomer.Address1 = customerModel.Address1; newCustomer.Address2 = customerModel.Address2; newCustomer.Address3 = customerModel.Address3; newCustomer.City = customerModel.City; newCustomer.State = customerModel.State; newCustomer.Country = customerModel.Country; newCustomer.Zip = customerModel.Zip; newCustomer = _customerdata.Add(newCustomer); _customerdata.Commit(); return(RedirectToAction("Index")); } return(View()); }