@@ -28,18 +28,18 @@ import (
2828 "go.mongodb.org/mongo-driver/mongo/options"
2929)
3030
31- //Post Field
31+ // Post Field
3232type Post struct {
3333 ID string `gorm:"primary_key;auto_increment" json:"id"`
3434 Title string `gorm:"size:255;not null;unique" json:"title"`
3535 Content string `gorm:"size:255;not null;" json:"content"`
36- Author Author `json:"author"`
36+ Author Author `json:"author"`
3737 Comments []Comments `json:"comments"`
3838 AuthorID uint64 `sql:"type:int REFERENCES users(id)" json:"authorid"`
3939 CreatedAt time.Time
4040}
4141
42- //Prepare initialize data
42+ // Prepare initialize data
4343func (post * Post ) Prepare () {
4444 post .ID = shortuuid .New ()
4545 post .Title = html .EscapeString (strings .TrimSpace (post .Title ))
@@ -51,13 +51,13 @@ func (post *Post) Prepare() {
5151}
5252
5353type PostsResponse struct {
54- Posts []Post `json:"posts"`
54+ Posts []Post `json:"posts"`
5555 NextOffset * int64 `json:"next_offset"`
5656 PrevOffset * int64 `json:"previous_offset"`
57- Total int `json:"total"`
57+ Total int `json:"total"`
5858}
5959
60- //Validate data of post
60+ // Validate data of post
6161func (post * Post ) Validate () error {
6262
6363 if post .Title == "" {
@@ -72,7 +72,7 @@ func (post *Post) Validate() error {
7272 return nil
7373}
7474
75- //Prepare initialize Field
75+ // Prepare initialize Field
7676func Prepare () Author {
7777 var u Author
7878 u .Nickname = nickname
@@ -83,7 +83,7 @@ func Prepare() Author {
8383 return u
8484}
8585
86- //SavePost persits data into database
86+ // SavePost persits data into database
8787func SavePost (client * mongo.Client , post Post ) (Post , error ) {
8888
8989 collection := client .Database ("crapi" ).Collection ("post" )
@@ -95,7 +95,7 @@ func SavePost(client *mongo.Client, post Post) (Post, error) {
9595 return post , err
9696}
9797
98- //GetPostByID fetch post by postId
98+ // GetPostByID fetch post by postId
9999func GetPostByID (client * mongo.Client , ID string ) (Post , error ) {
100100 var post Post
101101
@@ -111,10 +111,10 @@ func GetPostByID(client *mongo.Client, ID string) (Post, error) {
111111
112112}
113113
114- //FindAllPost return all recent post
114+ // FindAllPost return all recent post
115115func FindAllPost (client * mongo.Client , offset int64 , limit int64 ) (PostsResponse , error ) {
116116 postList := []Post {}
117- var postsResponse PostsResponse = PostsResponse {}
117+ postsResponse : = PostsResponse {}
118118 options := options .Find ()
119119 options .SetSort (bson.D {{Key : "_id" , Value : - 1 }})
120120 options .SetLimit (limit )
@@ -143,11 +143,11 @@ func FindAllPost(client *mongo.Client, offset int64, limit int64) (PostsResponse
143143 log .Println ("Error in counting posts: " , err1 )
144144 return postsResponse , err1
145145 }
146- if offset - limit >= 0 {
146+ if offset - limit >= 0 {
147147 tempOffset := offset - limit
148148 postsResponse .PrevOffset = & tempOffset
149149 }
150- if offset + limit < count {
150+ if offset + limit < count {
151151 tempOffset := offset + limit
152152 postsResponse .NextOffset = & tempOffset
153153 }
0 commit comments