58 lines
1.9 KiB
C#
58 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 IArticlesRepository _articles;
|
|
private readonly ILogger<ArticlesController> _logger;
|
|
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}")]
|
|
[EndpointDescription("Returns the article based on the Id value given.")]
|
|
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;
|
|
}
|
|
} |