public async Task <CustomerDto> Handle(CreateCustomerCommand request, CancellationToken cancellationToken) { if (await _customerRepository.EmailExistAsync(request.Email)) { string msg = $"This email {nameof(request.Email)} is already existed!"; _logger.LogError(msg); throw new BadRequestException(msg); } var customer = new Domain.Models.Customer(request.Name, request.Email, request.Address, request.Age, request.PhoneNumber); _customerRepository.Add(customer); if (await _customerRepository.SaveChangesAsync() == 0) { throw new Exceptions.ApplicationException("Couldn't save data"); } await _mediator.Publish(new Domain.Events.CustomerCreatedEvent(customer.Id), cancellationToken); var customerDto = _customerDxos.MapCustomerDto(customer); return(customerDto); }
public async Task <CustomerDto> Handle(GetCustomerQuery request, CancellationToken cancellationToken) { var customer = await _customerRepository.GetAsync(e => e.Id == request.CustomerId); if (customer != null) { _logger.LogInformation($"You've got a request to get customer Id: {customer.Id}"); var customerDto = _customerDxos.MapCustomerDto(customer); return(customerDto); } return(null); }
public async Task <CustomerDto> Handle(CreateCustomerCommand request, CancellationToken cancellationToken) { if (await _customerRepository.EmailExistAsync(request.Email)) { throw new ArgumentException($"This email {request.Email} is already existed!", nameof(request.Email)); } var customer = new Customer(request.Name, request.Email, request.Address, request.Age, request.PhoneNumber); _customerRepository.Add(customer); if (await _customerRepository.SaveChangesAsync() == 0) { throw new ApplicationException(); } await _mediator.Publish(new CustomerCreatedEvent(customer.Id), cancellationToken); var customerDto = _customerDxos.MapCustomerDto(customer); return(customerDto); }