merged page with listArticles as a optional

This commit is contained in:
James Tombleson 2023-01-31 08:17:27 -08:00
parent 8f34024832
commit b1af289884
5 changed files with 37 additions and 101 deletions

View File

@ -25,32 +25,12 @@ const docTemplate = `{
"Articles" "Articles"
], ],
"summary": "Lists the top 50 records", "summary": "Lists the top 50 records",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/routes.ArticlesListResults"
}
}
}
}
},
"/articles/by/page": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Articles"
],
"summary": "List 50 items based on the requested page",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
"description": "page number", "description": "page number",
"name": "page", "name": "page",
"in": "query", "in": "query"
"required": true
} }
], ],
"responses": { "responses": {

View File

@ -16,32 +16,12 @@
"Articles" "Articles"
], ],
"summary": "Lists the top 50 records", "summary": "Lists the top 50 records",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/routes.ArticlesListResults"
}
}
}
}
},
"/articles/by/page": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Articles"
],
"summary": "List 50 items based on the requested page",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
"description": "page number", "description": "page number",
"name": "page", "name": "page",
"in": "query", "in": "query"
"required": true
} }
], ],
"responses": { "responses": {

View File

@ -241,6 +241,11 @@ info:
paths: paths:
/articles: /articles:
get: get:
parameters:
- description: page number
in: query
name: page
type: string
produces: produces:
- application/json - application/json
responses: responses:
@ -287,24 +292,6 @@ 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

@ -1,6 +1,7 @@
package routes package routes
import ( import (
"fmt"
"net/http" "net/http"
"strconv" "strconv"
@ -18,7 +19,6 @@ func (s *Server) GetArticleRouter() http.Handler {
r.Get("/details", s.getArticleDetails) r.Get("/details", s.getArticleDetails)
}) })
r.Get("/by/sourceid", s.GetArticlesBySourceId) r.Get("/by/sourceid", s.GetArticlesBySourceId)
r.Get("/by/page", s.ListArticlesByPage)
return r return r
} }
@ -41,6 +41,7 @@ type ArticleDetailsResult struct {
// ListArticles // ListArticles
// @Summary Lists the top 50 records // @Summary Lists the top 50 records
// @Produce application/json // @Produce application/json
// @Param page query string false "page number"
// @Tags Articles // @Tags Articles
// @Router /articles [get] // @Router /articles [get]
// @Success 200 {object} ArticlesListResults "OK" // @Success 200 {object} ArticlesListResults "OK"
@ -52,46 +53,34 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
}, },
} }
res, err := s.dto.ListArticles(r.Context(), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
}
// ListArticlesByPage
// @Summary List 50 items based on the requested page
// @Produce application/json
// @Param page query string true "page number"
// @Tags Articles
// @Router /articles/by/page [get]
// @Success 200 {object} ArticlesListResults "OK"
func (s *Server) ListArticlesByPage(w http.ResponseWriter, r *http.Request) {
p := ArticlesListResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
query := r.URL.Query() query := r.URL.Query()
page, err := strconv.Atoi(query["page"][0]) queryPage := query["page"]
if err != nil { fmt.Printf("queryPage: %v\n", queryPage)
s.WriteError(w, err.Error(), http.StatusBadRequest)
return // if a page number was sent, process it
if len(queryPage) == 1 {
page, err := strconv.Atoi(query["page"][0])
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
res, err := s.dto.ListArticlesByPage(r.Context(), int32(page), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
} else {
res, err := s.dto.ListArticles(r.Context(), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
} }
res, err := s.dto.ListArticlesByPage(r.Context(), int32(page), 25)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
} }
// GetArticle // GetArticle

View File

@ -100,14 +100,14 @@ type ApiError struct {
func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) { func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) {
e := ApiError{ e := ApiError{
ApiStatusModel: &ApiStatusModel{ ApiStatusModel: &ApiStatusModel{
StatusCode: http.StatusInternalServerError, StatusCode: HttpStatusCode,
Message: errMessage, Message: errMessage,
}, },
} }
b, err := json.Marshal(e) b, err := json.Marshal(e)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), HttpStatusCode)
} }
w.Write(b) w.Write(b)