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 string false "page number" // @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 { _, err := s.ValidateJwtToken(c, domain.ScopeArticleRead) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } resp := domain.ArticleResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } 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 s.WriteError(c, err, http.StatusInternalServerError) } resp.Payload = dtoconv.ArticlesToDto(res) 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 { _, err := s.ValidateJwtToken(c, domain.ScopeArticleRead) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } p := domain.ArticleResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } id := c.Param("ID") idNumber, err := strconv.Atoi(id) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } item, err := s.repo.Articles.GetById(c.Request().Context(), int64(idNumber)) if err != nil { return c.JSON(http.StatusInternalServerError, err) } var dtos []domain.ArticleDto dtos = append(dtos, dtoconv.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 "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 { _, err := s.ValidateJwtToken(c, domain.ScopeArticleRead) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } p := domain.ArticleDetailedResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, Payload: domain.ArticleAndSourceModel{}, } id, err := strconv.Atoi(c.Param("ID")) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } article, err := s.repo.Articles.GetById(c.Request().Context(), int64(id)) if err != nil { return s.WriteError(c, err, http.StatusInternalServerError) } source, err := s.repo.Sources.GetById(c.Request().Context(), article.SourceID) if err != nil { return s.WriteError(c, err, http.StatusInternalServerError) } p.Payload.Article = dtoconv.ArticleToDto(article) p.Payload.Source = dtoconv.SourceToDto(source) return c.JSON(http.StatusOK, p) } // ListArticlesBySourceID // @Summary Finds the articles based on the SourceID provided. Returns the top 25. // @Param id query string true "source id" // @Param page query int false "Page to query" // @Produce application/json // @Tags Articles // @Router /v1/articles/by/sourceId [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 { _, err := s.ValidateJwtToken(c, domain.ScopeArticleRead) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } p := domain.ArticleResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } id, err := strconv.Atoi(c.QueryParam("id")) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } // 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.StatusInternalServerError, err) } p.Payload = dtoconv.ArticlesToDto(items) return c.JSON(http.StatusOK, p) }