2023-02-26 09:40:04 -08:00
|
|
|
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 IArticlesRepository _articles;
|
2023-04-10 22:59:13 -07:00
|
|
|
private readonly ILogger<ArticlesController> _logger;
|
2023-02-26 09:40:04 -08:00
|
|
|
private readonly ISourcesRepository _sources;
|
|
|
|
|
|
|
|
public ArticlesController(ILogger<ArticlesController> logger, IOptions<ConnectionStrings> settings)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
2023-03-11 10:43:06 -08:00
|
|
|
_articles = new ArticlesTable(settings.Value.Database);
|
|
|
|
_sources = new SourcesTable(settings.Value.Database);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet(Name = "GetArticles")]
|
|
|
|
public IEnumerable<ArticleDto> Get()
|
|
|
|
{
|
|
|
|
var res = new List<ArticleDto>();
|
|
|
|
var items = _articles.List(0, 25);
|
2023-04-10 22:59:13 -07:00
|
|
|
foreach (var item in items) res.Add(ArticleDto.Convert(item));
|
2023-02-26 09:40:04 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
[HttpGet("{id:guid}")]
|
2023-04-10 22:59:13 -07:00
|
|
|
[EndpointDescription("Returns the article based on the Id value given.")]
|
2023-02-26 09:40:04 -08:00
|
|
|
public ArticleDto GetById(Guid id)
|
|
|
|
{
|
|
|
|
var item = _articles.GetById(id);
|
|
|
|
return ArticleDto.Convert(item);
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
[HttpGet("{id:guid}/details")]
|
2023-02-26 09:40:04 -08:00
|
|
|
public ArticleDetailsDto GetDetailsById(Guid id)
|
|
|
|
{
|
|
|
|
var item = _articles.GetById(id);
|
|
|
|
var sourceItem = _sources.GetByID(item.SourceID);
|
|
|
|
return ArticleDetailsDto.Convert(item, sourceItem);
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
[HttpGet("by/{sourceId:guid}")]
|
|
|
|
public IEnumerable<ArticleDto> GetBySourceId(Guid sourceId, int page = 0, int count = 25)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
|
|
|
var res = new List<ArticleDto>();
|
2023-03-11 10:43:06 -08:00
|
|
|
var items = _articles.ListBySourceId(sourceId, page, count);
|
2023-04-10 22:59:13 -07:00
|
|
|
foreach (var item in items) res.Add(ArticleDto.Convert(item));
|
2023-02-26 09:40:04 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|