astral-api/Astral.DAL/Repositories/UserRepository.cs
2024-12-11 20:36:30 +00:00

52 lines
1.7 KiB
C#

// <copyright file="UserRepository.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
using Astral.Core.Entities;
using Astral.Core.Infrastructure;
using Astral.Core.RepositoryInterfaces;
using Dapper;
using Injectio.Attributes;
namespace Astral.DAL.Repositories;
/// <inheritdoc cref="IUserRepository" />
[RegisterScoped]
public class UserRepository : BaseRepository<User, Guid>, IUserRepository
{
/// <summary>
/// Initializes a new instance of the <see cref="UserRepository" /> class.
/// </summary>
/// <param name="db">Instance of <see cref="IDbConnectionProvider" />.</param>
public UserRepository(IDbConnectionProvider db)
: base(db)
{
}
/// <inheritdoc cref="IUserRepository" />
public async Task<long> CountUsersAsync()
{
return await WithDatabaseAsync(async connection =>
await connection.QueryFirstAsync<long>($"SELECT COUNT(*) FROM {Table};"));
}
/// <inheritdoc cref="IUserRepository" />
public async Task<User> FindByUsername(string username)
{
return await WithDatabaseAsync(async connection =>
await connection.QueryFirstOrDefaultAsync<User>($"SELECT * FROM {Table} WHERE username = @pUsername", new
{
pUsername = username
}));
}
/// <inheritdoc cref="IUserRepository" />
public async Task<User> FindByEmailAddress(string emailAddress)
{
return await WithDatabaseAsync(async connection =>
await connection.QueryFirstOrDefaultAsync<User>($"SELECT * FROM {Table} WHERE email = @pEmailAddress", new
{
pEmailAddress = emailAddress
}));
}
}