using Customers.Api.Contracts.Data; using Customers.Api.Database; using Dapper; namespace Customers.Api.Repositories; public class CustomerRepository : ICustomerRepository { private readonly IDbConnectionFactory _connectionFactory; public CustomerRepository(IDbConnectionFactory connectionFactory) { _connectionFactory = connectionFactory; } public async Task CreateAsync(CustomerDto customer) { using var connection = await _connectionFactory.CreateConnectionAsync(); var result = await connection.ExecuteAsync( @"INSERT INTO Customers (Id, Username, FullName, Email, DateOfBirth) VALUES (@Id, @Username, @FullName, @Email, @DateOfBirth)", customer); return result > 0; } public async Task GetAsync(Guid id) { using var connection = await _connectionFactory.CreateConnectionAsync(); return await connection.QuerySingleOrDefaultAsync( "SELECT * FROM Customers WHERE Id = @Id LIMIT 1", new { Id = id.ToString() }); } public async Task> GetAllAsync() { using var connection = await _connectionFactory.CreateConnectionAsync(); return await connection.QueryAsync("SELECT * FROM Customers"); } public async Task UpdateAsync(CustomerDto customer) { using var connection = await _connectionFactory.CreateConnectionAsync(); var result = await connection.ExecuteAsync( @"UPDATE Customers SET Username = @Username, FullName = @FullName, Email = @Email, DateOfBirth = @DateOfBirth WHERE Id = @Id", customer); return result > 0; } public async Task DeleteAsync(Guid id) { using var connection = await _connectionFactory.CreateConnectionAsync(); var result = await connection.ExecuteAsync(@"DELETE FROM Customers WHERE Id = @Id", new {Id = id.ToString()}); return result > 0; } }