Exemple #1
0
        public async Task <IActionResult> CreateCustomer(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (customer == null)
            {
                return(BadRequest("Customer data is empty"));
            }

            if (customer.CustomerID == null || customer.CustomerID?.Length == 0)
            {
                return(BadRequest("Customer ID is empty"));
            }

            var query = _dbContext.Customers.Where(c => c.CustomerID == customer.CustomerID);

            if (query.Any())
            {
                return(BadRequest("Customer exists"));
            }

            var customerEntity = Mapper.Map <Customer, CustomerEntity>(customer);

            var entry = await _dbContext.AddAsync(customerEntity);

            await _dbContext.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetByCustomerId), new { id = customer.CustomerID }, customer));
        }