@@ -3,36 +3,27 @@ package main
3
3
import (
4
4
"context"
5
5
"log"
6
- "net/http"
7
- "strconv"
8
6
"time"
9
7
10
- "github.com/labstack/echo/v4"
11
8
"go.mongodb.org/mongo-driver/bson"
12
9
"go.mongodb.org/mongo-driver/mongo"
13
10
"go.mongodb.org/mongo-driver/mongo/options"
14
11
)
15
12
16
- func GetPost (c echo. Context ) error {
13
+ func GetPost (collection * mongo. Collection , id int , category string ) * Post {
17
14
var result * Post
18
- id , _ := strconv .Atoi (c .QueryParam ("id" ))
19
- category := c .QueryParam ("category" )
20
15
21
16
// Search for post by id and category
22
17
filter := bson.D {{"postID" , id }, {"category" , category }}
23
18
err := collection .FindOne (context .TODO (), filter ).Decode (& result )
24
19
if err != nil {
25
20
log .Fatal (err )
26
21
}
27
- return c .JSON (http .StatusOK , H {
28
- "post" : result ,
29
- })
30
- }
31
22
32
- func GetAllPosts (c echo.Context ) error {
33
- count , _ := strconv .Atoi (c .QueryParam ("id" ))
34
- cat := c .QueryParam ("category" )
23
+ return result
24
+ }
35
25
26
+ func GetAllPosts (collection * mongo.Collection , count int , cat string ) []* Post {
36
27
findOptions := options .Find ()
37
28
if count != 10 {
38
29
findOptions .SetLimit (int64 (count ))
@@ -66,61 +57,43 @@ func GetAllPosts(c echo.Context) error {
66
57
posts = append (posts , & elem )
67
58
}
68
59
69
- return c .JSON (http .StatusOK , H {
70
- "posts" : posts ,
71
- })
60
+ return posts
72
61
}
73
62
74
- func NewPost (c echo.Context ) error {
75
- id , _ := strconv .Atoi (c .FormValue ("id" ))
76
- category , _ := strconv .Atoi (c .FormValue ("category" ))
77
- showinMenu , _ := strconv .ParseBool (c .FormValue ("showInMenu" ))
78
-
63
+ func NewPost (collection * mongo.Collection , id int , category int , showInMenu bool , title string , subtitle string , postType string , content string , github string , fb string ) {
79
64
post := Post {
80
65
postID : id ,
81
- postTitle : c . FormValue ( " title" ) ,
82
- postSubtitle : c . FormValue ( " subtitle" ) ,
83
- postType : c . FormValue ( "type" ) ,
66
+ postTitle : title ,
67
+ postSubtitle : subtitle ,
68
+ postType : postType ,
84
69
postCategory : category ,
85
70
createdOn : time .Now (),
86
71
lastEditedOn : time .Now (),
87
- postContent : c . FormValue ( " content" ) ,
88
- postLinkGithub : c . FormValue ( "linkGithub" ) ,
89
- postLinkFacebook : c . FormValue ( "linkFacebook" ) ,
90
- showInMenu : showinMenu ,
72
+ postContent : content ,
73
+ postLinkGithub : github ,
74
+ postLinkFacebook : fb ,
75
+ showInMenu : showInMenu ,
91
76
}
92
77
93
78
_ , err := collection .InsertOne (context .TODO (), post )
94
79
if err != nil {
95
80
log .Fatal (err )
96
81
}
97
-
98
- return c .JSON (http .StatusOK , H {})
99
82
}
100
83
101
- func UpdatePost (c echo.Context ) error {
102
- postID , _ := strconv .Atoi (c .FormValue ("id" ))
103
- postTitle := c .FormValue ("title" )
104
- postSubtitle := c .FormValue ("subtitle" )
105
- postType := c .FormValue ("type" )
106
- postCategory := c .FormValue ("category" )
107
- postContent := c .FormValue ("content" )
108
- postLinkGithub := c .FormValue ("linkGithub" )
109
- postLinkFacebook := c .FormValue ("linkFacebook" )
110
- showinMenu , _ := strconv .ParseBool (c .FormValue ("showInMenu" ))
111
-
112
- filter := bson.D {{"postID" , postID }}
84
+ func UpdatePost (collection * mongo.Collection , id int , category int , showInMenu bool , title string , subtitle string , postType string , content string , github string , fb string ) {
85
+ filter := bson.D {{"postID" , id }}
113
86
update := bson.D {
114
87
{"$set" , bson.D {
115
- {"postTitle" , postTitle },
116
- {"postSubtitle" , postSubtitle },
88
+ {"postTitle" , title },
89
+ {"postSubtitle" , subtitle },
117
90
{"postType" , postType },
118
- {"postCategory" , postCategory },
91
+ {"postCategory" , category },
119
92
{"lastEditedOn" , time .Now ()},
120
- {"postContent" , postContent },
121
- {"postLinkGithub" , postLinkGithub },
122
- {"postLinkFacebook" , postLinkFacebook },
123
- {"showinMenu" , showinMenu },
93
+ {"postContent" , content },
94
+ {"postLinkGithub" , github },
95
+ {"postLinkFacebook" , fb },
96
+ {"showinMenu" , showInMenu },
124
97
}},
125
98
}
126
99
@@ -129,19 +102,14 @@ func UpdatePost(c echo.Context) error {
129
102
if err != nil {
130
103
log .Fatal (err )
131
104
}
132
-
133
- return c .JSON (http .StatusOK , H {})
134
105
}
135
106
136
- func DeletePost (c echo.Context ) error {
137
- id , _ := strconv .Atoi (c .FormValue ("id" ))
107
+ func DeletePost (collection * mongo.Collection , id int ) {
138
108
filter := bson.D {{"postID" , id }}
139
109
140
110
// Find a post by id and delete it
141
111
_ , err := collection .DeleteOne (context .TODO (), filter )
142
112
if err != nil {
143
113
log .Fatal (err )
144
114
}
145
-
146
- return c .JSON (http .StatusOK , H {})
147
115
}
0 commit comments