Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/UserRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/UserRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/UserRepository.cs
-
curl -L -o UserRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/UserRepository.cs
2.69 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Users; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class UserRepository : IUserRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public UserRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Cols = "id, username, email, password_hash, role, is_active, created_at"; | |
| public async Task<User?> GetByUsernameAsync(string username, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Cols} FROM users WHERE lower(username) = lower(@Username)"; | |
| return await _uow.Connection.QueryFirstOrDefaultAsync<User>( | |
| new CommandDefinition(sql, new { Username = username }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<User>> GetAllAsync(CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Cols} FROM users ORDER BY username"; | |
| var rows = await _uow.Connection.QueryAsync<User>( | |
| new CommandDefinition(sql, transaction: Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<User?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Cols} FROM users WHERE id = @Id"; | |
| return await _uow.Connection.QueryFirstOrDefaultAsync<User>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> UsernameExistsAsync(string username, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM users WHERE lower(username) = lower(@Username)"; | |
| var count = await _uow.Connection.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Username = username }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddAsync(User u, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO users (id, username, email, password_hash, role, is_active, created_at) | |
| VALUES (@Id, @Username, @Email, @PasswordHash, @Role, @IsActive, @CreatedAt) | |
| """; | |
| await _uow.Connection.ExecuteAsync(new CommandDefinition(sql, u, Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(User u, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE users SET email = @Email, role = @Role, is_active = @IsActive, password_hash = @PasswordHash | |
| WHERE id = @Id | |
| """; | |
| await _uow.Connection.ExecuteAsync(new CommandDefinition(sql, u, Tx, cancellationToken: ct)); | |
| } | |
| } | |