135 lines
5.2 KiB
C#
135 lines
5.2 KiB
C#
using Modules.User.Application.Gateways;
|
|
using Modules.User.Application.Models;
|
|
using Modules.User.Database.Repositories;
|
|
using System.Linq;
|
|
|
|
namespace Modules.User.Database.GatewaysImplementations;
|
|
|
|
public class UserGateway(UserRepository userRepository, AccountRepository accountRepository) : IUserGateway
|
|
{
|
|
public async Task<Guid> CreateUser(Account account, Application.Models.User user, string? avatarId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(user.NickName)) throw new Exception($"{nameof(Application.Models.User.NickName)} is not set");
|
|
var userId = await userRepository.AddAsync(new Database.Entities.User
|
|
{
|
|
NickName = user.NickName,
|
|
FirstName = user.FirstName,
|
|
Patronymic = user.Patronymic,
|
|
LastName = user.LastName,
|
|
AvatarId = avatarId,
|
|
Account = new Database.Entities.Account
|
|
{
|
|
Email = account.Email.Trim().ToLower(),
|
|
HashedPassword = account.HashedPassword,
|
|
}
|
|
});
|
|
return userId;
|
|
}
|
|
|
|
public async Task<bool> Delete(Guid id) => await userRepository.DeleteAsync(id);
|
|
|
|
public async Task<Account> GetAccount(string email)
|
|
{
|
|
var account = await accountRepository.GetFirstWhere(q => q.Email == email.Trim().ToLower());
|
|
return new Account
|
|
{
|
|
Id = account.Id,
|
|
Email = account.Email,
|
|
HashedPassword = account.HashedPassword,
|
|
};
|
|
}
|
|
|
|
public async Task<Application.Models.User?> GetUser(Guid accountId, Guid? sessionId)
|
|
{
|
|
var user = await userRepository.GetFirstOrDefaultWhereAsync(q => q.Account.Id == accountId &&
|
|
sessionId.HasValue ? q.Account.Sessions.Any(q => q.Id == sessionId.Value) : true);
|
|
|
|
return user == null ? null : new Application.Models.User
|
|
{
|
|
Id= user.Id,
|
|
AccountId = user.Account.Id,
|
|
SessionId = sessionId,
|
|
NickName = user.NickName,
|
|
FirstName = user.FirstName,
|
|
Patronymic = user.Patronymic,
|
|
LastName = user.LastName,
|
|
LanguageId = user.RegionalSettings?.LanguageId,
|
|
Email = user.Account.Email,
|
|
IsAuthenticated = sessionId.HasValue,
|
|
};
|
|
}
|
|
|
|
public async Task<Domain.Entities.User.User?> Get(Guid accountId)
|
|
{
|
|
var user = await userRepository.GetFirstOrDefaultWhereAsync(q => q.Account.Id == accountId);
|
|
|
|
return user == null ? null : new Domain.Entities.User.User(new Domain.Models.User
|
|
{
|
|
Id = user.Id,
|
|
Account = new Domain.Models.Account
|
|
{
|
|
Id = user.Account.Id,
|
|
Email = user.Account.Email,
|
|
HashedPassword = user.Account.HashedPassword,
|
|
Sessions = user.Account.Sessions.Select(q => new Domain.Models.Session
|
|
{
|
|
Id = q.Id,
|
|
AccountId = q.AccountId,
|
|
RefreshToken = q.RefreshToken,
|
|
ClientInfo = new()
|
|
{
|
|
UserAgent = q.ClientInfo.UserAgent,
|
|
Country = q.ClientInfo.Country,
|
|
Region = q.ClientInfo.Region,
|
|
},
|
|
ExpiredDate = q.ExpiredDate,
|
|
})
|
|
},
|
|
AvatarId = user.AvatarId,
|
|
NickName = user.NickName,
|
|
FirstName = user.FirstName,
|
|
SecondName = user.Patronymic,
|
|
LastName = user.LastName,
|
|
LanguageId = user.RegionalSettings?.LanguageId,
|
|
});
|
|
}
|
|
|
|
public async Task<bool> IsExists(string email) =>
|
|
await accountRepository.AnyWhere(q => q.Email == email.Trim().ToLower());
|
|
|
|
public async Task<Account?> TryGetAccount(string email)
|
|
{
|
|
var account = await accountRepository.GetFirstOrDefaultWhere(q => q.Email == email.Trim().ToLower());
|
|
return account == null
|
|
? null
|
|
: new Account
|
|
{
|
|
Id = account.Id,
|
|
Email = account.Email,
|
|
HashedPassword = account.HashedPassword,
|
|
};
|
|
}
|
|
|
|
public async Task UpdateAccount(Guid accountId, Account account)
|
|
{
|
|
var dbAccount = await accountRepository.GetFirstWhere(q => q.Id == accountId && !q.Deleted);
|
|
dbAccount.HashedPassword = account.HashedPassword;
|
|
dbAccount.Email = account.Email;
|
|
await accountRepository.UpdateAccountAsync();
|
|
}
|
|
|
|
public async Task UpdateUser(Guid userId, Application.Models.User user, string? avatarId)
|
|
{
|
|
var dbUser = await userRepository.GetFirstWhereAsync(q => q.Id == userId);
|
|
if (!string.IsNullOrWhiteSpace(user.NickName)) dbUser.NickName = user.NickName;
|
|
if (!string.IsNullOrWhiteSpace(user.FirstName)) dbUser.FirstName = user.FirstName;
|
|
if (!string.IsNullOrWhiteSpace(user.Patronymic)) dbUser.Patronymic = user.Patronymic;
|
|
if (!string.IsNullOrWhiteSpace(user.LastName)) dbUser.LastName = user.LastName;
|
|
dbUser.AvatarId = avatarId;
|
|
dbUser.RegionalSettings = user.LanguageId == null ? null : new Database.Entities.RegionalSettings
|
|
{
|
|
LanguageId = user.LanguageId
|
|
};
|
|
await userRepository.UpdateUserAsync();
|
|
}
|
|
} |