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()); }
[HttpPut("{id}")] // ./api/customers/:id public IActionResult Update(int id, [FromBody] CustomerUpdateViewModel model) { if (model == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new ValidationFailedResult(ModelState)); } var customer = _customerData.Get(id); if (customer == null) { return(NotFound()); } //update only editable properties from model customer.EmailAddress = model.EmailAddress; customer.FirstName = model.FirstName; customer.LastName = model.LastName; customer.PhoneNumber = model.PhoneNumber; customer.PreferredContactMethod = model.PreferredContactMethod; _customerData.Update(customer); _customerData.Commit(); return(Ok(new CustomerDisplayViewModel(customer))); //server version, updated per request }
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 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()); }
[HttpPut("{id}")] // ./api/customers/:id public IActionResult Update(int id, [FromBody] CustomerUpdateViewModel model) { var customer = _customerData.Get(id); if (customer == null) { _logger.LogWarning("Customer {0} not found", id); return(NotFound()); } string ifMatch = Request.Headers["If-Match"]; if (ifMatch != customer.LastContactDate.ToString()) { _logger.LogInformation("Customer update failed due to concurrency issue: {0}", id); return(StatusCode(422, "Customer has been changed by another user since it was loaded. Reload and try again.")); } // TODO: ensure User owns this customer record, else return 403. //update only editable properties from model customer.EmailAddress = model.EmailAddress; customer.FirstName = model.FirstName; customer.LastName = model.LastName; customer.PhoneNumber = model.PhoneNumber; customer.PreferredContactMethod = model.PreferredContactMethod; customer.LastContactDate = DateTime.UtcNow; // acting as a last date updated _customerData.Update(customer); _customerData.Commit(); return(Ok(customer)); //server version, updated per request }
public async Task <IActionResult> Put([FromBody] CustomerViewModel customerViewModel) { if (!ModelState.IsValid) { return(CustomResponse(ModelState)); } _customerData.Update(_mapper.Map <Customer>(customerViewModel)); await _uow.Commit(); return(Ok()); }
public IActionResult Update(int id, CustomerDto customer) { var dbCustomer = customerData.GetCustomerById(id); if (dbCustomer == null) { return(BadRequest()); } dbCustomer.FirstName = customer.FirstName; dbCustomer.LastName = customer.LastName; dbCustomer.MembershipId = customer.MembershipId; customerData.Update(dbCustomer); customerData.Commit(); return(NoContent()); }
public async Task <ActionResult> Put(int id, [FromBody] Customer customer) { if (customer?.Id != id) { return(BadRequest()); } var customerDapper = await _customerData.GetByIdAsync(id); if (customerDapper is null) { return(NotFound()); } _customerData.Update(customer); _unitOfWork.Commit(); return(NoContent()); }
[HttpPut("{id}")] // ./api/customers/:id public IActionResult Update(int id, [FromBody] CustomerUpdateViewModel model) { if (model == null) { return(BadRequest()); } if (!ModelState.IsValid) { //rules failed, return a well formed error _logger.LogWarning("Customer Update failed due to validation"); return(new ValidationFailedResult(ModelState)); } var customer = _customerData.Get(id); if (customer == null) { _logger.LogWarning("Customer {0} not found", id); return(NotFound()); } string ifMatch = Request.Headers["If-Match"]; if (ifMatch != customer.LastContactDate.ToString()) { _logger.LogInformation("Customer update failed due to concurrency issue: {0}", id); return(StatusCode(422, "This record has been updated by another user. Please refresh and try again.")); } //update only editable properties from model customer.EmailAddress = model.EmailAddress; customer.FirstName = model.FirstName; customer.LastName = model.LastName; customer.PhoneNumber = model.PhoneNumber; customer.PreferredContactMethod = model.PreferredContactMethod; customer.LastContactDate = DateTime.UtcNow; _customerData.Update(customer); _customerData.Commit(); return(Ok(customer)); //server version, updated per request }
public IActionResult Edit(CustomerEditViewModel model) { if (ModelState.IsValid) { var customer = _customerData.Get(model.Id); if (customer == null) { return(RedirectToAction(nameof(Index))); } customer.FirstName = model.FirstName; customer.LastName = model.LastName; customer.PhoneNumber = model.PhoneNumber; customer.OptInNewsletter = model.OptInNewsletter; customer.Type = model.Type; _customerData.Update(customer); return(RedirectToAction(nameof(Details), new { id = customer.Id })); } return(View()); }