Newsbot.Collector/Newsbot.Collector.Api/Controllers/v1/ArticlesController.cs

89 lines
2.7 KiB
C#
Raw Permalink Normal View History

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newsbot.Collector.Domain.Dto;
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Results;
namespace Newsbot.Collector.Api.Controllers.v1;
[ApiController]
[Route("api/v1/articles")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ArticlesController : ControllerBase
{
//private readonly ILogger<ArticlesController> _logger;
private readonly IArticlesRepository _articles;
private readonly ISourcesRepository _sources;
private readonly IAuthorTable _author;
public ArticlesController(IArticlesRepository articles, ISourcesRepository sources, IAuthorTable author)
{
_articles = articles;
_sources = sources;
_author = author;
}
[HttpGet(Name = "GetArticles")]
public ActionResult<ArticleResult> Get()
{
var res = new List<ArticleDto>();
var items = _articles.List(0);
foreach (var item in items)
{
res.Add(ArticleDto.Convert(item));
}
return new OkObjectResult(new ArticleResult
{
IsSuccessful = true,
Items = res
});
}
[HttpGet("{id:guid}")]
[EndpointDescription("Returns the article based on the Id value given.")]
public ActionResult<ArticleResult> GetById(Guid id)
{
var item = _articles.GetById(id);
return new OkObjectResult(new ArticleResult
{
IsSuccessful = true,
Items = new List<ArticleDto>
{
ArticleDto.Convert(item)
}
});
}
[HttpGet("{id:guid}/details")]
public ActionResult<ArticleDetailsResult> GetDetailsById(Guid id)
{
var item = _articles.GetById(id);
var sourceItem = _sources.GetById(item.SourceId);
var author = _author.GetBySourceIdAndNameAsync(sourceItem.Id, sourceItem.Name);
author.Wait();
return new OkObjectResult(new ArticleDetailsResult
{
IsSuccessful = true,
Item = ArticleDetailsDto.Convert(item, sourceItem, author.Result)
});
}
[HttpGet("by/{sourceId:guid}")]
public ActionResult<ArticleResult> GetBySourceId(Guid sourceId, int page = 0)
{
var res = new List<ArticleDto>();
var items = _articles.ListBySourceId(sourceId, page);
foreach (var item in items) res.Add(ArticleDto.Convert(item));
return new OkObjectResult(new ArticleResult
{
IsSuccessful = true,
Items = res
});
}
}