diff --git a/database/query.sql.go b/database/query.sql.go index 8cca38e..97dfe45 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -1110,6 +1110,57 @@ func (q *Queries) ListNewArticlesBySourceId(ctx context.Context, arg ListNewArti return items, nil } +const listOldestArticlesBySourceId = `-- name: ListOldestArticlesBySourceId :many +SELECT id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage FROM articles +Where sourceid = $1 +ORDER BY pubdate asc +offset $3 +fetch next $2 rows only +` + +type ListOldestArticlesBySourceIdParams struct { + Sourceid uuid.UUID + Limit int32 + Offset int32 +} + +func (q *Queries) ListOldestArticlesBySourceId(ctx context.Context, arg ListOldestArticlesBySourceIdParams) ([]Article, error) { + rows, err := q.db.QueryContext(ctx, listOldestArticlesBySourceId, arg.Sourceid, arg.Limit, arg.Offset) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Article + for rows.Next() { + var i Article + if err := rows.Scan( + &i.ID, + &i.Sourceid, + &i.Tags, + &i.Title, + &i.Url, + &i.Pubdate, + &i.Video, + &i.Videoheight, + &i.Videowidth, + &i.Thumbnail, + &i.Description, + &i.Authorname, + &i.Authorimage, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const listSources = `-- name: ListSources :many Select id, site, name, source, type, value, enabled, url, tags, deleted From Sources Limit $1 ` diff --git a/database/schema/query.sql b/database/schema/query.sql index bdb7ca8..f65ed96 100644 --- a/database/schema/query.sql +++ b/database/schema/query.sql @@ -30,6 +30,14 @@ ORDER BY pubdate desc offset $3 fetch next $2 rows only; +-- name: ListOldestArticlesBySourceId :many +SELECT * FROM articles +Where sourceid = $1 +ORDER BY pubdate asc +offset $3 +fetch next $2 rows only; + + -- name: ListArticlesBySourceId :many Select * From articles Where sourceid = $1 diff --git a/docs/docs.go b/docs/docs.go index 169c5cd..7cc13e7 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -51,7 +51,7 @@ const docTemplate = `{ "tags": [ "Articles" ], - "summary": "Finds the articles based on the SourceID provided. Returns the top 50.", + "summary": "Finds the articles based on the SourceID provided. Returns the top 25.", "parameters": [ { "type": "string", @@ -59,6 +59,12 @@ const docTemplate = `{ "name": "id", "in": "query", "required": true + }, + { + "type": "integer", + "description": "Page to query", + "name": "page", + "in": "query" } ], "responses": { diff --git a/docs/swagger.json b/docs/swagger.json index b0d109d..b6d4557 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -42,7 +42,7 @@ "tags": [ "Articles" ], - "summary": "Finds the articles based on the SourceID provided. Returns the top 50.", + "summary": "Finds the articles based on the SourceID provided. Returns the top 25.", "parameters": [ { "type": "string", @@ -50,6 +50,12 @@ "name": "id", "in": "query", "required": true + }, + { + "type": "integer", + "description": "Page to query", + "name": "page", + "in": "query" } ], "responses": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 65fbaeb..cb9d714 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -300,6 +300,10 @@ paths: name: id required: true type: string + - description: Page to query + in: query + name: page + type: integer produces: - application/json responses: @@ -308,7 +312,7 @@ paths: schema: $ref: '#/definitions/routes.ArticlesListResults' summary: Finds the articles based on the SourceID provided. Returns the top - 50. + 25. tags: - Articles /discord/webhooks: diff --git a/dto/articles.go b/dto/articles.go index c526050..b41366f 100644 --- a/dto/articles.go +++ b/dto/articles.go @@ -81,9 +81,13 @@ func (c *DtoClient) GetArticleDetails(ctx context.Context, ID uuid.UUID) (models return res, nil } -func (c *DtoClient) GetArticlesBySourceId(ctx context.Context, SourceID uuid.UUID) ([]models.ArticleDto, error) { +func (c *DtoClient) ListNewArticlesBySourceId(ctx context.Context, SourceID uuid.UUID, limit, page int) ([]models.ArticleDto, error) { var res []models.ArticleDto - a, err := c.db.ListArticlesBySourceId(ctx, SourceID) + a, err := c.db.ListNewArticlesBySourceId(ctx, database.ListNewArticlesBySourceIdParams{ + Sourceid: SourceID, + Limit: int32(limit), + Offset: int32(limit * page), + }) if err != nil { return res, err } diff --git a/routes/articles.go b/routes/articles.go index 77fad7e..6e88c20 100644 --- a/routes/articles.go +++ b/routes/articles.go @@ -17,7 +17,7 @@ func (s *Server) GetArticleRouter() http.Handler { r.Get("/", s.getArticle) r.Get("/details", s.getArticleDetails) }) - r.Get("/by/sourceid", s.GetArticlesBySourceId) + r.Get("/by/sourceid", s.ListArticlesBySourceId) return r } @@ -148,14 +148,15 @@ func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) { } // TODO add page support -// GetArticlesBySourceID -// @Summary Finds the articles based on the SourceID provided. Returns the top 50. -// @Param id query string true "Source ID UUID" +// 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 *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) { +func (s *Server) ListArticlesBySourceId(w http.ResponseWriter, r *http.Request) { p := ArticlesListResults{ ApiStatusModel: ApiStatusModel{ Message: "OK", @@ -167,18 +168,38 @@ func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() _id := query["id"][0] + uuid, err := uuid.Parse(_id) if err != nil { s.WriteError(w, err.Error(), http.StatusBadRequest) return + } + + // if a page number was sent, process it + if len(query["page"]) >= 1 { + _page, err := strconv.Atoi(query["page"][0]) + if err != nil { + s.WriteError(w, err.Error(), http.StatusBadRequest) + return + } + + res, err := s.dto.ListNewArticlesBySourceId(r.Context(), uuid, 25, _page) + if err != nil { + s.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + + p.Payload = res + s.WriteJson(w, p) + } else { + res, err := s.dto.ListNewArticlesBySourceId(r.Context(), uuid, 25, 0) + if err != nil { + s.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + + p.Payload = res + s.WriteJson(w, p) } - res, err := s.dto.GetArticlesBySourceId(r.Context(), uuid) - if err != nil { - s.WriteError(w, err.Error(), http.StatusInternalServerError) - return - } - p.Payload = res - - s.WriteJson(w, p) }