Skip to content

Commit 96c16d7

Browse files
committed
Merge branch 'develop'
2 parents d1bc425 + 6bfa7eb commit 96c16d7

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

services/community/api/controllers/post_controller.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import (
1818
"encoding/json"
1919
"io/ioutil"
2020
"net/http"
21+
"strconv"
2122

22-
"github.com/gorilla/mux"
2323
"crapi.proj/goservice/api/models"
2424
"crapi.proj/goservice/api/responses"
25+
"github.com/gorilla/mux"
2526
)
2627

2728
//AddNewPost add post in database,
@@ -72,8 +73,30 @@ func (s *Server) GetPostByID(w http.ResponseWriter, r *http.Request) {
7273
//GetPost Vulnerabilities
7374
func (s *Server) GetPost(w http.ResponseWriter, r *http.Request) {
7475
//post := models.Post{}
76+
limit_param := r.URL.Query().Get("limit")
77+
limit := 30
78+
err := error(nil)
79+
if limit_param != "" {
80+
// Parse limit_param and set to limit
81+
limit, err = strconv.Atoi(limit_param)
82+
if err != nil {
83+
limit = 30
84+
}
85+
}
86+
if limit > 50 {
87+
limit = 50
88+
}
89+
90+
page_param := r.URL.Query().Get("page")
91+
page := 0
92+
if page_param != "" {
93+
page, err = strconv.Atoi(page_param)
94+
if err != nil {
95+
page = 0
96+
}
97+
}
98+
posts, err := models.FindAllPost(s.Client, page, limit)
7599

76-
posts, err := models.FindAllPost(s.Client)
77100
if err != nil {
78101
responses.ERROR(w, http.StatusInternalServerError, err)
79102
return

services/community/api/models/post.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ func GetPostByID(client *mongo.Client, ID string) (Post, error) {
104104
}
105105

106106
//FindAllPost return all recent post
107-
func FindAllPost(client *mongo.Client) ([]interface{}, error) {
107+
func FindAllPost(client *mongo.Client, page int, limit int) ([]interface{}, error) {
108108
post := []Post{}
109109

110110
options := options.Find()
111111
options.SetSort(bson.D{{"_id", -1}})
112-
options.SetLimit(10)
112+
options.SetLimit(int64(limit))
113+
options.SetSkip(int64(page * limit))
113114
collection := client.Database("crapi").Collection("post")
114115
cur, err := collection.Find(context.Background(), bson.D{}, options)
115116
if err != nil {

0 commit comments

Comments
 (0)