cleaned up the article handler with new reponse models and moving to the repo structs

This commit is contained in:
James Tombleson 2024-04-28 11:40:19 -07:00
parent 9586c6a544
commit ef15af6cbd

View File

@ -4,186 +4,148 @@ import (
"net/http"
"strconv"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models"
"github.com/google/uuid"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
"github.com/labstack/echo/v4"
)
//func (s *Handler) GetArticleRouter() http.Handler {
// r := chi.NewRouter()
//
// r.Get("/", s.listArticles)
// r.Route("/{ID}", func(r chi.Router) {
// r.Get("/", s.getArticle)
// r.Get("/details", s.getArticleDetails)
// })
// r.Get("/by/sourceid", s.ListArticlesBySourceId)
//
// return r
//}
type ArticlesListResults struct {
ApiStatusModel
Payload []models.ArticleDto `json:"payload"`
}
type ArticleGetResults struct {
ApiStatusModel
Payload models.ArticleDto `json:"payload"`
}
type ArticleDetailsResult struct {
ApiStatusModel
Payload models.ArticleDetailsDto `json:"payload"`
}
// 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} ArticlesListResults "OK"
// @Success 200 {object} domain.ArticleResponse
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
func (s *Handler) listArticles(c echo.Context) error {
p := ArticlesListResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
resp := domain.ArticleResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
queryPage := c.QueryParam("page")
// if a page number was sent, process it
if len(queryPage) >= 1 {
page, err := strconv.Atoi(queryPage)
page, err := strconv.Atoi(c.QueryParam("page"))
if err != nil {
return c.JSON(http.StatusBadRequest, err)
page = 0
}
res, err := s.dto.ListArticles(c.Request().Context(), 25, page)
res, err := s.repo.Articles.ListByPage(c.Request().Context(), page, 25)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = res
return c.JSON(http.StatusOK, p)
} else {
res, err := s.dto.ListArticles(c.Request().Context(), 25, 0)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = res
return c.JSON(http.StatusOK, p)
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 "uuid"
// @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
func (s *Handler) getArticle(c echo.Context) error {
p := ArticleGetResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
p := domain.ArticleResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
id := c.Param("ID")
uuid, err := uuid.Parse(id)
idNumber, err := strconv.Atoi(id)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
s.WriteError(c, err, http.StatusBadRequest)
}
res, err := s.dto.GetArticle(c.Request().Context(), uuid)
item, err := s.repo.Articles.GetById(c.Request().Context(), int64(idNumber))
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = res
var dtos []domain.ArticleDto
dtos = append(dtos, services.ArticleToDto(item))
p.Payload = dtos
return c.JSON(http.StatusOK, p)
}
// GetArticleDetails
// @Summary Returns an article and source based on defined ID.
// @Param ID path string true "uuid"
// @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
func (s *Handler) getArticleDetails(c echo.Context) error {
p := ArticleDetailsResult{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
p := domain.ArticleDetailResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
Payload: domain.ArticleAndSourceModel{
},
}
id := c.Param("ID")
uuid, err := uuid.Parse(id)
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return c.JSON(http.StatusBadRequest, err)
s.WriteError(c, err, http.StatusBadRequest)
}
res, err := s.dto.GetArticleDetails(c.Request().Context(), uuid)
article, err := s.repo.Articles.GetById(c.Request().Context(), int64(id))
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
s.WriteError(c, err, http.StatusInternalServerError)
}
p.Payload = res
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)
return c.JSON(http.StatusOK, p)
}
// TODO add page support
// ListArticlesBySourceID
// @Summary Finds the articles based on the SourceID provided. Returns the top 25.
// @Param id query string true "Source ID UUID"
// @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
func (s *Handler) ListArticlesBySourceId(c echo.Context) error {
p := ArticlesListResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
p := domain.ArticleResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
id := c.QueryParam("id")
uuid, err := uuid.Parse(id)
id, err := strconv.Atoi(c.QueryParam("id"))
if err != nil {
return c.JSON(http.StatusBadRequest, err)
s.WriteError(c, err, http.StatusBadRequest)
}
// if a page number was sent, process it
if len(c.QueryParam("page")) >= 1 {
// if the page number is missing, default to 0
_page, err := strconv.Atoi(c.QueryParam("page"))
if err != nil {
return c.JSON(http.StatusBadRequest, err)
_page = 0
}
res, err := s.dto.ListNewArticlesBySourceId(c.Request().Context(), uuid, 25, _page)
items, err := s.repo.Articles.ListBySource(c.Request().Context(), _page, 25, id, "")
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = res
} else {
res, err := s.dto.ListNewArticlesBySourceId(c.Request().Context(), uuid, 25, 0)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = res
}
p.Payload = services.ArticlesToDto(items)
return c.JSON(http.StatusOK, p)
}