using System.Data; using Dapper; using SCM.Application.Common; using SCM.Domain.Projects; namespace SCM.Infrastructure.Persistence.Repositories; public sealed class ProjectRepository : IProjectRepository { private readonly IUnitOfWork _uow; public ProjectRepository(IUnitOfWork uow) => _uow = uow; private IDbConnection Conn => _uow.Connection; private IDbTransaction? Tx => _uow.CurrentTransaction; public async Task GetByIdAsync(Guid id, CancellationToken ct = default) { const string sql = """ SELECT id, code, name, description, start_date, end_date, status, created_at, updated_at FROM projects WHERE id = @Id """; return await Conn.QueryFirstOrDefaultAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); } public async Task<(IReadOnlyList Items, long TotalItems)> GetPagedAsync( int offset, int pageSize, string? search, CancellationToken ct = default) { var term = string.IsNullOrWhiteSpace(search) ? null : $"%{search.Trim()}%"; const string countSql = """ SELECT COUNT(*) FROM projects WHERE (@Search::text IS NULL OR name ILIKE @Search OR code ILIKE @Search) """; const string pageSql = """ SELECT id, code, name, description, start_date, end_date, status, created_at, updated_at FROM projects WHERE (@Search::text IS NULL OR name ILIKE @Search OR code ILIKE @Search) ORDER BY created_at DESC LIMIT @PageSize OFFSET @Offset """; var total = await Conn.ExecuteScalarAsync( new CommandDefinition(countSql, new { Search = term }, Tx, cancellationToken: ct)); var rows = await Conn.QueryAsync( new CommandDefinition(pageSql, new { Search = term, PageSize = pageSize, Offset = offset }, Tx, cancellationToken: ct)); return (rows.ToList(), total); } public async Task CodeExistsAsync(string code, Guid? excludeId = null, CancellationToken ct = default) { const string sql = """ SELECT COUNT(*) FROM projects WHERE lower(code) = lower(@Code) AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) """; var count = await Conn.ExecuteScalarAsync( new CommandDefinition(sql, new { Code = code, ExcludeId = excludeId }, Tx, cancellationToken: ct)); return count > 0; } public async Task AddAsync(Project project, CancellationToken ct = default) { const string sql = """ INSERT INTO projects (id, code, name, description, start_date, end_date, status, created_at, updated_at) VALUES (@Id, @Code, @Name, @Description, @StartDate, @EndDate, @Status, @CreatedAt, @UpdatedAt) """; await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(project), Tx, cancellationToken: ct)); } public async Task UpdateAsync(Project project, CancellationToken ct = default) { const string sql = """ UPDATE projects SET name = @Name, description = @Description, start_date = @StartDate, end_date = @EndDate, status = @Status, updated_at = @UpdatedAt WHERE id = @Id """; await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(project), Tx, cancellationToken: ct)); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { const string sql = "DELETE FROM projects WHERE id = @Id"; var affected = await Conn.ExecuteAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); return affected > 0; } private static object ToParameters(Project p) => new { p.Id, p.Code, p.Name, p.Description, p.StartDate, p.EndDate, Status = (int)p.Status, p.CreatedAt, p.UpdatedAt }; }