99 lines
2.4 KiB
C#
99 lines
2.4 KiB
C#
using Backend.Interface;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Backend.Controller
|
|
{
|
|
[ApiController]
|
|
[Route("api/annotations")]
|
|
public sealed class AnnotationController : ControllerBase
|
|
{
|
|
private readonly IAnnotationStore _store;
|
|
|
|
public AnnotationController(IAnnotationStore store)
|
|
{
|
|
_store = store;
|
|
}
|
|
|
|
[HttpGet("fetch")]
|
|
public async Task<IActionResult> Fetch(
|
|
[FromQuery] string user,
|
|
[FromQuery] string symbol,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(user))
|
|
return BadRequest("User cannot be empty.");
|
|
if (string.IsNullOrWhiteSpace(symbol))
|
|
return BadRequest("Symbol cannot be empty.");
|
|
|
|
var json = await _store.GetAsync(
|
|
user.Trim(), symbol.Trim().ToUpperInvariant(), cancellationToken);
|
|
|
|
if (json is null)
|
|
return Ok("[]");
|
|
|
|
return Content(json, "application/json");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
|
|
[HttpPost("save")]
|
|
public async Task<IActionResult> Save(
|
|
[FromQuery] string user,
|
|
[FromQuery] string symbol,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(user))
|
|
return BadRequest("User cannot be empty.");
|
|
if (string.IsNullOrWhiteSpace(symbol))
|
|
return BadRequest("Symbol cannot be empty.");
|
|
|
|
using var reader = new StreamReader(Request.Body);
|
|
string body = await reader.ReadToEndAsync(cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(body))
|
|
return BadRequest("Request body cannot be empty.");
|
|
|
|
await _store.UpsertAsync(
|
|
user.Trim(), symbol.Trim().ToUpperInvariant(), body, cancellationToken);
|
|
|
|
return Ok(new { success = true });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
|
|
[HttpGet("delete")]
|
|
public async Task<IActionResult> Delete(
|
|
[FromQuery] string user,
|
|
[FromQuery] string symbol,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(user))
|
|
return BadRequest("User cannot be empty.");
|
|
if (string.IsNullOrWhiteSpace(symbol))
|
|
return BadRequest("Symbol cannot be empty.");
|
|
|
|
await _store.DeleteAsync(
|
|
user.Trim(), symbol.Trim().ToUpperInvariant(), cancellationToken);
|
|
|
|
return Ok(new { success = true });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
}
|
|
}
|