コード例 #1
0
        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))));
        }
コード例 #2
0
 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));
 }
コード例 #3
0
        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"));
            }
        }
コード例 #4
0
 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"));
 }
コード例 #5
0
        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"));
            }
        }
コード例 #6
0
        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)));
        }
コード例 #7
0
        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));
        }
コード例 #8
0
 private void AddCustomer(Customer customer)
 {
     customer.CreatedAt = DateTime.Now;
     customer.UpdatedAt = DateTime.Now;
     _customersRepository.AddCustomer(customer);
 }