2024-04-23 07:15:38 -07:00
|
|
|
package v1
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-01-31 08:19:23 -08:00
|
|
|
"strconv"
|
2022-06-19 22:02:44 -07:00
|
|
|
|
2024-05-09 19:08:43 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/domain"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv"
|
2024-04-23 22:18:07 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2022-06-19 22:02:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListArticles
|
2024-07-17 22:01:29 -07:00
|
|
|
// @Summary Lists the top 25 records ordering from newest to oldest.
|
|
|
|
// @Produce application/json
|
|
|
|
// @Param page query int true "page number" test
|
|
|
|
// @Tags Articles
|
|
|
|
// @Router /v1/articles [get]
|
|
|
|
// @Success 200 {object} domain.ArticleResponse
|
|
|
|
// @Failure 400 {object} domain.BaseResponse
|
|
|
|
// @Failure 500 {object} domain.BaseResponse
|
|
|
|
// @Security Bearer
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) listArticles(c echo.Context) error {
|
2024-04-28 11:40:19 -07:00
|
|
|
resp := domain.ArticleResponse{
|
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
2024-06-02 17:27:36 -07:00
|
|
|
IsError: true,
|
2023-01-22 10:12:55 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-02 17:27:36 -07:00
|
|
|
_, err := s.ValidateJwtToken(c, domain.ScopeArticleRead)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusUnauthorized, resp)
|
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
page, err := strconv.Atoi(c.QueryParam("page"))
|
|
|
|
if err != nil {
|
|
|
|
page = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.repo.Articles.ListByPage(c.Request().Context(), page, 25)
|
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, resp)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2024-04-28 11:40:19 -07:00
|
|
|
|
2024-05-09 19:08:43 -07:00
|
|
|
resp.Payload = dtoconv.ArticlesToDto(res)
|
2024-06-02 17:27:36 -07:00
|
|
|
resp.BaseResponse.IsError = false
|
2024-04-28 11:40:19 -07:00
|
|
|
return c.JSON(http.StatusOK, resp)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetArticle
|
2024-07-17 22:01:29 -07:00
|
|
|
// @Summary Returns an article based on defined ID.
|
|
|
|
// @Param id path string true "int"
|
|
|
|
// @Produce application/json
|
|
|
|
// @Tags Articles
|
|
|
|
// @Router /v1/articles/{id} [get]
|
|
|
|
// @Success 200 {object} domain.ArticleResponse "OK"
|
|
|
|
// @Failure 400 {object} domain.BaseResponse
|
|
|
|
// @Failure 500 {object} domain.BaseResponse
|
|
|
|
// @Security Bearer
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) getArticle(c echo.Context) error {
|
2024-04-28 11:40:19 -07:00
|
|
|
p := domain.ArticleResponse{
|
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
2024-06-02 17:27:36 -07:00
|
|
|
IsError: true,
|
2023-01-22 10:12:55 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-02 17:27:36 -07:00
|
|
|
_, err := s.ValidateJwtToken(c, domain.ScopeArticleRead)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusUnauthorized, p)
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
id := c.Param("ID")
|
2024-04-28 11:40:19 -07:00
|
|
|
idNumber, err := strconv.Atoi(id)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, p)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
item, err := s.repo.Articles.GetById(c.Request().Context(), int64(idNumber))
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, p)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
var dtos []domain.ArticleDto
|
2024-05-09 19:08:43 -07:00
|
|
|
dtos = append(dtos, dtoconv.ArticleToDto(item))
|
2024-04-28 11:40:19 -07:00
|
|
|
p.Payload = dtos
|
2023-01-22 10:12:55 -08:00
|
|
|
|
2024-06-02 17:27:36 -07:00
|
|
|
p.BaseResponse.IsError = false
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetArticleDetails
|
2024-07-17 22:01:29 -07:00
|
|
|
// @Summary Returns an article and source based on defined ID.
|
|
|
|
// @Param id path string true "int"
|
|
|
|
// @Produce application/json
|
|
|
|
// @Tags Articles
|
|
|
|
// @Router /v1/articles/{id}/details [get]
|
|
|
|
// @Success 200 {object} domain.ArticleDetailedResponse "OK"
|
|
|
|
// @Failure 400 {object} domain.BaseResponse
|
|
|
|
// @Failure 500 {object} domain.BaseResponse
|
|
|
|
// @Security Bearer
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) getArticleDetails(c echo.Context) error {
|
2024-04-28 12:32:51 -07:00
|
|
|
p := domain.ArticleDetailedResponse{
|
2024-04-28 11:40:19 -07:00
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
2024-06-02 17:27:36 -07:00
|
|
|
IsError: true,
|
2024-04-28 11:40:19 -07:00
|
|
|
},
|
2024-05-07 19:14:37 -07:00
|
|
|
Payload: domain.ArticleAndSourceModel{},
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
|
2024-06-02 17:27:36 -07:00
|
|
|
_, err := s.ValidateJwtToken(c, domain.ScopeArticleRead)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusUnauthorized, p)
|
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
id, err := strconv.Atoi(c.Param("ID"))
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, p)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
article, err := s.repo.Articles.GetById(c.Request().Context(), int64(id))
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, p)
|
2024-04-28 11:40:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
source, err := s.repo.Sources.GetById(c.Request().Context(), article.SourceID)
|
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, p)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:43 -07:00
|
|
|
p.Payload.Article = dtoconv.ArticleToDto(article)
|
|
|
|
p.Payload.Source = dtoconv.SourceToDto(source)
|
2024-06-02 17:27:36 -07:00
|
|
|
p.BaseResponse.IsError = false
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
2023-02-04 21:43:07 -08:00
|
|
|
// ListArticlesBySourceID
|
2024-07-17 22:01:29 -07:00
|
|
|
// @Summary Finds the articles based on the SourceID provided. Returns the top 25.
|
|
|
|
// @Param id path string true "source id"
|
|
|
|
// @Param page query int false "Page to query"
|
|
|
|
// @Produce application/json
|
|
|
|
// @Tags Articles
|
|
|
|
// @Router /v1/articles/by/source/{id} [get]
|
|
|
|
// @Success 200 {object} domain.ArticleResponse "OK"
|
|
|
|
// @Failure 400 {object} domain.BaseResponse
|
|
|
|
// @Failure 500 {object} domain.BaseResponse
|
|
|
|
// @Security Bearer
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) ListArticlesBySourceId(c echo.Context) error {
|
2024-04-28 11:40:19 -07:00
|
|
|
p := domain.ArticleResponse{
|
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
2024-06-02 17:27:36 -07:00
|
|
|
IsError: true,
|
2023-01-31 08:19:23 -08:00
|
|
|
},
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2024-06-02 17:27:36 -07:00
|
|
|
_, err := s.ValidateJwtToken(c, domain.ScopeArticleRead)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusUnauthorized, p)
|
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
id, err := strconv.Atoi(c.QueryParam("id"))
|
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, p)
|
2024-04-28 11:40:19 -07:00
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
// if the page number is missing, default to 0
|
|
|
|
_page, err := strconv.Atoi(c.QueryParam("page"))
|
2022-07-12 15:28:31 -07:00
|
|
|
if err != nil {
|
2024-04-28 11:40:19 -07:00
|
|
|
_page = 0
|
2024-04-23 07:15:38 -07:00
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
items, err := s.repo.Articles.ListBySource(c.Request().Context(), _page, 25, id, "")
|
|
|
|
if err != nil {
|
2024-06-02 17:27:36 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, p)
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:43 -07:00
|
|
|
p.Payload = dtoconv.ArticlesToDto(items)
|
2024-06-02 17:27:36 -07:00
|
|
|
p.BaseResponse.IsError = false
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|