From 118b7eb5e2d7b89f2e143ac073606b6ece89362c Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sat, 4 Feb 2023 20:15:10 -0800 Subject: [PATCH] updated ArticlesList to always list the newest by default (#40) --- database/query.sql.go | 197 +++++++++++++++++++++----------------- database/schema/query.sql | 20 ++-- docs/docs.go | 2 +- docs/swagger.json | 2 +- docs/swagger.yaml | 2 +- dto/articles.go | 9 +- routes/articles.go | 8 +- 7 files changed, 135 insertions(+), 105 deletions(-) diff --git a/database/query.sql.go b/database/query.sql.go index 34f2a84..8cca38e 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -429,48 +429,6 @@ func (q *Queries) GetArticlesBySource(ctx context.Context, site string) ([]GetAr return items, nil } -const getArticlesBySourceId = `-- name: GetArticlesBySourceId :many -Select id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage From articles -Where sourceid = $1 Limit 50 -` - -func (q *Queries) GetArticlesBySourceId(ctx context.Context, sourceid uuid.UUID) ([]Article, error) { - rows, err := q.db.QueryContext(ctx, getArticlesBySourceId, sourceid) - 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 getArticlesBySourceName = `-- name: GetArticlesBySourceName :many select articles.ID, articles.SourceId, articles.Tags, articles.Title, articles.Url, articles.PubDate, articles.Video, articles.VideoHeight, articles.VideoWidth, articles.Thumbnail, articles.Description, articles.AuthorName, articles.AuthorImage, sources.source, sources.name @@ -647,49 +605,6 @@ func (q *Queries) GetIconBySite(ctx context.Context, site string) (Icon, error) return i, err } -const getNewArticlesBySourceId = `-- name: GetNewArticlesBySourceId :many -SELECT id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage FROM articles -Where sourceid = $1 -ORDER BY pubdate desc Limit 50 -` - -func (q *Queries) GetNewArticlesBySourceId(ctx context.Context, sourceid uuid.UUID) ([]Article, error) { - rows, err := q.db.QueryContext(ctx, getNewArticlesBySourceId, sourceid) - 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 getSettingByID = `-- name: GetSettingByID :one Select id, key, value, options From settings Where ID = $1 Limit 1 @@ -867,11 +782,19 @@ func (q *Queries) GetSubscriptionsBySourceID(ctx context.Context, sourceid uuid. } const listArticles = `-- name: ListArticles :many -Select id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage From articles Limit $1 +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 ` -func (q *Queries) ListArticles(ctx context.Context, limit int32) ([]Article, error) { - rows, err := q.db.QueryContext(ctx, listArticles, limit) +type ListArticlesParams struct { + Limit int32 + Offset int32 +} + +func (q *Queries) ListArticles(ctx context.Context, arg ListArticlesParams) ([]Article, error) { + rows, err := q.db.QueryContext(ctx, listArticles, arg.Limit, arg.Offset) if err != nil { return nil, err } @@ -908,7 +831,9 @@ func (q *Queries) ListArticles(ctx context.Context, limit int32) ([]Article, err } const listArticlesByDate = `-- name: ListArticlesByDate :many -Select id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage From articles ORDER BY pubdate desc Limit $1 +Select id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage From articles +ORDER BY pubdate desc +Limit $1 ` func (q *Queries) ListArticlesByDate(ctx context.Context, limit int32) ([]Article, error) { @@ -997,6 +922,49 @@ func (q *Queries) ListArticlesByPage(ctx context.Context, arg ListArticlesByPage return items, nil } +const listArticlesBySourceId = `-- name: ListArticlesBySourceId :many +Select id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage From articles +Where sourceid = $1 +Limit 50 +` + +func (q *Queries) ListArticlesBySourceId(ctx context.Context, sourceid uuid.UUID) ([]Article, error) { + rows, err := q.db.QueryContext(ctx, listArticlesBySourceId, sourceid) + 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 ` @@ -1091,6 +1059,57 @@ func (q *Queries) ListDiscordWebhooks(ctx context.Context, limit int32) ([]Disco return items, nil } +const listNewArticlesBySourceId = `-- name: ListNewArticlesBySourceId :many +SELECT id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage FROM articles +Where sourceid = $1 +ORDER BY pubdate desc +offset $3 +fetch next $2 rows only +` + +type ListNewArticlesBySourceIdParams struct { + Sourceid uuid.UUID + Limit int32 + Offset int32 +} + +func (q *Queries) ListNewArticlesBySourceId(ctx context.Context, arg ListNewArticlesBySourceIdParams) ([]Article, error) { + rows, err := q.db.QueryContext(ctx, listNewArticlesBySourceId, 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 fea4e64..bdb7ca8 100644 --- a/database/schema/query.sql +++ b/database/schema/query.sql @@ -8,24 +8,32 @@ Select * from Articles Where Url = $1 LIMIT 1; -- name: ListArticles :many -Select * From articles Limit $1; +Select * From articles +Order By PubDate DESC +offset $2 +fetch next $1 rows only; -- name: ListArticlesByDate :many -Select * From articles ORDER BY pubdate desc Limit $1; +Select * From articles +ORDER BY pubdate desc +Limit $1; -- name: GetArticlesBySource :many select * from articles INNER join sources on articles.sourceid=Sources.ID where site = $1; --- name: GetNewArticlesBySourceId :many +-- name: ListNewArticlesBySourceId :many SELECT * FROM articles Where sourceid = $1 -ORDER BY pubdate desc Limit 50; +ORDER BY pubdate desc +offset $3 +fetch next $2 rows only; --- name: GetArticlesBySourceId :many +-- name: ListArticlesBySourceId :many Select * From articles -Where sourceid = $1 Limit 50; +Where sourceid = $1 +Limit 50; -- name: GetArticlesBySourceName :many select diff --git a/docs/docs.go b/docs/docs.go index 4e0cd8a..169c5cd 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -24,7 +24,7 @@ const docTemplate = `{ "tags": [ "Articles" ], - "summary": "Lists the top 50 records", + "summary": "Lists the top 25 records ordering from newest to oldest.", "parameters": [ { "type": "string", diff --git a/docs/swagger.json b/docs/swagger.json index f18beb2..b0d109d 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -15,7 +15,7 @@ "tags": [ "Articles" ], - "summary": "Lists the top 50 records", + "summary": "Lists the top 25 records ordering from newest to oldest.", "parameters": [ { "type": "string", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 1970414..65fbaeb 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -253,7 +253,7 @@ paths: description: OK schema: $ref: '#/definitions/routes.ArticlesListResults' - summary: Lists the top 50 records + summary: Lists the top 25 records ordering from newest to oldest. tags: - Articles /articles/{ID}: diff --git a/dto/articles.go b/dto/articles.go index fd8cc20..c526050 100644 --- a/dto/articles.go +++ b/dto/articles.go @@ -21,10 +21,13 @@ func NewDtoClient(db *database.Queries) *DtoClient { } } -func (c *DtoClient) ListArticles(ctx context.Context, limit int) ([]models.ArticleDto, error) { +func (c *DtoClient) ListArticles(ctx context.Context, limit, page int) ([]models.ArticleDto, error) { var res []models.ArticleDto - a, err := c.db.ListArticles(ctx, int32(limit)) + a, err := c.db.ListArticles(ctx, database.ListArticlesParams{ + Limit: int32(limit), + Offset: int32(limit * page), + }) if err != nil { return res, err } @@ -80,7 +83,7 @@ func (c *DtoClient) GetArticleDetails(ctx context.Context, ID uuid.UUID) (models func (c *DtoClient) GetArticlesBySourceId(ctx context.Context, SourceID uuid.UUID) ([]models.ArticleDto, error) { var res []models.ArticleDto - a, err := c.db.GetArticlesBySourceId(ctx, SourceID) + a, err := c.db.ListArticlesBySourceId(ctx, SourceID) if err != nil { return res, err } diff --git a/routes/articles.go b/routes/articles.go index 0282715..77fad7e 100644 --- a/routes/articles.go +++ b/routes/articles.go @@ -38,7 +38,7 @@ type ArticleDetailsResult struct { } // ListArticles -// @Summary Lists the top 50 records +// @Summary Lists the top 25 records ordering from newest to oldest. // @Produce application/json // @Param page query string false "page number" // @Tags Articles @@ -56,14 +56,14 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) { queryPage := query["page"] // if a page number was sent, process it - if len(queryPage) == 1 { + if len(queryPage) >= 1 { page, err := strconv.Atoi(query["page"][0]) if err != nil { s.WriteError(w, err.Error(), http.StatusBadRequest) return } - res, err := s.dto.ListArticlesByPage(r.Context(), int32(page), 50) + res, err := s.dto.ListArticles(r.Context(), 25, page) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return @@ -71,7 +71,7 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) { p.Payload = res s.WriteJson(w, p) } else { - res, err := s.dto.ListArticles(r.Context(), 50) + res, err := s.dto.ListArticles(r.Context(), 25, 0) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return