25 lines
786 B
C#
25 lines
786 B
C#
using Messenger.Api.Contracts;
|
|
using Messenger.Api.Data;
|
|
using Npgsql;
|
|
|
|
namespace Messenger.Api.Services;
|
|
|
|
public sealed class UserService(AppDatabase database, PasswordService passwords)
|
|
{
|
|
public async Task<List<UserDto>> ListForChatAsync(Guid chatId) =>
|
|
(await database.ListChatUsersAsync(chatId)).Select(user => user.ToDto()).ToList();
|
|
|
|
public async Task<UserDto?> RegisterAsync(string username, string displayName, string password)
|
|
{
|
|
var hash = passwords.Hash(password);
|
|
try
|
|
{
|
|
return (await database.CreateUserAsync(username, displayName, hash)).ToDto();
|
|
}
|
|
catch (PostgresException exception) when (exception.SqlState == PostgresErrorCodes.UniqueViolation)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|