public async Task <IActionResult> CreateCustomer( [FromBody] Models.CustomersForCreation customersForCreation) { if (customersForCreation == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } customersForCreation.GenerateCustomerId(); var customerEntity = _mapper.Map <Customers>(customersForCreation); _customersRepository.AddCustomer(customerEntity); await _customersRepository.SaveChanges(); await _customersRepository.GetCustomer(customerEntity.CustomerId); return(Ok(CreatedAtRoute("GetCustomer", new { customerId = customerEntity.CustomerId }, _mapper.Map <Models.Customers>(customerEntity)))); }
public async Task <IActionResult> Create([Bind("CustomerId,SocSecNumber,FirstName,LastName")] Customer customer) { if (ModelState.IsValid) { customer.CustomerId = Guid.NewGuid(); _customersRepository.AddCustomer(customer); return(RedirectToAction(nameof(Index))); } return(View(customer)); }
public async Task <ActionResult <Customer> > CreateCustomer(Customer customer) { try { if (customer == null) { return(BadRequest()); } var createdCustomer = await customerRepository.AddCustomer(customer); return(CreatedAtAction(nameof(GetCustomer), new { id = createdCustomer.Id }, createdCustomer)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from database")); } }
public async Task <ActionResult> AddCustomer([Bind(Include = "Name, BillingAddress")] Customer customer) { if (ModelState.IsValid) { try { await _customersRepository.AddCustomer(customer); } catch (Exception ex) { throw new HttpException("Unable to Add Customer. " + ex); } return(RedirectToAction("Create")); } ModelState.AddModelError("Customer", "Cannot add this customer."); return(View("Create")); }
public async Task <ActionResult <Customers> > CreateCustomer([FromBody] Customers customer) { try { if (customer == null) { return(BadRequest()); } var customerToCreate = await customersRepository.AddCustomer(customer); return(CreatedAtAction(nameof(GetCustomer), new { email = customerToCreate.Email }, customerToCreate)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving creating a new customer")); } }
public ActionResult Create(CustomerModel customerToAdd) { if (!ModelState.IsValid) { return(View(customerToAdd)); } var mappedCustomer = new Customer { Id = customerToAdd.Id, Firstname = customerToAdd.Firstname, Lastname = customerToAdd.Lastname, PhoneNumber = customerToAdd.PhoneNumber, SecondPhoneNumber = customerToAdd.SecondPhoneNumber, Address = customerToAdd.Address }; _service.AddCustomer(mappedCustomer); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> CreateCustomer([FromBody] CustomerForCreation customer) { var customerEntity = _mapper.Map <Customer>(customer); var search = _customersRepository.CheckCustomerExists(customerEntity); if (search != null) { return(BadRequest("Customer already exists")); } _customersRepository.AddCustomer(customerEntity); await _customersRepository.SaveChangesAsync(); await _customersRepository.GetCustomerAsync(customerEntity.Id); return(CreatedAtRoute("GetCustomer", new { id = customerEntity.Id }, customerEntity)); }
private void AddCustomer(Customer customer) { customer.CreatedAt = DateTime.Now; customer.UpdatedAt = DateTime.Now; _customersRepository.AddCustomer(customer); }