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
}
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
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
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
Select * From articles
Where sourceid = $1

View File

@ -51,7 +51,7 @@ const docTemplate = `{
"tags": [
"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": [
{
"type": "string",
@ -59,6 +59,12 @@ const docTemplate = `{
"name": "id",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Page to query",
"name": "page",
"in": "query"
}
],
"responses": {

View File

@ -42,7 +42,7 @@
"tags": [
"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": [
{
"type": "string",
@ -50,6 +50,12 @@
"name": "id",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Page to query",
"name": "page",
"in": "query"
}
],
"responses": {

View File

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

View File

@ -81,9 +81,13 @@ func (c *DtoClient) GetArticleDetails(ctx context.Context, ID uuid.UUID) (models
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
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 {
return res, err
}

View File

@ -17,7 +17,7 @@ func (s *Server) GetArticleRouter() http.Handler {
r.Get("/", s.getArticle)
r.Get("/details", s.getArticleDetails)
})
r.Get("/by/sourceid", s.GetArticlesBySourceId)
r.Get("/by/sourceid", s.ListArticlesBySourceId)
return r
}
@ -148,14 +148,15 @@ func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
}
// TODO add page support
// GetArticlesBySourceID
// @Summary Finds the articles based on the SourceID provided. Returns the top 50.
// ListArticlesBySourceID
// @Summary Finds the articles based on the SourceID provided. Returns the top 25.
// @Param id query string true "Source ID UUID"
// @Param page query int false "Page to query"
// @Produce application/json
// @Tags Articles
// @Router /articles/by/sourceid [get]
// @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{
ApiStatusModel: ApiStatusModel{
Message: "OK",
@ -167,18 +168,38 @@ func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
_id := query["id"][0]
uuid, err := uuid.Parse(_id)
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
res, err := s.dto.GetArticlesBySourceId(r.Context(), uuid)
// 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
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)
}
}