public CustomerDto Add(CustomerDto customerDto) { ISpecification<Customer> alreadyRegisteredSpec = new CustomerAlreadyRegisteredSpec(customerDto.Email); Customer existingCustomer = this.customerRepository.FindOne(alreadyRegisteredSpec); if (existingCustomer != null) throw new Exception("Customer with this email already exists"); Country country = this.countryRepository.FindById(customerDto.CountryId); Customer customer = Customer.Create(customerDto.FirstName, customerDto.LastName, customerDto.Email, country); this.customerRepository.Add(customer); this.unitOfWork.Commit(); return AutoMapper.Mapper.Map<Customer, CustomerDto>(customer); }
public void Update(CustomerDto customerDto) { if (customerDto.Id == Guid.Empty) throw new Exception("Id can't be empty"); ISpecification<Customer> registeredSpec = new CustomerRegisteredSpec(customerDto.Id); Customer customer = this.customerRepository.FindOne(registeredSpec); if (customer == null) throw new Exception("No such customer exists"); customer.ChangeEmail(customerDto.Email); this.unitOfWork.Commit(); }