diff --git a/docs/docs.go b/docs/docs.go index e33d8e2..4e0cd8a 100644 --- a/docs/docs.go +++ b/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": { diff --git a/docs/swagger.json b/docs/swagger.json index a1504b1..f18beb2 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index c0e237e..1970414 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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: diff --git a/routes/articles.go b/routes/articles.go index 3cfaf05..1773d2b 100644 --- a/routes/articles.go +++ b/routes/articles.go @@ -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,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() - page, err := strconv.Atoi(query["page"][0]) - if err != nil { - s.WriteError(w, err.Error(), http.StatusBadRequest) - return + 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) + 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 diff --git a/routes/server.go b/routes/server.go index eaedffd..952a457 100644 --- a/routes/server.go +++ b/routes/server.go @@ -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)