added a func to query articles by page

This commit is contained in:
James Tombleson 2023-01-30 22:34:46 -08:00
parent 6629a55157
commit 6e8df73bd2
6 changed files with 147 additions and 0 deletions

View File

@ -948,6 +948,55 @@ func (q *Queries) ListArticlesByDate(ctx context.Context, limit int32) ([]Articl
return items, nil 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 const listDiscordQueueItems = `-- name: ListDiscordQueueItems :many
Select id, articleid from DiscordQueue LIMIT $1 Select id, articleid from DiscordQueue LIMIT $1
` `

View File

@ -35,6 +35,12 @@ Left Join sources
On articles.sourceid = sources.id On articles.sourceid = sources.id
Where name = $1; Where name = $1;
-- name: ListArticlesByPage :many
select * from articles
order by pubdate desc
offset $2
fetch next $1 rows only;
-- name: CreateArticle :exec -- name: CreateArticle :exec
INSERT INTO Articles INSERT INTO Articles
(ID, SourceId, Tags, Title, Url, PubDate, Video, VideoHeight, VideoWidth, Thumbnail, Description, AuthorName, AuthorImage) (ID, SourceId, Tags, Title, Url, PubDate, Video, VideoHeight, VideoWidth, Thumbnail, Description, AuthorName, AuthorImage)

View File

@ -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": { "/articles/by/sourceid": {
"get": { "get": {
"produces": [ "produces": [

View File

@ -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": { "/articles/by/sourceid": {
"get": { "get": {
"produces": [ "produces": [

View File

@ -287,6 +287,24 @@ paths:
summary: Returns an article and source based on defined ID. summary: Returns an article and source based on defined ID.
tags: tags:
- Articles - 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: /articles/by/sourceid:
get: get:
parameters: parameters:

View File

@ -35,6 +35,24 @@ func (c DtoClient) ListArticles(ctx context.Context, limit int) ([]models.Articl
return res, nil 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) { func (c DtoClient) GetArticle(ctx context.Context, ID uuid.UUID) (models.ArticleDto, error) {
a, err := c.db.GetArticleByID(ctx, ID) a, err := c.db.GetArticleByID(ctx, ID)
if err != nil { if err != nil {