Пример #1
0
 public static Customer MapCustomerCommandDtoToModel(CustomerCommandDTO customerDto)
 {
     return(new Customer
     {
         FirstName = customerDto.FirstName,
         LastName = customerDto.LastName,
         DateOfBirth = customerDto.DateOfBirth
     });
 }
Пример #2
0
        public async Task <ActionResult> CreateCustomerAsync([FromBody] CustomerCommandDTO customerDto)
        {
            if (customerDto is null)
            {
                return(BadRequest());
            }

            var customer = CustomerMapper.MapCustomerCommandDtoToModel(customerDto);

            await _context.Customers.AddAsync(customer);

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetCustomerByIdAsync), new { id = customer.Id }, null));
        }
Пример #3
0
        public async Task <ActionResult> UpdateCustomerAsync(int id, [FromBody] CustomerCommandDTO customerDto)
        {
            if (id < 1 || customerDto is null)
            {
                return(BadRequest());
            }

            var customerToUpdate = await _context.Customers.FindAsync(id);

            if (customerToUpdate is null)
            {
                return(NotFound());
            }

            customerToUpdate.FirstName   = customerDto.FirstName;
            customerToUpdate.LastName    = customerDto.LastName;
            customerToUpdate.DateOfBirth = customerDto.DateOfBirth;

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetCustomerByIdAsync), new { id = customerToUpdate.Id }, null));
        }