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-28 11:40:19 -07:00
|
|
|
"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"
|
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
|
2024-05-02 17:36:39 -07:00
|
|
|
// @Router /v1/articles [get]
|
2024-04-28 11:40:19 -07:00
|
|
|
// @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 {
|
2024-04-28 11:40:19 -07:00
|
|
|
resp := domain.ArticleResponse{
|
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
2023-01-22 10:12:55 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
s.WriteError(c, err, http.StatusInternalServerError)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2024-04-28 11:40:19 -07:00
|
|
|
|
|
|
|
resp.Payload = services.ArticlesToDto(res)
|
|
|
|
return c.JSON(http.StatusOK, resp)
|
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.
|
2024-04-28 11:40:19 -07:00
|
|
|
// @Param ID path string true "int"
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2024-05-02 17:36:39 -07:00
|
|
|
// @Router /v1/articles/{ID} [get]
|
2024-04-28 12:32:51 -07:00
|
|
|
// @Success 200 {object} domain.ArticleResponse "OK"
|
2024-04-28 11:40:19 -07:00
|
|
|
// @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 {
|
2024-04-28 11:40:19 -07:00
|
|
|
p := domain.ArticleResponse{
|
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
2023-01-22 10:12:55 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-04-28 11:40:19 -07:00
|
|
|
s.WriteError(c, err, http.StatusBadRequest)
|
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-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
var dtos []domain.ArticleDto
|
|
|
|
dtos = append(dtos, services.ArticleToDto(item))
|
|
|
|
p.Payload = dtos
|
2023-01-22 10:12:55 -08:00
|
|
|
|
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.
|
2024-04-28 11:40:19 -07:00
|
|
|
// @Param ID path string true "int"
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2024-05-02 17:36:39 -07:00
|
|
|
// @Router /v1/articles/{ID}/details [get]
|
2024-04-28 12:32:51 -07:00
|
|
|
// @Success 200 {object} domain.ArticleDetailedResponse "OK"
|
2024-04-28 11:40:19 -07:00
|
|
|
// @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 {
|
2024-04-28 12:32:51 -07:00
|
|
|
p := domain.ArticleDetailedResponse{
|
2024-04-28 11:40:19 -07:00
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
|
|
|
},
|
|
|
|
Payload: domain.ArticleAndSourceModel{
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
},
|
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
|
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-04-28 11:40:19 -07:00
|
|
|
s.WriteError(c, err, http.StatusBadRequest)
|
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-04-28 11:40:19 -07:00
|
|
|
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)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
p.Payload.Article = services.ArticleToDto(article)
|
|
|
|
p.Payload.Source = services.SourceToDto(source)
|
2023-01-22 10:12:55 -08:00
|
|
|
|
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
|
|
|
|
// @Summary Finds the articles based on the SourceID provided. Returns the top 25.
|
2024-04-28 12:32:51 -07:00
|
|
|
// @Param id query string true "source id"
|
2023-02-04 21:43:07 -08:00
|
|
|
// @Param page query int false "Page to query"
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Produce application/json
|
|
|
|
// @Tags Articles
|
2024-05-02 17:36:39 -07:00
|
|
|
// @Router /v1/articles/by/sourceid [get]
|
2024-04-28 12:32:51 -07:00
|
|
|
// @Success 200 {object} domain.ArticleResponse "OK"
|
2024-04-28 11:40:19 -07:00
|
|
|
// @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 {
|
2024-04-28 11:40:19 -07:00
|
|
|
p := domain.ArticleResponse{
|
|
|
|
BaseResponse: domain.BaseResponse{
|
|
|
|
Message: ResponseMessageSuccess,
|
2023-01-31 08:19:23 -08:00
|
|
|
},
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
id, err := strconv.Atoi(c.QueryParam("id"))
|
|
|
|
if err != nil {
|
|
|
|
s.WriteError(c, err, http.StatusBadRequest)
|
|
|
|
}
|
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 {
|
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
|
|
|
|
2024-04-28 11:40:19 -07:00
|
|
|
p.Payload = services.ArticlesToDto(items)
|
|
|
|
return c.JSON(http.StatusOK, p)
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|