Example #1
0
        public async Task <IActionResult> RegisterCustomer(CustomerRegistrationDto registrationDto)
        {
            Result <CustomerFirstName>   customerFirstNameOrError = CustomerFirstName.Create(registrationDto.FirstName);
            Result <CustomerLastName>    customerLastNameOrError  = CustomerLastName.Create(registrationDto.LastName);
            Result <CustomerDateOfBirth> customerDOBOrError       = CustomerDateOfBirth.Create(registrationDto.DateOfBirth);
            Result <Email> emailOrError = Email.Create(registrationDto.Email);
            Result <PolicyReferenceNumber> policyReferenceOrError = PolicyReferenceNumber.Create(registrationDto.PolicyReferenceNumber);
            var result = Result.Combine(customerFirstNameOrError,
                                        customerLastNameOrError,
                                        customerDOBOrError,
                                        emailOrError,
                                        policyReferenceOrError);

            if (result.IsFailure)
            {
                return(BadRequest(result.Error));
            }
            var customerEntity = _mapper.Map <Customer>(registrationDto);
            await _customerRepository.AddItemAsync(customerEntity);

            await _customerRepository.SaveAsync();

            var customerToReturn = _mapper.Map <CustomerDto>(customerEntity);

            _logger.LogInformation($"a customer id {customerToReturn.Id} is registered");
            return(CreatedAtRoute("GetCustomer", new { id = customerToReturn.Id }, customerToReturn));
        }