Newsbot.Collector/Newsbot.Collector.Api/Controllers/ArticlesController.cs
James Tombleson 799668a059
Features/pulling GitHub (#9)
* Still working though it but looking good on releases

* added the example discord message test

* updated source repo to return an existing record before a new is added

* updated the sources repo interface

* updated new routes to check for existing records

* starting to migrate the seed out of the sql migrations

* A new seed script was made to reload the db from the api

* Docker image works locally

* Adding CI to build docker image

* ... disabled swagger so I can test docker

* Added more to the github job but its not finished.  Isnt pulling sources yet.

* cleaned up formatting

* Controller updates to look for existing records when requesting a new one

* null check cleanup

* namespace fix
2023-03-11 10:43:06 -08:00

63 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newsbot.Collector.Database.Repositories;
using Newsbot.Collector.Domain.Dto;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Api.Controllers;
[ApiController]
[Route("api/articles")]
public class ArticlesController : ControllerBase
{
private readonly ILogger<ArticlesController> _logger;
private readonly IArticlesRepository _articles;
private readonly ISourcesRepository _sources;
public ArticlesController(ILogger<ArticlesController> logger, IOptions<ConnectionStrings> settings)
{
_logger = logger;
_articles = new ArticlesTable(settings.Value.Database);
_sources = new SourcesTable(settings.Value.Database);
}
[HttpGet(Name = "GetArticles")]
public IEnumerable<ArticleDto> Get()
{
var res = new List<ArticleDto>();
var items = _articles.List(0, 25);
foreach (var item in items)
{
res.Add(ArticleDto.Convert(item));
}
return res;
}
[HttpGet("{id:guid}")]
public ArticleDto GetById(Guid id)
{
var item = _articles.GetById(id);
return ArticleDto.Convert(item);
}
[HttpGet("{id:guid}/details")]
public ArticleDetailsDto GetDetailsById(Guid id)
{
var item = _articles.GetById(id);
var sourceItem = _sources.GetByID(item.SourceID);
return ArticleDetailsDto.Convert(item, sourceItem);
}
[HttpGet("by/{sourceId:guid}")]
public IEnumerable<ArticleDto> GetBySourceId(Guid sourceId, int page = 0, int count = 25)
{
var res = new List<ArticleDto>();
var items = _articles.ListBySourceId(sourceId, page, count);
foreach (var item in items)
{
res.Add(ArticleDto.Convert(item));
}
return res;
}
}