From 1a85d169ed17654d373c1d736e5fc19c1e1ad67b Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 6 Nov 2022 19:07:31 -0800 Subject: [PATCH] replaced sql command to order the new articles by source --- database/query.sql.go | 44 ++++++++++++++++++++++++++++++++++++++- database/schema/query.sql | 11 ++++++---- routes/articles.go | 3 ++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/database/query.sql.go b/database/query.sql.go index 6d63884..6e80ac4 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -354,7 +354,6 @@ const getArticlesBySource = `-- name: GetArticlesBySource :many select articles.id, sourceid, articles.tags, title, articles.url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage, sources.id, site, name, source, type, value, enabled, sources.url, sources.tags from articles INNER join sources on articles.sourceid=Sources.ID where site = $1 -ORDER By pubdate desc ` type GetArticlesBySourceRow struct { @@ -607,6 +606,49 @@ 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 diff --git a/database/schema/query.sql b/database/schema/query.sql index 4825167..ca88681 100644 --- a/database/schema/query.sql +++ b/database/schema/query.sql @@ -11,14 +11,17 @@ Where Url = $1 LIMIT 1; Select * From articles Limit $1; -- 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 -ORDER By pubdate desc; +where site = $1; + +-- name: GetNewArticlesBySourceId :many +SELECT * FROM articles +Where sourceid = $1 +ORDER BY pubdate desc Limit 50; -- name: GetArticlesBySourceId :many Select * From articles diff --git a/routes/articles.go b/routes/articles.go index 2841b23..9b3c75c 100644 --- a/routes/articles.go +++ b/routes/articles.go @@ -81,7 +81,8 @@ func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) { panic(err) } - res, err := s.Db.GetArticlesBySourceId(*s.ctx, uuid) + res, err := s.Db.GetNewArticlesBySourceId(*s.ctx, uuid) + //res, err := s.Db.GetArticlesBySourceId(*s.ctx, uuid) if err != nil { w.Write([]byte(err.Error())) panic(err)