@@ -19,7 +19,6 @@ import (
1919 "errors"
2020 "html"
2121 "log"
22- "reflect"
2322 "strings"
2423 "time"
2524
@@ -51,17 +50,24 @@ func (post *Post) Prepare() {
5150 post .CreatedAt = time .Now ()
5251}
5352
53+ type PostsResponse struct {
54+ Posts []Post `json:"posts"`
55+ NextOffset * int64 `json:"next_offset"`
56+ PrevOffset * int64 `json:"previous_offset"`
57+ Total int `json:"total"`
58+ }
59+
5460//Validate data of post
5561func (post * Post ) Validate () error {
5662
5763 if post .Title == "" {
58- return errors .New ("Required Title " )
64+ return errors .New ("required title " )
5965 }
6066 if post .Content == "" {
61- return errors .New ("Required Content " )
67+ return errors .New ("tequired content " )
6268 }
6369 if post .AuthorID < 1 {
64- return errors .New ("Required Author " )
70+ return errors .New ("required author " )
6571 }
6672 return nil
6773}
@@ -97,42 +103,57 @@ func GetPostByID(client *mongo.Client, ID string) (Post, error) {
97103 collection := client .Database ("crapi" ).Collection ("post" )
98104 filter := bson.D {{Key : "id" , Value : ID }}
99105 err := collection .FindOne (context .TODO (), filter ).Decode (& post )
106+ if err != nil {
107+ log .Println (err )
108+ }
100109
101110 return post , err
102111
103112}
104113
105114//FindAllPost return all recent post
106- func FindAllPost (client * mongo.Client , offset int , limit int ) ([] interface {} , error ) {
107- post := []Post {}
108-
115+ func FindAllPost (client * mongo.Client , offset int64 , limit int64 ) (PostsResponse , error ) {
116+ postList := []Post {}
117+ var postsResponse PostsResponse = PostsResponse {}
109118 options := options .Find ()
110119 options .SetSort (bson.D {{Key : "_id" , Value : - 1 }})
111- options .SetLimit (int64 (limit ))
112- options .SetSkip (int64 (offset * limit ))
120+ options .SetLimit (limit )
121+ options .SetSkip (offset )
122+ ctx := context .Background ()
113123 collection := client .Database ("crapi" ).Collection ("post" )
114- cur , err := collection .Find (context . Background () , bson.D {}, options )
124+ cur , err := collection .Find (ctx , bson.D {}, options )
115125 if err != nil {
116- log .Println (err )
126+ log .Println ("Error in finding posts: " , err )
127+ return postsResponse , err
117128 }
118- log .Println (cur )
119- objectType := reflect .TypeOf (post ).Elem ()
120- var list = make ([]interface {}, 0 )
121- defer cur .Close (context .Background ())
122- for cur .Next (context .Background ()) {
123- result := reflect .New (objectType ).Interface ()
124- err := cur .Decode (result )
125-
129+ for cur .Next (ctx ) {
130+ var elem Post
131+ err := cur .Decode (& elem )
126132 if err != nil {
127- log .Println (err )
128- return nil , err
133+ log .Println ("Error in decoding posts: " , err )
134+ return postsResponse , err
129135 }
136+ postList = append (postList , elem )
137+ }
130138
131- list = append (list , result )
139+ postsResponse .Posts = postList
140+ // get posts count for pagination
141+ count , err1 := collection .CountDocuments (context .Background (), bson.D {})
142+ if err1 != nil {
143+ log .Println ("Error in counting posts: " , err1 )
144+ return postsResponse , err1
132145 }
133- if err := cur .Err (); err != nil {
134- return nil , err
146+ if offset - limit >= 0 {
147+ tempOffset := offset - limit
148+ postsResponse .PrevOffset = & tempOffset
135149 }
136-
137- return list , err
150+ if offset + limit < count {
151+ tempOffset := offset + limit
152+ postsResponse .NextOffset = & tempOffset
153+ }
154+ postsResponse .Total = len (postList )
155+ if err = cur .Err (); err != nil {
156+ log .Println ("Error in cursor: " , err )
157+ }
158+ return postsResponse , err
138159}
0 commit comments