using Customers.Api.Domain; using Customers.Api.Mapping; using Customers.Api.Repositories; using FluentValidation; using FluentValidation.Results; namespace Customers.Api.Services; public class CustomerService : ICustomerService { private readonly ICustomerRepository _customerRepository; public CustomerService(ICustomerRepository customerRepository) { _customerRepository = customerRepository; } public async Task CreateAsync(Customer customer) { var existingUser = await _customerRepository.GetAsync(customer.Id.Value); if (existingUser is not null) { var message = $"A user with id {customer.Id} already exists"; throw new ValidationException(message, new [] { new ValidationFailure(nameof(Customer), message) }); } var customerDto = customer.ToCustomerDto(); return await _customerRepository.CreateAsync(customerDto); } public async Task GetAsync(Guid id) { var customerDto = await _customerRepository.GetAsync(id); return customerDto?.ToCustomer(); } public async Task> GetAllAsync() { var customerDtos = await _customerRepository.GetAllAsync(); return customerDtos.Select(x => x.ToCustomer()); } public async Task UpdateAsync(Customer customer) { var customerDto = customer.ToCustomerDto(); return await _customerRepository.UpdateAsync(customerDto); } public async Task DeleteAsync(Guid id) { return await _customerRepository.DeleteAsync(id); } }