updated ArticlesList to always list the newest by default

This commit is contained in:
James Tombleson 2023-02-04 20:10:55 -08:00
parent 0ae1de0d33
commit d3eeebceb3
7 changed files with 135 additions and 105 deletions

View File

@ -429,48 +429,6 @@ func (q *Queries) GetArticlesBySource(ctx context.Context, site string) ([]GetAr
return items, nil 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 const getArticlesBySourceName = `-- name: GetArticlesBySourceName :many
select 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 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 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 const getSettingByID = `-- name: GetSettingByID :one
Select id, key, value, options From settings Select id, key, value, options From settings
Where ID = $1 Limit 1 Where ID = $1 Limit 1
@ -867,11 +782,19 @@ func (q *Queries) GetSubscriptionsBySourceID(ctx context.Context, sourceid uuid.
} }
const listArticles = `-- name: ListArticles :many 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) { type ListArticlesParams struct {
rows, err := q.db.QueryContext(ctx, listArticles, limit) 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 { if err != nil {
return nil, err return nil, err
} }
@ -908,7 +831,9 @@ func (q *Queries) ListArticles(ctx context.Context, limit int32) ([]Article, err
} }
const listArticlesByDate = `-- name: ListArticlesByDate :many 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) { 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 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 const listDiscordQueueItems = `-- name: ListDiscordQueueItems :many
Select id, articleid from DiscordQueue LIMIT $1 Select id, articleid from DiscordQueue LIMIT $1
` `
@ -1091,6 +1059,57 @@ func (q *Queries) ListDiscordWebhooks(ctx context.Context, limit int32) ([]Disco
return items, nil 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 const listSources = `-- name: ListSources :many
Select id, site, name, source, type, value, enabled, url, tags, deleted From Sources Limit $1 Select id, site, name, source, type, value, enabled, url, tags, deleted From Sources Limit $1
` `

View File

@ -8,24 +8,32 @@ Select * from Articles
Where Url = $1 LIMIT 1; Where Url = $1 LIMIT 1;
-- name: ListArticles :many -- 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 -- name: ListArticlesByDate :many
Select * From articles ORDER BY pubdate desc Limit $1; Select * From articles
ORDER BY pubdate desc
Limit $1;
-- name: GetArticlesBySource :many -- name: GetArticlesBySource :many
select * from articles select * from articles
INNER join sources on articles.sourceid=Sources.ID INNER join sources on articles.sourceid=Sources.ID
where site = $1; where site = $1;
-- name: GetNewArticlesBySourceId :many -- name: ListNewArticlesBySourceId :many
SELECT * FROM articles SELECT * FROM articles
Where sourceid = $1 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 Select * From articles
Where sourceid = $1 Limit 50; Where sourceid = $1
Limit 50;
-- name: GetArticlesBySourceName :many -- name: GetArticlesBySourceName :many
select select

View File

@ -24,7 +24,7 @@ const docTemplate = `{
"tags": [ "tags": [
"Articles" "Articles"
], ],
"summary": "Lists the top 50 records", "summary": "Lists the top 25 records ordering from newest to oldest.",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",

View File

@ -15,7 +15,7 @@
"tags": [ "tags": [
"Articles" "Articles"
], ],
"summary": "Lists the top 50 records", "summary": "Lists the top 25 records ordering from newest to oldest.",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",

View File

@ -253,7 +253,7 @@ paths:
description: OK description: OK
schema: schema:
$ref: '#/definitions/routes.ArticlesListResults' $ref: '#/definitions/routes.ArticlesListResults'
summary: Lists the top 50 records summary: Lists the top 25 records ordering from newest to oldest.
tags: tags:
- Articles - Articles
/articles/{ID}: /articles/{ID}:

View File

@ -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 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 { if err != nil {
return res, err 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) { func (c *DtoClient) GetArticlesBySourceId(ctx context.Context, SourceID uuid.UUID) ([]models.ArticleDto, error) {
var res []models.ArticleDto var res []models.ArticleDto
a, err := c.db.GetArticlesBySourceId(ctx, SourceID) a, err := c.db.ListArticlesBySourceId(ctx, SourceID)
if err != nil { if err != nil {
return res, err return res, err
} }

View File

@ -38,7 +38,7 @@ type ArticleDetailsResult struct {
} }
// ListArticles // ListArticles
// @Summary Lists the top 50 records // @Summary Lists the top 25 records ordering from newest to oldest.
// @Produce application/json // @Produce application/json
// @Param page query string false "page number" // @Param page query string false "page number"
// @Tags Articles // @Tags Articles
@ -56,14 +56,14 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
queryPage := query["page"] queryPage := query["page"]
// if a page number was sent, process it // if a page number was sent, process it
if len(queryPage) == 1 { if len(queryPage) >= 1 {
page, err := strconv.Atoi(query["page"][0]) page, err := strconv.Atoi(query["page"][0])
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
res, err := s.dto.ListArticlesByPage(r.Context(), int32(page), 50) res, err := s.dto.ListArticles(r.Context(), 25, page)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
@ -71,7 +71,7 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
p.Payload = res p.Payload = res
s.WriteJson(w, p) s.WriteJson(w, p)
} else { } else {
res, err := s.dto.ListArticles(r.Context(), 50) res, err := s.dto.ListArticles(r.Context(), 25, 0)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return