1
0
tinklu-paslaugos/Lab4/ChatRoom/ChatRoomRestController.cs
2024-12-07 17:21:09 +02:00

104 lines
3.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
namespace ChatRoom;
/// <summary>
/// Service. Class must be marked public, otherwise ASP.NET core runtime will not find it.
///
/// Look into FromXXX attributes if you need to map inputs to custom parts of HTTP request.
/// </summary>
[Route("chatRoom")]
[ApiController]
public class ChatRoomRestController : ControllerBase
{
/// <summary>
/// Service logic. This is created in Server.StartServer() and received through DI in constructor.
/// </summary>
private readonly ChatRoomLogic logic;
/// <summary>
/// Constructor
/// </summary>
/// <param name="logic">Logic to use. This will get passed through DI.</param>
public ChatRoomRestController(ChatRoomLogic logic)
{
this.logic = logic;
}
/// <summary>
/// Register client with a name
/// </summary>
/// <param name="name">Name of client, can be duplicate between clients</param>
/// <returns>Client ID</returns>
[HttpPost("/registerClient")]
public ActionResult<int> RegisterClient(string name)
{
return logic.RegisterClient(name);
}
/// <summary>
/// Get number of strikes a participant has
/// </summary>
/// <param name="clientId">Client ID</param>
/// <returns>Number of strikes</returns>
[HttpGet("/getStrikes")]
public ActionResult<int> GetStrikes(int clientId)
{
return logic.GetStrikes(clientId);
}
/// <summary>
/// Get timestamp until when the client is blocked
/// </summary>
/// <param name="clientId">Client ID</param>
/// <returns>Optional datetime object</returns>
[HttpGet("/getBlockedUntil")]
public ActionResult<DateTime?> GetBlockedUntil(int clientId)
{
return logic.GetBlockedUntil(clientId);
}
/// <summary>
/// Send a message, will be given to a moderator to be approved
/// </summary>
/// <param name="clientId">Client ID</param>
/// <param name="contents">Message contents</param>
/// <param name="needsToBeCensored">Does this message need to be censored?</param>
/// <returns>Was sending successful, can fail if user is blocked</returns>
[HttpPost("/sendMessage")]
public ActionResult<bool> SendMessage(int clientId, string contents, bool needsToBeCensored)
{
return logic.SendMessage(clientId, contents, needsToBeCensored);
}
/// <summary>
/// Get the next message which hasn't been approved or rejected
/// </summary>
/// <returns>Message object. Returns null if there is no message</returns>
[HttpGet("/getNewMessage")]
public ActionResult<ChatRoomContract.Message?> GetNewMessage()
{
return logic.GetNewMessage();
}
/// <summary>
/// Reject a message
/// </summary>
/// <param name="messageId">Message ID</param>
[HttpPost("/rejectMessage")]
public void RejectMessage(int messageId)
{
logic.RejectMessage(messageId);
}
/// <summary>
/// Approve a message
/// </summary>
/// <param name="messageId">Message ID</param>
[HttpPost("/approveMessage")]
public void ApproveMessage(int messageId)
{
logic.ApproveMessage(messageId);
}
}