bySourceId now supports paging and defaults to newest items (#41)

This commit is contained in:
James Tombleson 2023-02-04 21:43:07 -08:00 committed by GitHub
parent 118b7eb5e2
commit b0790359d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 118 additions and 18 deletions

View File

@ -1110,6 +1110,57 @@ func (q *Queries) ListNewArticlesBySourceId(ctx context.Context, arg ListNewArti
return items, nil return items, nil
} }
const listOldestArticlesBySourceId = `-- name: ListOldestArticlesBySourceId :many
SELECT id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage FROM articles
Where sourceid = $1
ORDER BY pubdate asc
offset $3
fetch next $2 rows only
`
type ListOldestArticlesBySourceIdParams struct {
Sourceid uuid.UUID
Limit int32
Offset int32
}
func (q *Queries) ListOldestArticlesBySourceId(ctx context.Context, arg ListOldestArticlesBySourceIdParams) ([]Article, error) {
rows, err := q.db.QueryContext(ctx, listOldestArticlesBySourceId, 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

@ -30,6 +30,14 @@ ORDER BY pubdate desc
offset $3 offset $3
fetch next $2 rows only; fetch next $2 rows only;
-- name: ListOldestArticlesBySourceId :many
SELECT * FROM articles
Where sourceid = $1
ORDER BY pubdate asc
offset $3
fetch next $2 rows only;
-- name: ListArticlesBySourceId :many -- name: ListArticlesBySourceId :many
Select * From articles Select * From articles
Where sourceid = $1 Where sourceid = $1

View File

@ -51,7 +51,7 @@ const docTemplate = `{
"tags": [ "tags": [
"Articles" "Articles"
], ],
"summary": "Finds the articles based on the SourceID provided. Returns the top 50.", "summary": "Finds the articles based on the SourceID provided. Returns the top 25.",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -59,6 +59,12 @@ const docTemplate = `{
"name": "id", "name": "id",
"in": "query", "in": "query",
"required": true "required": true
},
{
"type": "integer",
"description": "Page to query",
"name": "page",
"in": "query"
} }
], ],
"responses": { "responses": {

View File

@ -42,7 +42,7 @@
"tags": [ "tags": [
"Articles" "Articles"
], ],
"summary": "Finds the articles based on the SourceID provided. Returns the top 50.", "summary": "Finds the articles based on the SourceID provided. Returns the top 25.",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -50,6 +50,12 @@
"name": "id", "name": "id",
"in": "query", "in": "query",
"required": true "required": true
},
{
"type": "integer",
"description": "Page to query",
"name": "page",
"in": "query"
} }
], ],
"responses": { "responses": {

View File

@ -300,6 +300,10 @@ paths:
name: id name: id
required: true required: true
type: string type: string
- description: Page to query
in: query
name: page
type: integer
produces: produces:
- application/json - application/json
responses: responses:
@ -308,7 +312,7 @@ paths:
schema: schema:
$ref: '#/definitions/routes.ArticlesListResults' $ref: '#/definitions/routes.ArticlesListResults'
summary: Finds the articles based on the SourceID provided. Returns the top summary: Finds the articles based on the SourceID provided. Returns the top
50. 25.
tags: tags:
- Articles - Articles
/discord/webhooks: /discord/webhooks:

View File

@ -81,9 +81,13 @@ func (c *DtoClient) GetArticleDetails(ctx context.Context, ID uuid.UUID) (models
return res, nil return res, nil
} }
func (c *DtoClient) GetArticlesBySourceId(ctx context.Context, SourceID uuid.UUID) ([]models.ArticleDto, error) { func (c *DtoClient) ListNewArticlesBySourceId(ctx context.Context, SourceID uuid.UUID, limit, page int) ([]models.ArticleDto, error) {
var res []models.ArticleDto var res []models.ArticleDto
a, err := c.db.ListArticlesBySourceId(ctx, SourceID) a, err := c.db.ListNewArticlesBySourceId(ctx, database.ListNewArticlesBySourceIdParams{
Sourceid: SourceID,
Limit: int32(limit),
Offset: int32(limit * page),
})
if err != nil { if err != nil {
return res, err return res, err
} }

View File

@ -17,7 +17,7 @@ func (s *Server) GetArticleRouter() http.Handler {
r.Get("/", s.getArticle) r.Get("/", s.getArticle)
r.Get("/details", s.getArticleDetails) r.Get("/details", s.getArticleDetails)
}) })
r.Get("/by/sourceid", s.GetArticlesBySourceId) r.Get("/by/sourceid", s.ListArticlesBySourceId)
return r return r
} }
@ -148,14 +148,15 @@ func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
} }
// TODO add page support // TODO add page support
// GetArticlesBySourceID // ListArticlesBySourceID
// @Summary Finds the articles based on the SourceID provided. Returns the top 50. // @Summary Finds the articles based on the SourceID provided. Returns the top 25.
// @Param id query string true "Source ID UUID" // @Param id query string true "Source ID UUID"
// @Param page query int false "Page to query"
// @Produce application/json // @Produce application/json
// @Tags Articles // @Tags Articles
// @Router /articles/by/sourceid [get] // @Router /articles/by/sourceid [get]
// @Success 200 {object} ArticlesListResults "OK" // @Success 200 {object} ArticlesListResults "OK"
func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) { func (s *Server) ListArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
p := ArticlesListResults{ p := ArticlesListResults{
ApiStatusModel: ApiStatusModel{ ApiStatusModel: ApiStatusModel{
Message: "OK", Message: "OK",
@ -167,18 +168,38 @@ func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
_id := query["id"][0] _id := query["id"][0]
uuid, err := uuid.Parse(_id) uuid, err := uuid.Parse(_id)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
}
// if a page number was sent, process it
if len(query["page"]) >= 1 {
_page, err := strconv.Atoi(query["page"][0])
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
res, err := s.dto.ListNewArticlesBySourceId(r.Context(), uuid, 25, _page)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
} else {
res, err := s.dto.ListNewArticlesBySourceId(r.Context(), uuid, 25, 0)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
} }
res, err := s.dto.GetArticlesBySourceId(r.Context(), uuid)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
} }