From 6e8df73bd2e8c7584c40f7463f59d3b0b8f7e2a9 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Mon, 30 Jan 2023 22:34:46 -0800 Subject: [PATCH] added a func to query articles by page --- database/query.sql.go | 49 +++++++++++++++++++++++++++++++++++++++ database/schema/query.sql | 6 +++++ docs/docs.go | 28 ++++++++++++++++++++++ docs/swagger.json | 28 ++++++++++++++++++++++ docs/swagger.yaml | 18 ++++++++++++++ dto/articles.go | 18 ++++++++++++++ 6 files changed, 147 insertions(+) diff --git a/database/query.sql.go b/database/query.sql.go index 96b4f9b..34f2a84 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -948,6 +948,55 @@ func (q *Queries) ListArticlesByDate(ctx context.Context, limit int32) ([]Articl return items, nil } +const listArticlesByPage = `-- name: ListArticlesByPage :many +select id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage from articles +order by pubdate desc +offset $2 +fetch next $1 rows only +` + +type ListArticlesByPageParams struct { + Limit int32 + Offset int32 +} + +func (q *Queries) ListArticlesByPage(ctx context.Context, arg ListArticlesByPageParams) ([]Article, error) { + rows, err := q.db.QueryContext(ctx, listArticlesByPage, 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 listDiscordQueueItems = `-- name: ListDiscordQueueItems :many Select id, articleid from DiscordQueue LIMIT $1 ` diff --git a/database/schema/query.sql b/database/schema/query.sql index 4c28e94..fea4e64 100644 --- a/database/schema/query.sql +++ b/database/schema/query.sql @@ -35,6 +35,12 @@ Left Join sources On articles.sourceid = sources.id Where name = $1; +-- name: ListArticlesByPage :many +select * from articles +order by pubdate desc +offset $2 +fetch next $1 rows only; + -- name: CreateArticle :exec INSERT INTO Articles (ID, SourceId, Tags, Title, Url, PubDate, Video, VideoHeight, VideoWidth, Thumbnail, Description, AuthorName, AuthorImage) diff --git a/docs/docs.go b/docs/docs.go index f8eba2b..e33d8e2 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -35,6 +35,34 @@ const docTemplate = `{ } } }, + "/articles/by/page": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "Articles" + ], + "summary": "List 50 items based on the requested page", + "parameters": [ + { + "type": "string", + "description": "page number", + "name": "page", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/routes.ArticlesListResults" + } + } + } + } + }, "/articles/by/sourceid": { "get": { "produces": [ diff --git a/docs/swagger.json b/docs/swagger.json index 4eed4d9..a1504b1 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -26,6 +26,34 @@ } } }, + "/articles/by/page": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "Articles" + ], + "summary": "List 50 items based on the requested page", + "parameters": [ + { + "type": "string", + "description": "page number", + "name": "page", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/routes.ArticlesListResults" + } + } + } + } + }, "/articles/by/sourceid": { "get": { "produces": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 3996964..c0e237e 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -287,6 +287,24 @@ paths: summary: Returns an article and source based on defined ID. tags: - Articles + /articles/by/page: + get: + parameters: + - description: page number + in: query + name: page + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/routes.ArticlesListResults' + summary: List 50 items based on the requested page + tags: + - Articles /articles/by/sourceid: get: parameters: diff --git a/dto/articles.go b/dto/articles.go index f04797a..bc37498 100644 --- a/dto/articles.go +++ b/dto/articles.go @@ -35,6 +35,24 @@ func (c DtoClient) ListArticles(ctx context.Context, limit int) ([]models.Articl return res, nil } +func (c DtoClient) ListArticlesByPage(ctx context.Context, page, limit int32 ) ([]models.ArticleDto, error) { + var res []models.ArticleDto + + a, err := c.db.ListArticlesByPage(ctx, database.ListArticlesByPageParams{ + Limit: limit, + Offset: page * limit, + }) + if err != nil { + return res, err + } + + for _, article := range a { + res = append(res, c.convertArticle(article)) + } + + return res, nil +} + func (c DtoClient) GetArticle(ctx context.Context, ID uuid.UUID) (models.ArticleDto, error) { a, err := c.db.GetArticleByID(ctx, ID) if err != nil {