merged page with listArticles as a optional
This commit is contained in:
parent
8f34024832
commit
b1af289884
22
docs/docs.go
22
docs/docs.go
@ -25,32 +25,12 @@ const docTemplate = `{
|
||||
"Articles"
|
||||
],
|
||||
"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": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "page number",
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"required": true
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
@ -16,32 +16,12 @@
|
||||
"Articles"
|
||||
],
|
||||
"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": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "page number",
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"required": true
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
@ -241,6 +241,11 @@ info:
|
||||
paths:
|
||||
/articles:
|
||||
get:
|
||||
parameters:
|
||||
- description: page number
|
||||
in: query
|
||||
name: page
|
||||
type: string
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
@ -287,24 +292,6 @@ paths:
|
||||
summary: Returns an article and source based on defined ID.
|
||||
tags:
|
||||
- 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:
|
||||
get:
|
||||
parameters:
|
||||
|
@ -1,6 +1,7 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@ -18,7 +19,6 @@ func (s *Server) GetArticleRouter() http.Handler {
|
||||
r.Get("/details", s.getArticleDetails)
|
||||
})
|
||||
r.Get("/by/sourceid", s.GetArticlesBySourceId)
|
||||
r.Get("/by/page", s.ListArticlesByPage)
|
||||
|
||||
return r
|
||||
}
|
||||
@ -41,6 +41,7 @@ type ArticleDetailsResult struct {
|
||||
// ListArticles
|
||||
// @Summary Lists the top 50 records
|
||||
// @Produce application/json
|
||||
// @Param page query string false "page number"
|
||||
// @Tags Articles
|
||||
// @Router /articles [get]
|
||||
// @Success 200 {object} ArticlesListResults "OK"
|
||||
@ -52,6 +53,26 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
|
||||
},
|
||||
}
|
||||
|
||||
query := r.URL.Query()
|
||||
queryPage := query["page"]
|
||||
fmt.Printf("queryPage: %v\n", queryPage)
|
||||
|
||||
// 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)
|
||||
@ -60,38 +81,6 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
|
||||
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()
|
||||
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), 25)
|
||||
if err != nil {
|
||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
p.Payload = res
|
||||
|
||||
s.WriteJson(w, p)
|
||||
}
|
||||
|
||||
// GetArticle
|
||||
|
@ -100,14 +100,14 @@ type ApiError struct {
|
||||
func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) {
|
||||
e := ApiError{
|
||||
ApiStatusModel: &ApiStatusModel{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
StatusCode: HttpStatusCode,
|
||||
Message: errMessage,
|
||||
},
|
||||
}
|
||||
|
||||
b, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), HttpStatusCode)
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
|
Loading…
Reference in New Issue
Block a user