using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using Microsoft.EntityFrameworkCore; using OliverBooth.Data.Web; using BC = BCrypt.Net.BCrypt; namespace OliverBooth.Services; /// /// Represents an implementation of . /// internal sealed class UserService : IUserService { private readonly IDbContextFactory _dbContextFactory; private readonly ConcurrentDictionary _userCache = new(); /// /// Initializes a new instance of the class. /// /// /// The used to create a . /// public UserService(IDbContextFactory dbContextFactory) { _dbContextFactory = dbContextFactory; } /// public bool TryGetUser(Guid id, [NotNullWhen(true)] out IUser? user) { if (_userCache.TryGetValue(id, out user)) return true; using WebContext context = _dbContextFactory.CreateDbContext(); user = context.Users.Find(id); if (user is not null) _userCache.TryAdd(id, user); return user is not null; } /// public bool VerifyLogin(string email, string password, [NotNullWhen(true)] out IUser? user) { using WebContext context = _dbContextFactory.CreateDbContext(); user = context.Users.FirstOrDefault(u => u.EmailAddress == email); return user is not null && BC.Verify(password, ((User)user).Password); } }