提交
This commit is contained in:
@@ -1,82 +1,82 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Auth;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Tools;
|
||||
using IM_API.VOs.Auth;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<AuthController> _logger;
|
||||
private readonly IAuthService _authService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IJWTService _jwtService;
|
||||
private readonly IRefreshTokenService _refreshTokenService;
|
||||
private readonly IConfiguration _configuration;
|
||||
private IMapper _mapper;
|
||||
public AuthController(ILogger<AuthController> logger, IAuthService authService,
|
||||
IJWTService jwtService, IRefreshTokenService refreshTokenService,
|
||||
IConfiguration configuration,IUserService userService,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
_authService = authService;
|
||||
_jwtService = jwtService;
|
||||
_refreshTokenService = refreshTokenService;
|
||||
_configuration = configuration;
|
||||
_userService = userService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Login(LoginRequestDto dto)
|
||||
{
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
var user = await _authService.LoginAsync(dto);
|
||||
_logger.LogInformation("服务耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
var userInfo = _mapper.Map<UserInfoDto>(user);
|
||||
_logger.LogInformation("序列化耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
//生成凭证
|
||||
(string token,DateTime expiresAt) = _jwtService.CreateAccessTokenForUser(user.Id,user.Username,"user");
|
||||
_logger.LogInformation("Token生成耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
//生成刷新凭证
|
||||
|
||||
string refreshToken = await _refreshTokenService.CreateRefreshTokenAsync(user.Id);
|
||||
_logger.LogInformation("RefreshToken生成耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
var res = new BaseResponse<LoginVo>(new LoginVo(userInfo,token,refreshToken, expiresAt));
|
||||
_logger.LogInformation("总耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Register(RegisterRequestDto dto)
|
||||
{
|
||||
var userInfo = await _authService.RegisterAsync(dto);
|
||||
var res = new BaseResponse<UserInfoDto>(userInfo);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<LoginVo>),StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Refresh(RefreshDto dto)
|
||||
{
|
||||
(bool ok,int userId) = await _refreshTokenService.ValidateRefreshTokenAsync(dto.refreshToken);
|
||||
if (!ok)
|
||||
{
|
||||
var err = new BaseResponse<LoginVo>(CodeDefine.AUTH_FAILED);
|
||||
return Unauthorized(err);
|
||||
}
|
||||
var userinfo = await _userService.GetUserInfoAsync(userId);
|
||||
(string token,DateTime expiresAt) = _jwtService.CreateAccessTokenForUser(userinfo.Id,userinfo.Username,"user");
|
||||
var res = new BaseResponse<LoginVo>(new LoginVo(userinfo,token, dto.refreshToken, expiresAt));
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Auth;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Tools;
|
||||
using IM_API.VOs.Auth;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<AuthController> _logger;
|
||||
private readonly IAuthService _authService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IJWTService _jwtService;
|
||||
private readonly IRefreshTokenService _refreshTokenService;
|
||||
private readonly IConfiguration _configuration;
|
||||
private IMapper _mapper;
|
||||
public AuthController(ILogger<AuthController> logger, IAuthService authService,
|
||||
IJWTService jwtService, IRefreshTokenService refreshTokenService,
|
||||
IConfiguration configuration,IUserService userService,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
_authService = authService;
|
||||
_jwtService = jwtService;
|
||||
_refreshTokenService = refreshTokenService;
|
||||
_configuration = configuration;
|
||||
_userService = userService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Login(LoginRequestDto dto)
|
||||
{
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
var user = await _authService.LoginAsync(dto);
|
||||
_logger.LogInformation("服务耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
var userInfo = _mapper.Map<UserInfoDto>(user);
|
||||
_logger.LogInformation("序列化耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
//生成凭证
|
||||
(string token,DateTime expiresAt) = _jwtService.CreateAccessTokenForUser(user.Id,user.Username,"user");
|
||||
_logger.LogInformation("Token生成耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
//生成刷新凭证
|
||||
|
||||
string refreshToken = await _refreshTokenService.CreateRefreshTokenAsync(user.Id);
|
||||
_logger.LogInformation("RefreshToken生成耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
var res = new BaseResponse<LoginVo>(new LoginVo(userInfo,token,refreshToken, expiresAt));
|
||||
_logger.LogInformation("总耗时: {ms}ms", sw.ElapsedMilliseconds);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Register(RegisterRequestDto dto)
|
||||
{
|
||||
var userInfo = await _authService.RegisterAsync(dto);
|
||||
var res = new BaseResponse<UserInfoDto>(userInfo);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<LoginVo>),StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Refresh(RefreshDto dto)
|
||||
{
|
||||
(bool ok,int userId) = await _refreshTokenService.ValidateRefreshTokenAsync(dto.refreshToken);
|
||||
if (!ok)
|
||||
{
|
||||
var err = new BaseResponse<LoginVo>(CodeDefine.AUTH_FAILED);
|
||||
return Unauthorized(err);
|
||||
}
|
||||
var userinfo = await _userService.GetUserInfoAsync(userId);
|
||||
(string token,DateTime expiresAt) = _jwtService.CreateAccessTokenForUser(userinfo.Id,userinfo.Username,"user");
|
||||
var res = new BaseResponse<LoginVo>(new LoginVo(userinfo,token, dto.refreshToken, expiresAt));
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.VOs.Conversation;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class ConversationController : ControllerBase
|
||||
{
|
||||
private readonly IConversationService _conversationSerivice;
|
||||
private readonly ILogger<ConversationController> _logger;
|
||||
public ConversationController(IConversationService conversationSerivice, ILogger<ConversationController> logger)
|
||||
{
|
||||
_conversationSerivice = conversationSerivice;
|
||||
_logger = logger;
|
||||
}
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> List()
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var list = await _conversationSerivice.GetConversationsAsync(int.Parse(userIdStr));
|
||||
var res = new BaseResponse<List<ConversationVo>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get([FromQuery]int conversationId)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var conversation = await _conversationSerivice.GetConversationByIdAsync(int.Parse(userIdStr), conversationId);
|
||||
var res = new BaseResponse<ConversationVo>(conversation);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Clear()
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _conversationSerivice.ClearConversationsAsync(int.Parse(userIdStr));
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Delete(int cid)
|
||||
{
|
||||
await _conversationSerivice.DeleteConversationAsync(cid);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.VOs.Conversation;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class ConversationController : ControllerBase
|
||||
{
|
||||
private readonly IConversationService _conversationSerivice;
|
||||
private readonly ILogger<ConversationController> _logger;
|
||||
public ConversationController(IConversationService conversationSerivice, ILogger<ConversationController> logger)
|
||||
{
|
||||
_conversationSerivice = conversationSerivice;
|
||||
_logger = logger;
|
||||
}
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> List()
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var list = await _conversationSerivice.GetConversationsAsync(int.Parse(userIdStr));
|
||||
var res = new BaseResponse<List<ConversationVo>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get([FromQuery]int conversationId)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var conversation = await _conversationSerivice.GetConversationByIdAsync(int.Parse(userIdStr), conversationId);
|
||||
var res = new BaseResponse<ConversationVo>(conversation);
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Clear()
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _conversationSerivice.ClearConversationsAsync(int.Parse(userIdStr));
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Delete(int cid)
|
||||
{
|
||||
await _conversationSerivice.DeleteConversationAsync(cid);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,117 +1,117 @@
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Friend;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class FriendController : ControllerBase
|
||||
{
|
||||
private readonly IFriendSerivce _friendService;
|
||||
private readonly ILogger<FriendController> _logger;
|
||||
public FriendController(IFriendSerivce friendService, ILogger<FriendController> logger)
|
||||
{
|
||||
_friendService = friendService;
|
||||
_logger = logger;
|
||||
}
|
||||
/// <summary>
|
||||
/// 发起好友请求
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Request(FriendRequestDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
dto.FromUserId = userId;
|
||||
await _friendService.SendFriendRequestAsync(dto);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取好友请求列表
|
||||
/// </summary>
|
||||
/// <param name="isReceived"></param>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="limit"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Requests(int page,int limit,bool desc)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var list = await _friendService.GetFriendRequestListAsync(userId,page,limit,desc);
|
||||
var res = new BaseResponse<List<FriendRequestResDto>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理好友请求
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> HandleRequest(
|
||||
[FromQuery]int id, [FromBody]FriendRequestHandleDto dto
|
||||
)
|
||||
{
|
||||
await _friendService.HandleFriendRequestAsync(new HandleFriendRequestDto()
|
||||
{
|
||||
RequestId = id,
|
||||
RemarkName = dto.RemarkName,
|
||||
Action = dto.Action
|
||||
});
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取好友列表
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="limit"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> List(int page,int limit,bool desc)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var list = await _friendService.GetFriendListAsync(userId,page,limit,desc);
|
||||
var res = new BaseResponse<List<FriendInfoDto>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除好友
|
||||
/// </summary>
|
||||
/// <param name="friendId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Delete([FromRoute] int friendId)
|
||||
{
|
||||
//TODO: 这里存在安全问题,当用户传入的id与用户无关时也可以删除成功,待修复。
|
||||
await _friendService.DeleteFriendAsync(friendId);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
/// <summary>
|
||||
/// 拉黑好友
|
||||
/// </summary>
|
||||
/// <param name="friendId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Block([FromRoute] int friendId)
|
||||
{
|
||||
//TODO: 这里存在安全问题,当用户传入的id与用户无关时也可以拉黑成功,待修复。
|
||||
await _friendService.BlockeFriendAsync(friendId);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
}
|
||||
}
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Friend;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class FriendController : ControllerBase
|
||||
{
|
||||
private readonly IFriendSerivce _friendService;
|
||||
private readonly ILogger<FriendController> _logger;
|
||||
public FriendController(IFriendSerivce friendService, ILogger<FriendController> logger)
|
||||
{
|
||||
_friendService = friendService;
|
||||
_logger = logger;
|
||||
}
|
||||
/// <summary>
|
||||
/// 发起好友请求
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Request(FriendRequestDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
dto.FromUserId = userId;
|
||||
await _friendService.SendFriendRequestAsync(dto);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取好友请求列表
|
||||
/// </summary>
|
||||
/// <param name="isReceived"></param>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="limit"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Requests(int page,int limit,bool desc)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var list = await _friendService.GetFriendRequestListAsync(userId,page,limit,desc);
|
||||
var res = new BaseResponse<List<FriendRequestResDto>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理好友请求
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> HandleRequest(
|
||||
[FromQuery]int id, [FromBody]FriendRequestHandleDto dto
|
||||
)
|
||||
{
|
||||
await _friendService.HandleFriendRequestAsync(new HandleFriendRequestDto()
|
||||
{
|
||||
RequestId = id,
|
||||
RemarkName = dto.RemarkName,
|
||||
Action = dto.Action
|
||||
});
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取好友列表
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="limit"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> List(int page,int limit,bool desc)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var list = await _friendService.GetFriendListAsync(userId,page,limit,desc);
|
||||
var res = new BaseResponse<List<FriendInfoDto>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除好友
|
||||
/// </summary>
|
||||
/// <param name="friendId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Delete([FromRoute] int friendId)
|
||||
{
|
||||
//TODO: 这里存在安全问题,当用户传入的id与用户无关时也可以删除成功,待修复。
|
||||
await _friendService.DeleteFriendAsync(friendId);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
/// <summary>
|
||||
/// 拉黑好友
|
||||
/// </summary>
|
||||
/// <param name="friendId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Block([FromRoute] int friendId)
|
||||
{
|
||||
//TODO: 这里存在安全问题,当用户传入的id与用户无关时也可以拉黑成功,待修复。
|
||||
await _friendService.BlockeFriendAsync(friendId);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +1,71 @@
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Group;
|
||||
using IM_API.Interface.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class GroupController : ControllerBase
|
||||
{
|
||||
private readonly IGroupService _groupService;
|
||||
private readonly ILogger<GroupController> _logger;
|
||||
public GroupController(IGroupService groupService, ILogger<GroupController> logger)
|
||||
{
|
||||
_groupService = groupService;
|
||||
_logger = logger;
|
||||
}
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(BaseResponse<List<GroupInfoDto>>) ,StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetGroups(int page = 1, int limit = 100, bool desc = false)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var list = await _groupService.GetGroupListAsync(int.Parse(userIdStr), page, limit, desc);
|
||||
var res = new BaseResponse<List<GroupInfoDto>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<GroupInfoDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> CreateGroup([FromBody]GroupCreateDto groupCreateDto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var groupInfo = await _groupService.CreateGroupAsync(int.Parse(userIdStr), groupCreateDto);
|
||||
var res = new BaseResponse<GroupInfoDto>(groupInfo);
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<object?>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> HandleGroupInvite([FromBody]HandleGroupInviteDto dto)
|
||||
{
|
||||
string userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier)!;
|
||||
await _groupService.HandleGroupInviteAsync(int.Parse(userIdStr), dto);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<object?>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> HandleGroupRequest([FromBody]HandleGroupRequestDto dto)
|
||||
{
|
||||
string userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _groupService.HandleGroupRequestAsync(int.Parse(userIdStr),dto);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<object?>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> InviteUser([FromBody]GroupInviteUserDto dto)
|
||||
{
|
||||
string userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _groupService.InviteUsersAsync(int.Parse(userIdStr), dto.GroupId, dto.Ids);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Group;
|
||||
using IM_API.Interface.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class GroupController : ControllerBase
|
||||
{
|
||||
private readonly IGroupService _groupService;
|
||||
private readonly ILogger<GroupController> _logger;
|
||||
public GroupController(IGroupService groupService, ILogger<GroupController> logger)
|
||||
{
|
||||
_groupService = groupService;
|
||||
_logger = logger;
|
||||
}
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(BaseResponse<List<GroupInfoDto>>) ,StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetGroups(int page = 1, int limit = 100, bool desc = false)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var list = await _groupService.GetGroupListAsync(int.Parse(userIdStr), page, limit, desc);
|
||||
var res = new BaseResponse<List<GroupInfoDto>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<GroupInfoDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> CreateGroup([FromBody]GroupCreateDto groupCreateDto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var groupInfo = await _groupService.CreateGroupAsync(int.Parse(userIdStr), groupCreateDto);
|
||||
var res = new BaseResponse<GroupInfoDto>(groupInfo);
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<object?>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> HandleGroupInvite([FromBody]HandleGroupInviteDto dto)
|
||||
{
|
||||
string userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier)!;
|
||||
await _groupService.HandleGroupInviteAsync(int.Parse(userIdStr), dto);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<object?>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> HandleGroupRequest([FromBody]HandleGroupRequestDto dto)
|
||||
{
|
||||
string userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _groupService.HandleGroupRequestAsync(int.Parse(userIdStr),dto);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<object?>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> InviteUser([FromBody]GroupInviteUserDto dto)
|
||||
{
|
||||
string userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _groupService.InviteUsersAsync(int.Parse(userIdStr), dto.GroupId, dto.Ids);
|
||||
var res = new BaseResponse<object?>();
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,55 +1,55 @@
|
||||
using IM_API.Application.Interfaces;
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Message;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.VOs.Message;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class MessageController : ControllerBase
|
||||
{
|
||||
private readonly IMessageSevice _messageService;
|
||||
private readonly ILogger<MessageController> _logger;
|
||||
private readonly IEventBus _eventBus;
|
||||
public MessageController(IMessageSevice messageService, ILogger<MessageController> logger, IEventBus eventBus)
|
||||
{
|
||||
_messageService = messageService;
|
||||
_logger = logger;
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<MessageBaseVo>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> SendMessage(MessageBaseDto dto)
|
||||
{
|
||||
var userIdstr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
MessageBaseVo messageBaseVo = new MessageBaseVo();
|
||||
if(dto.ChatType == Models.ChatType.PRIVATE)
|
||||
{
|
||||
messageBaseVo = await _messageService.SendPrivateMessageAsync(int.Parse(userIdstr), dto.ReceiverId, dto);
|
||||
}
|
||||
else
|
||||
{
|
||||
messageBaseVo = await _messageService.SendGroupMessageAsync(int.Parse(userIdstr), dto.ReceiverId, dto);
|
||||
}
|
||||
return Ok(new BaseResponse<MessageBaseVo>(messageBaseVo));
|
||||
}
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(BaseResponse<List<MessageBaseVo>>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetMessageList([FromQuery]MessageQueryDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var msgList = await _messageService.GetMessagesAsync(int.Parse(userIdStr),dto);
|
||||
var res = new BaseResponse<List<MessageBaseVo>>(msgList);
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
using IM_API.Application.Interfaces;
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Message;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.VOs.Message;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class MessageController : ControllerBase
|
||||
{
|
||||
private readonly IMessageSevice _messageService;
|
||||
private readonly ILogger<MessageController> _logger;
|
||||
private readonly IEventBus _eventBus;
|
||||
public MessageController(IMessageSevice messageService, ILogger<MessageController> logger, IEventBus eventBus)
|
||||
{
|
||||
_messageService = messageService;
|
||||
_logger = logger;
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<MessageBaseVo>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> SendMessage(MessageBaseDto dto)
|
||||
{
|
||||
var userIdstr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
MessageBaseVo messageBaseVo = new MessageBaseVo();
|
||||
if(dto.ChatType == Models.ChatType.PRIVATE)
|
||||
{
|
||||
messageBaseVo = await _messageService.SendPrivateMessageAsync(int.Parse(userIdstr), dto.ReceiverId, dto);
|
||||
}
|
||||
else
|
||||
{
|
||||
messageBaseVo = await _messageService.SendGroupMessageAsync(int.Parse(userIdstr), dto.ReceiverId, dto);
|
||||
}
|
||||
return Ok(new BaseResponse<MessageBaseVo>(messageBaseVo));
|
||||
}
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(BaseResponse<List<MessageBaseVo>>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetMessageList([FromQuery]MessageQueryDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var msgList = await _messageService.GetMessagesAsync(int.Parse(userIdStr),dto);
|
||||
var res = new BaseResponse<List<MessageBaseVo>>(msgList);
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,114 +1,114 @@
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Tools;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
private readonly ILogger<UserController> _logger;
|
||||
public UserController(IUserService userService, ILogger<UserController> logger)
|
||||
{
|
||||
_userService = userService;
|
||||
_logger = logger;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前用户信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Me()
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var userinfo = await _userService.GetUserInfoAsync(userId);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 修改用户资料
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Profile(UpdateUserDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var userinfo = await _userService.UpdateUserAsync(userId, dto);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// ID查询用户
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Find(int userId)
|
||||
{
|
||||
var userinfo = await _userService.GetUserInfoAsync(userId);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 用户名查询用户
|
||||
/// </summary>
|
||||
/// <param name="username"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> FindByUsername(string username)
|
||||
{
|
||||
var userinfo = await _userService.GetUserInfoByUsernameAsync(username);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 重置用户密码
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> ResetPassword(PasswordResetDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
await _userService.ResetPasswordAsync(userId, dto.OldPassword, dto.Password);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置在线状态
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> SetOnlineStatus(OnlineStatusSetDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
await _userService.UpdateOlineStatusAsync(userId, dto.OnlineStatus);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<List<UserInfoDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetUserList([FromBody][Required]List<int> ids)
|
||||
{
|
||||
var users = await _userService.GetUserInfoListAsync(ids);
|
||||
var res = new BaseResponse<List<UserInfoDto>>(users);
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Tools;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IM_API.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
private readonly ILogger<UserController> _logger;
|
||||
public UserController(IUserService userService, ILogger<UserController> logger)
|
||||
{
|
||||
_userService = userService;
|
||||
_logger = logger;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前用户信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Me()
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var userinfo = await _userService.GetUserInfoAsync(userId);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 修改用户资料
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Profile(UpdateUserDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var userinfo = await _userService.UpdateUserAsync(userId, dto);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// ID查询用户
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Find(int userId)
|
||||
{
|
||||
var userinfo = await _userService.GetUserInfoAsync(userId);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 用户名查询用户
|
||||
/// </summary>
|
||||
/// <param name="username"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> FindByUsername(string username)
|
||||
{
|
||||
var userinfo = await _userService.GetUserInfoByUsernameAsync(username);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 重置用户密码
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> ResetPassword(PasswordResetDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
await _userService.ResetPasswordAsync(userId, dto.OldPassword, dto.Password);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置在线状态
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> SetOnlineStatus(OnlineStatusSetDto dto)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
await _userService.UpdateOlineStatusAsync(userId, dto.OnlineStatus);
|
||||
return Ok(new BaseResponse<object?>());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BaseResponse<List<UserInfoDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetUserList([FromBody][Required]List<int> ids)
|
||||
{
|
||||
var users = await _userService.GetUserInfoListAsync(ids);
|
||||
var res = new BaseResponse<List<UserInfoDto>>(users);
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user