Blazorcrud.Server/Models/UserRepository.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Blazorcrud.Server.Authorization;
using Blazorcrud.Server.Helpers;
using Blazorcrud.Shared.Data;
using Blazorcrud.Shared.Models;
using Microsoft.EntityFrameworkCore;

namespace Blazorcrud.Server.Models
{
    public class UserRepository : IUserRepository
    {
        private readonly AppDbContext _appDbContext;
        private IJwtUtils _jwtUtils;

        public UserRepository(AppDbContext appDbContext, IJwtUtils jwtUtils)
        {
            _appDbContext = appDbContext;
            _jwtUtils = jwtUtils;
        }

        public AuthenticateResponse Authenticate(AuthenticateRequest request)
    {
        var _user = _appDbContext.Users.SingleOrDefault(u => u.Username == request.Username);

        // validate
        if (_user == null || !BCrypt.Net.BCrypt.Verify(request.Password, _user.PasswordHash))
            throw new AppException("Username or password is incorrect");

        // authentication successful
        AuthenticateResponse response = new AuthenticateResponse();
        response.Id = _user.Id;
        response.LastName = _user.LastName;
        response.FirstName = _user.FirstName;
        response.Username = _user.Username;
        response.Token = _jwtUtils.GenerateToken(_user);
        return response;
    }

        public PagedResult<User> GetUsers(string? name, int page)
        {
            int pageSize = 5;

            if (name != null)
            {
                return _appDbContext.Users
                    .Where(u => u.FirstName.Contains(name, StringComparison.CurrentCultureIgnoreCase) ||
                        u.LastName.Contains(name, StringComparison.CurrentCultureIgnoreCase) || 
                        u.Username.Contains(name, StringComparison.CurrentCultureIgnoreCase))
                    .OrderBy(u => u.Username)
                    .GetPaged(page, pageSize);
            }
            else
            {
                return _appDbContext.Users
                    .OrderBy(u => u.Username)
                    .GetPaged(page, pageSize);
            }
        }

        public async Task<User?> GetUser(int Id)
        {
后面还有 80 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。