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