56 lines
2.2 KiB
C#
56 lines
2.2 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|