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-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models"
|
2022-06-19 22:02:44 -07:00
|
|
|
"github.com/google/uuid"
|
2024-04-23 22:18:07 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2022-06-19 22:02:44 -07:00
|
|
|
)
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
//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
|
|
|
|
//}
|
2023-01-22 10:12:55 -08:00
|
|
|
|
|
|
|
type ArticlesListResults struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload []models.ArticleDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
type ArticleGetResults struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload models.ArticleDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ArticleDetailsResult struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload models.ArticleDetailsDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
// ListArticles
|
2023-02-04 20:15:10 -08:00
|
|
|
// @Summary Lists the top 25 records ordering from newest to oldest.
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2023-01-31 08:19:23 -08:00
|
|
|
// @Param page query string false "page number"
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /articles [get]
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Success 200 {object} ArticlesListResults "OK"
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) listArticles(c echo.Context) error {
|
2023-01-22 10:12:55 -08:00
|
|
|
p := ArticlesListResults{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
queryPage := c.QueryParam("page")
|
2023-01-31 12:39:54 -08:00
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
// if a page number was sent, process it
|
2023-02-04 20:15:10 -08:00
|
|
|
if len(queryPage) >= 1 {
|
2024-04-23 22:18:07 -07:00
|
|
|
page, err := strconv.Atoi(queryPage)
|
2023-01-31 08:19:23 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, err)
|
2023-01-31 08:19:23 -08:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.ListArticles(c.Request().Context(), 25, page)
|
2023-01-31 08:19:23 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2023-01-31 08:19:23 -08:00
|
|
|
}
|
|
|
|
p.Payload = res
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2023-01-31 08:19:23 -08:00
|
|
|
} else {
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.ListArticles(c.Request().Context(), 25, 0)
|
2023-01-31 08:19:23 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2023-01-31 08:19:23 -08:00
|
|
|
}
|
|
|
|
p.Payload = res
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetArticle
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Summary Returns an article based on defined ID.
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Param ID path string true "uuid"
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2022-12-04 08:49:17 -08:00
|
|
|
// @Router /articles/{ID} [get]
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Success 200 {object} ArticleGetResults "OK"
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) getArticle(c echo.Context) error {
|
2023-01-31 08:19:23 -08:00
|
|
|
p := ArticleGetResults{
|
2023-01-22 10:12:55 -08:00
|
|
|
ApiStatusModel: ApiStatusModel{
|
2023-01-31 08:19:23 -08:00
|
|
|
Message: "OK",
|
2023-01-22 10:12:55 -08:00
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
id := c.Param("ID")
|
2022-06-19 22:02:44 -07:00
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, err)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.GetArticle(c.Request().Context(), uuid)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetArticleDetails
|
|
|
|
// @Summary Returns an article and source based on defined ID.
|
|
|
|
// @Param ID path string true "uuid"
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Router /articles/{ID}/details [get]
|
|
|
|
// @Success 200 {object} ArticleDetailsResult "OK"
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) getArticleDetails(c echo.Context) error {
|
2023-01-31 08:19:23 -08:00
|
|
|
p := ArticleDetailsResult{
|
2023-01-22 10:12:55 -08:00
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
id := c.Param("ID")
|
2023-01-22 10:12:55 -08:00
|
|
|
uuid, err := uuid.Parse(id)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, err)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.GetArticleDetails(c.Request().Context(), uuid)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
// TODO add page support
|
2023-02-04 21:43:07 -08:00
|
|
|
// 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"
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Produce application/json
|
|
|
|
// @Tags Articles
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Router /articles/by/sourceid [get]
|
|
|
|
// @Success 200 {object} ArticlesListResults "OK"
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) ListArticlesBySourceId(c echo.Context) error {
|
2023-01-31 08:19:23 -08:00
|
|
|
p := ArticlesListResults{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
id := c.QueryParam("id")
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
uuid, err := uuid.Parse(id)
|
2022-07-12 15:28:31 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, err)
|
2024-04-23 07:15:38 -07:00
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2023-02-04 21:43:07 -08:00
|
|
|
// if a page number was sent, process it
|
2024-04-23 22:18:07 -07:00
|
|
|
if len(c.QueryParam("page")) >= 1 {
|
|
|
|
_page, err := strconv.Atoi(c.QueryParam("page"))
|
2023-02-04 21:43:07 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, err)
|
2023-02-04 21:43:07 -08:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.ListNewArticlesBySourceId(c.Request().Context(), uuid, 25, _page)
|
2023-02-04 21:43:07 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2023-02-04 21:43:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Payload = res
|
|
|
|
} else {
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.ListNewArticlesBySourceId(c.Request().Context(), uuid, 25, 0)
|
2023-02-04 21:43:07 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2023-02-04 21:43:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Payload = res
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, p)
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
}
|