add(Friend):添加好友相关逻辑
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using IM_API.Dtos;
|
||||
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(bool isReceived,int page,int limit,bool desc)
|
||||
{
|
||||
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = int.Parse(userIdStr);
|
||||
var list = await _friendService.GetFriendRequestListAsync(userId,isReceived,page,limit,desc);
|
||||
var res = new BaseResponse<List<FriendRequest>>(list);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理好友请求
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> HandleRequest([FromRoute]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?>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,7 @@ namespace IM_API.Controllers
|
||||
/// <param name="username"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Find(string username)
|
||||
public async Task<IActionResult> FindByUsername(string username)
|
||||
{
|
||||
var userinfo = await _userService.GetUserInfoByUsernameAsync(username);
|
||||
var res = new BaseResponse<UserInfoDto>(userinfo);
|
||||
|
||||
Reference in New Issue
Block a user