Files
IM_NEW/MessageService.WebApi/Controllers/Message/MessageController.cs
T

61 lines
2.3 KiB
C#

using IM.ASPNETCore;
using MessageService.Infrastructure;
using MessageService.WebApi.Application.Message;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace MessageService.WebApi.Controllers.Message
{
[Route("api/[controller]/[action]")]
[Authorize]
[ApiController]
public class MessageController : ControllerBase
{
private readonly Application.Message.MessageService service;
public MessageController(Application.Message.MessageService service)
{
this.service = service;
}
[HttpPost]
[UnitOfWork(typeof(MessageDbContext))]
public async Task<IActionResult> Send(MessageSendRequest request)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var command = request.ToCommand(Guid.Parse(userId));
return Ok(await service.SendMsgAsync(command));
}
[HttpPost]
[UnitOfWork(typeof(MessageDbContext))]
public async Task<IActionResult> WithDraw([FromQuery] Guid msgId)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
return Ok(await service.WithDrawMsgAsync(msgId, Guid.Parse(userId)));
}
[HttpGet]
public async Task<IActionResult> GetMessages([FromQuery]Guid conversationId, long? cursor, int direction, int limit, CancellationToken cancellationToken)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var command = new GetMessageCommand(conversationId, Guid.Parse(userId), cursor, direction, limit);
return Ok(await service.GetMessagesAsync(command, cancellationToken));
}
[HttpGet]
public async Task<IActionResult> Search(
[FromQuery] Guid conversationId,
[FromQuery] string keyword,
long? cursor,
int limit = 30,
CancellationToken cancellationToken = default)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var command = new SearchMessageCommand(conversationId, Guid.Parse(userId!), keyword, cursor, limit);
return Ok(await service.SearchMessagesAsync(command, cancellationToken));
}
}
}