IM/backend/IM_API/Controllers/ConversationController.cs
2026-02-12 21:53:26 +08:00

57 lines
2.1 KiB
C#

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?>());
}
}
}