84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newsbot.Collector.Domain.Dto;
|
|
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;
|
|
|
|
public ArticlesController(IArticlesRepository articles, ISourcesRepository sources)
|
|
{
|
|
_articles = articles;
|
|
_sources = sources;
|
|
}
|
|
|
|
[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);
|
|
|
|
return new OkObjectResult(new ArticleDetailsResult
|
|
{
|
|
IsSuccessful = true,
|
|
Item = ArticleDetailsDto.Convert(item, sourceItem)
|
|
});
|
|
}
|
|
|
|
[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
|
|
});
|
|
|
|
}
|
|
} |