48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
using GameAuthApi.Models;
|
|
using GameAuthApi.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GameAuthApi.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class ChatController : ControllerBase
|
|
{
|
|
private readonly ChatService _chatService;
|
|
|
|
public ChatController(ChatService chatService)
|
|
{
|
|
_chatService = chatService;
|
|
}
|
|
|
|
[HttpPost("send")]
|
|
[Authorize(Roles = "USER,SUPER")]
|
|
public async Task<IActionResult> Send([FromBody] ChatMessageRequest req)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(req.Content))
|
|
return BadRequest("Content required");
|
|
|
|
var senderId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var msg = new ChatMessage
|
|
{
|
|
SenderId = senderId,
|
|
Content = req.Content
|
|
};
|
|
await _chatService.CreateAsync(msg);
|
|
return Ok(msg);
|
|
}
|
|
|
|
[HttpGet("recent")]
|
|
[Authorize(Roles = "USER,SUPER")]
|
|
public async Task<IActionResult> Recent([FromQuery] int count = 10)
|
|
{
|
|
if (count <= 0)
|
|
count = 10;
|
|
var messages = await _chatService.GetRecentAsync(count);
|
|
return Ok(messages);
|
|
}
|
|
}
|