using MAX.Framework.Validation; using MAX.Core.DependencyInjection; using MAX.Framework.Commons; using Store.Users.Contracts; using Store.Users.Contracts.Dtos; using Store.Users.Entities; using Store.Users.Mappers; using Store.Users.Repositories; namespace Store.Users.Services; internal sealed class UserQueryService(IUserRepository repository) : IUserQueryService, ITransientDependency { public async Task GetUser(string userName) { RequestValidator.Validate(new UserNameRequest { UserName = userName?.Trim()! }); var user = await repository.GetUser(userName.Trim()) ?? throw new AppException("用户不存在"); return new UserMapper().ToInfo(user); } /// /// 获取主账号 /// /// /// public async Task GetMainUser(string userName) { RequestValidator.Validate(new UserNameRequest { UserName = userName?.Trim()! }); var user = await repository.GetMainUser(userName.Trim()) ?? throw new AppException("用户不存在"); return new UserMapper().ToInfo(user); } } internal sealed class UserAuthenticationService(IFreeSql orm, IUserQueryService query) : IUserAuthenticationService, ITransientDependency { public async Task Authenticate(string userName, string password, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (!RequestValidator.TryValidate(new UserLoginRequest { UserName = userName?.Trim()!, Password = password }, out _)) return null; var user = await orm.Select().Where(u => u.UserName == userName.Trim()).FirstAsync(cancellationToken); if (user == null || !UserPassword.Verify(user, password)) return null; return await query.GetUser(user.UserName); } }