newsbot-api/internal/handler/v1/articles.go

152 lines
4.1 KiB
Go
Raw Normal View History

2024-04-23 07:15:38 -07:00
package v1
import (
"net/http"
"strconv"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
2024-04-23 22:18:07 -07:00
"github.com/labstack/echo/v4"
)
// ListArticles
// @Summary Lists the top 25 records ordering from newest to oldest.
// @Produce application/json
// @Param page query string false "page number"
// @Tags Articles
// @Router /articles [get]
// @Success 200 {object} domain.ArticleResponse
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) listArticles(c echo.Context) error {
resp := domain.ArticleResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
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 {
s.WriteError(c, err, http.StatusInternalServerError)
}
resp.Payload = services.ArticlesToDto(res)
return c.JSON(http.StatusOK, resp)
}
// GetArticle
// @Summary Returns an article based on defined ID.
// @Param ID path string true "int"
// @Produce application/json
// @Tags Articles
// @Router /articles/{ID} [get]
// @Success 200 {object} ArticleGetResults "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) getArticle(c echo.Context) error {
p := domain.ArticleResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-23 22:18:07 -07:00
id := c.Param("ID")
idNumber, err := strconv.Atoi(id)
if err != nil {
s.WriteError(c, err, http.StatusBadRequest)
}
item, err := s.repo.Articles.GetById(c.Request().Context(), int64(idNumber))
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err)
}
var dtos []domain.ArticleDto
dtos = append(dtos, services.ArticleToDto(item))
p.Payload = dtos
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// GetArticleDetails
// @Summary Returns an article and source based on defined ID.
// @Param ID path string true "int"
// @Produce application/json
// @Tags Articles
// @Router /articles/{ID}/details [get]
// @Success 200 {object} ArticleDetailsResult "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) getArticleDetails(c echo.Context) error {
p := domain.ArticleDetailResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
Payload: domain.ArticleAndSourceModel{
},
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
s.WriteError(c, err, http.StatusBadRequest)
}
article, err := s.repo.Articles.GetById(c.Request().Context(), int64(id))
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
source, err := s.repo.Sources.GetById(c.Request().Context(), article.SourceID)
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
p.Payload.Article = services.ArticleToDto(article)
p.Payload.Source = services.SourceToDto(source)
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// ListArticlesBySourceID
// @Summary Finds the articles based on the SourceID provided. Returns the top 25.
// @Param id query string true
// @Param page query int false "Page to query"
// @Produce application/json
// @Tags Articles
// @Router /articles/by/sourceid [get]
// @Success 200 {object} ArticlesListResults "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) ListArticlesBySourceId(c echo.Context) error {
p := domain.ArticleResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
id, err := strconv.Atoi(c.QueryParam("id"))
if err != nil {
s.WriteError(c, err, http.StatusBadRequest)
}
// if the page number is missing, default to 0
_page, err := strconv.Atoi(c.QueryParam("page"))
if err != nil {
_page = 0
2024-04-23 07:15:38 -07:00
}
items, err := s.repo.Articles.ListBySource(c.Request().Context(), _page, 25, id, "")
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = services.ArticlesToDto(items)
return c.JSON(http.StatusOK, p)
}