using Dapper; using ScrapperAPI.Dtos; using ScrapperAPI.Interfaces; namespace ScrapperAPI.Repositories; public sealed class SessionRepository : ISessionRepository { private readonly IDbConnectionFactory _db; public SessionRepository(IDbConnectionFactory db) => _db = db; public async Task CreateAsync(string name, CancellationToken ct) { const string sql = """ insert into session(name) values (@name) returning id; """; using var conn = await _db.CreateOpenConnectionAsync(ct); return await conn.ExecuteScalarAsync(new CommandDefinition(sql, new { name }, cancellationToken: ct)); } public async Task FindByNameAsync(string name, CancellationToken ct) { const string sql = """ select id, name from session where name = @name limit 1; """; using var conn = await _db.CreateOpenConnectionAsync(ct); return await conn.QuerySingleOrDefaultAsync( new CommandDefinition(sql, new { name }, cancellationToken: ct)); } public async Task GetIdByNameAsync(string name, CancellationToken ct) => (await FindByNameAsync(name, ct))?.Id; public async Task> GetAllAsync(CancellationToken ct) { const string sql = """ select id, name from session order by id; """; using var conn = await _db.CreateOpenConnectionAsync(ct); return (await conn.QueryAsync( new CommandDefinition(sql, cancellationToken: ct) )).ToList(); } }