Merge branch 'feature-nxdev' of https://gitea.nxsir.cn/code/IM into feature-nxdev
This commit is contained in:
@@ -35,20 +35,10 @@ namespace IM_API.Services
|
||||
public async Task<UserInfoDto> RegisterAsync(RegisterRequestDto dto)
|
||||
{
|
||||
string username = dto.Username;
|
||||
string password = dto.Password;
|
||||
string nickname = dto.NickName;
|
||||
//用户是否存在
|
||||
bool isExist = await _context.Users.AnyAsync(x => x.Username == username);
|
||||
if (isExist) throw new BaseException(CodeDefine.USER_ALREADY_EXISTS);
|
||||
User user = new User
|
||||
{
|
||||
Username = username,
|
||||
Password = password,
|
||||
NickName = nickname,
|
||||
OnlineStatusEnum = UserOnlineStatus.Offline,
|
||||
StatusEnum = UserStatus.Normal,
|
||||
Created = DateTime.Now
|
||||
};
|
||||
User user = _mapper.Map<User>(dto);
|
||||
_context.Users.Add(user);
|
||||
await _context.SaveChangesAsync();
|
||||
return _mapper.Map<UserInfoDto>(user);
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Interface.Services;
|
||||
|
||||
namespace IM_API.Services
|
||||
{
|
||||
public class Demo : IDemo
|
||||
{
|
||||
public TestDto Test()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,67 @@
|
||||
using IM_API.Models;
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IM_API.Services
|
||||
{
|
||||
public class FriendService
|
||||
public class FriendService : IFriendSerivce
|
||||
{
|
||||
|
||||
private readonly ImContext _context;
|
||||
private readonly ILogger<FriendService> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
public FriendService(ImContext context, ILogger<FriendService> logger, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public Task<bool> BlockeFriendAsync(int friendId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> BlockFriendByUserIdAsync(int userId, int toUserId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> DeleteFriendAsync(int friendId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> DeleteFriendByUserIdAsync(int userId, int toUserId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<List<FriendInfoDto>> GetFriendListAsync(int userId, int page, int limit, bool desc)
|
||||
{
|
||||
var query = _context.Friends.Where(x => x.UserId == userId);
|
||||
if (desc)
|
||||
{
|
||||
query = query.OrderByDescending(x => x.UserId);
|
||||
}
|
||||
var friendList = await query.Skip((page - 1 * limit)).Take(limit).ToListAsync();
|
||||
return _mapper.Map<List<FriendInfoDto>>(friendList);
|
||||
}
|
||||
|
||||
public Task<FriendRequest> GetFriendRequestListAsync(int userId, bool isReceived, int page, int limit, bool desc)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> HandleFriendRequestAsync(HandleFriendRequestDto requestDto)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> SendFriendRequestAsync(FriendRequestDto friendRequest)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
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 UserService : IUserService
|
||||
{
|
||||
private readonly ImContext _context;
|
||||
private readonly ILogger<UserService> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
public UserService(ImContext imContext,ILogger<UserService> logger,IMapper mapper)
|
||||
{
|
||||
this._context = imContext;
|
||||
this._logger = logger;
|
||||
this._mapper = mapper;
|
||||
}
|
||||
#region 获取用户信息
|
||||
public async Task<UserInfoDto> GetUserInfoAsync(int userId)
|
||||
{
|
||||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
if (user == null)
|
||||
{
|
||||
throw new BaseException(CodeDefine.USER_NOT_FOUND);
|
||||
}
|
||||
return _mapper.Map<UserInfoDto>(user);
|
||||
}
|
||||
#endregion
|
||||
#region 通过用户名获取用户信息
|
||||
public async Task<UserInfoDto> GetUserInfoByUsernameAsync(string username)
|
||||
{
|
||||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username);
|
||||
if (user == null)
|
||||
{
|
||||
throw new BaseException(CodeDefine.USER_NOT_FOUND);
|
||||
}
|
||||
return _mapper.Map<UserInfoDto>(user);
|
||||
}
|
||||
#endregion
|
||||
#region 重置用户密码
|
||||
public async Task<bool> ResetPasswordAsync(int userId, string oldPassword, string password)
|
||||
{
|
||||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
if (user is null) throw new BaseException(CodeDefine.USER_NOT_FOUND);
|
||||
//验证原密码
|
||||
if (user.Password != oldPassword) throw new BaseException(CodeDefine.PASSWORD_ERROR);
|
||||
user.Password = password;
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
#region 更新用户在线状态
|
||||
public async Task<bool> UpdateOlineStatusAsync(int userId,UserOnlineStatus onlineStatus)
|
||||
{
|
||||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
if (user is null) throw new BaseException(CodeDefine.USER_NOT_FOUND);
|
||||
user.OnlineStatusEnum = onlineStatus;
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
#region 更新用户信息
|
||||
public async Task<UserInfoDto> UpdateUserAsync(int userId,UpdateUserDto dto)
|
||||
{
|
||||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
if (user is null) throw new BaseException(CodeDefine.USER_NOT_FOUND);
|
||||
_mapper.Map(dto,user);
|
||||
await _context.SaveChangesAsync();
|
||||
return _mapper.Map<UserInfoDto>(user);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user