@@ -1,53 +1,53 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos.Auth;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.Tools;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IM_API.Services
|
||||
{
|
||||
public class AuthService : IAuthService
|
||||
{
|
||||
private readonly ImContext _context;
|
||||
private readonly ILogger<AuthService> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ICacheService _cache;
|
||||
public AuthService(ImContext context, ILogger<AuthService> logger, IMapper mapper, ICacheService cache)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public async Task<User> LoginAsync(LoginRequestDto dto)
|
||||
{
|
||||
var userinfo = await _cache.GetUserCacheAsync(dto.Username);
|
||||
if (userinfo != null && userinfo.Password == dto.Password) return userinfo;
|
||||
string username = dto.Username;
|
||||
string password = dto.Password;
|
||||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username && x.Password == password);
|
||||
if(user is null)
|
||||
{
|
||||
throw new BaseException(CodeDefine.PASSWORD_ERROR);
|
||||
}
|
||||
await _cache.SetUserCacheAsync(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<UserInfoDto> RegisterAsync(RegisterRequestDto dto)
|
||||
{
|
||||
string username = dto.Username;
|
||||
//用户是否存在
|
||||
bool isExist = await _context.Users.AnyAsync(x => x.Username == username);
|
||||
if (isExist) throw new BaseException(CodeDefine.USER_ALREADY_EXISTS);
|
||||
User user = _mapper.Map<User>(dto);
|
||||
_context.Users.Add(user);
|
||||
await _context.SaveChangesAsync();
|
||||
return _mapper.Map<UserInfoDto>(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos.Auth;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.Tools;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IM_API.Services
|
||||
{
|
||||
public class AuthService : IAuthService
|
||||
{
|
||||
private readonly ImContext _context;
|
||||
private readonly ILogger<AuthService> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ICacheService _cache;
|
||||
public AuthService(ImContext context, ILogger<AuthService> logger, IMapper mapper, ICacheService cache)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public async Task<User> LoginAsync(LoginRequestDto dto)
|
||||
{
|
||||
var userinfo = await _cache.GetUserCacheAsync(dto.Username);
|
||||
if (userinfo != null && userinfo.Password == dto.Password) return userinfo;
|
||||
string username = dto.Username;
|
||||
string password = dto.Password;
|
||||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username && x.Password == password);
|
||||
if(user is null)
|
||||
{
|
||||
throw new BaseException(CodeDefine.PASSWORD_ERROR);
|
||||
}
|
||||
await _cache.SetUserCacheAsync(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<UserInfoDto> RegisterAsync(RegisterRequestDto dto)
|
||||
{
|
||||
string username = dto.Username;
|
||||
//用户是否存在
|
||||
bool isExist = await _context.Users.AnyAsync(x => x.Username == username);
|
||||
if (isExist) throw new BaseException(CodeDefine.USER_ALREADY_EXISTS);
|
||||
User user = _mapper.Map<User>(dto);
|
||||
_context.Users.Add(user);
|
||||
await _context.SaveChangesAsync();
|
||||
return _mapper.Map<UserInfoDto>(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user