SoftTraderBackend/Controller/AlertController.cs
2026-03-20 10:42:48 -04:00

78 lines
2.1 KiB
C#

using Backend.DatabaseHandler;
using Backend.Interface;
using Microsoft.AspNetCore.Mvc;
namespace Backend.Controllers;
[ApiController]
[Route("api/alerts")]
public sealed class AlertController : ControllerBase
{
private readonly IAlertStore _store;
public AlertController(IAlertStore store)
{
_store = store;
}
[HttpPost("create")]
public async Task<ActionResult<AlertRecord>> Create([FromBody] CreateAlertRequest request)
{
if (string.IsNullOrWhiteSpace(request.Username))
return BadRequest("Username is required.");
if (string.IsNullOrWhiteSpace(request.Symbol))
return BadRequest("Symbol is required.");
if (string.IsNullOrWhiteSpace(request.AlertType))
return BadRequest("AlertType is required.");
var alert = new AlertRecord
{
Username = request.Username,
Symbol = request.Symbol,
AlertType = request.AlertType,
Parameters = request.Parameters ?? "{}"
};
var created = await _store.CreateAlertAsync(alert);
return Ok(created);
}
[HttpGet("list")]
public async Task<ActionResult<List<AlertRecord>>> List([FromQuery] string username)
{
if (string.IsNullOrWhiteSpace(username))
return BadRequest("Username is required.");
var alerts = await _store.GetAlertsByUserAsync(username);
return Ok(alerts);
}
[HttpGet("triggered")]
public async Task<ActionResult<List<AlertRecord>>> Triggered([FromQuery] string username)
{
if (string.IsNullOrWhiteSpace(username))
return BadRequest("Username is required.");
var alerts = await _store.GetAlertsByUserAsync(username);
return Ok(alerts.Where(a => a.IsTriggered).ToList());
}
[HttpDelete("delete")]
public async Task<ActionResult<object>> Delete([FromQuery] string username, [FromQuery] long id)
{
if (string.IsNullOrWhiteSpace(username))
return BadRequest("Username is required.");
bool success = await _store.DeleteAlertAsync(username, id);
return Ok(new { success });
}
}
public sealed class CreateAlertRequest
{
public string Username { get; set; } = string.Empty;
public string Symbol { get; set; } = string.Empty;
public string AlertType { get; set; } = string.Empty;
public string? Parameters { get; set; }
}