package v1 import ( "net/http" "strconv" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" "github.com/google/uuid" "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" func (s *Handler) listArticles(c echo.Context) error { p := ArticlesListResults{ ApiStatusModel: ApiStatusModel{ Message: "OK", StatusCode: http.StatusOK, }, } queryPage := c.QueryParam("page") // if a page number was sent, process it if len(queryPage) >= 1 { page, err := strconv.Atoi(queryPage) if err != nil { return c.JSON(http.StatusBadRequest, err) } res, err := s.dto.ListArticles(c.Request().Context(), 25, page) 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) } } // GetArticle // @Summary Returns an article based on defined ID. // @Param ID path string true "uuid" // @Produce application/json // @Tags Articles // @Router /articles/{ID} [get] // @Success 200 {object} ArticleGetResults "OK" func (s *Handler) getArticle(c echo.Context) error { p := ArticleGetResults{ ApiStatusModel: ApiStatusModel{ Message: "OK", StatusCode: http.StatusOK, }, } id := c.Param("ID") uuid, err := uuid.Parse(id) if err != nil { return c.JSON(http.StatusBadRequest, err) } res, err := s.dto.GetArticle(c.Request().Context(), uuid) if err != nil { return c.JSON(http.StatusInternalServerError, err) } p.Payload = res return c.JSON(http.StatusOK, p) } // GetArticleDetails // @Summary Returns an article and source based on defined ID. // @Param ID path string true "uuid" // @Produce application/json // @Tags Articles // @Router /articles/{ID}/details [get] // @Success 200 {object} ArticleDetailsResult "OK" func (s *Handler) getArticleDetails(c echo.Context) error { p := ArticleDetailsResult{ ApiStatusModel: ApiStatusModel{ Message: "OK", StatusCode: http.StatusOK, }, } id := c.Param("ID") uuid, err := uuid.Parse(id) if err != nil { return c.JSON(http.StatusBadRequest, err) } res, err := s.dto.GetArticleDetails(c.Request().Context(), uuid) if err != nil { return c.JSON(http.StatusInternalServerError, err) } p.Payload = res 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 page query int false "Page to query" // @Produce application/json // @Tags Articles // @Router /articles/by/sourceid [get] // @Success 200 {object} ArticlesListResults "OK" func (s *Handler) ListArticlesBySourceId(c echo.Context) error { p := ArticlesListResults{ ApiStatusModel: ApiStatusModel{ Message: "OK", StatusCode: http.StatusOK, }, } id := c.QueryParam("id") uuid, err := uuid.Parse(id) if err != nil { return c.JSON(http.StatusBadRequest, err) } // if a page number was sent, process it if len(c.QueryParam("page")) >= 1 { _page, err := strconv.Atoi(c.QueryParam("page")) if err != nil { return c.JSON(http.StatusBadRequest, err) } res, err := s.dto.ListNewArticlesBySourceId(c.Request().Context(), uuid, 25, _page) 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 } return c.JSON(http.StatusOK, p) }