using System.ComponentModel.DataAnnotations; using System.Text.Json; using Microsoft.AspNetCore.Mvc; using ScrapperAPI.Dtos; using ScrapperAPI.Interfaces; namespace ScrapperAPI.Controllers; public sealed record CreateExtractionModelRequest( [Required] string Name, [Required] JsonDocument Definition, int Version = 1, string? Description = null); [ApiController] [Route("extraction-models")] public sealed class ExtractionModelsController : ControllerBase { private readonly IExtractionModelRepository _models; public ExtractionModelsController(IExtractionModelRepository models) => _models = models; [HttpPost] public async Task Create([FromBody] CreateExtractionModelRequest req, CancellationToken ct) { var id = await _models.CreateAsync(new CreateExtractionModelDto( name: req.Name, version: req.Version <= 0 ? 1 : req.Version, description: req.Description, definition: req.Definition ), ct); return Created($"/extraction-models/{id}", new { id }); } [HttpGet] public async Task List(CancellationToken ct) => Ok(await _models.GetAllAsync(ct)); [HttpGet("{id:long}")] public async Task GetById(long id, CancellationToken ct) { var row = await _models.GetByIdAsync(id, ct); return row is null ? NotFound() : Ok(row); } }