@@ -52,6 +52,7 @@ app.get("/",(req,res)=>{
52
52
} ) ;
53
53
54
54
} ) ;
55
+ // Posts Routes
55
56
// Create Post route
56
57
app . get ( "/f/:id/posts/create" , isLoggedIn , ( req , res ) => {
57
58
res . render ( "posts/new" ) ;
@@ -61,20 +62,107 @@ app.post("/",isLoggedIn,(req, res)=>{
61
62
id : req . user . _id ,
62
63
username : req . user . username
63
64
}
64
- Post . create ( req . body . post , ( err , newPost ) => {
65
+ var post = { title :req . body . post . title , description :req . body . post . description , channel :req . body . post . channel , author :author } ;
66
+ Post . create ( post , ( err , newPost ) => {
65
67
if ( err )
66
68
{
67
69
req . flash ( "error" , err . message ) ;
68
70
return res . redirect ( "back" ) ;
69
71
}
70
72
else
71
73
{
72
- console . log ( newPost ) ;
73
74
res . redirect ( "/" ) ;
74
75
}
75
76
} ) ;
76
77
77
78
} ) ;
79
+ // Show Post Route
80
+ app . get ( "/f/:channel/:id" , isLoggedIn , ( req , res ) => {
81
+ Post . findById ( req . params . id ) . populate ( "comments" ) . exec ( ( err , foundPost ) => {
82
+ if ( err )
83
+ {
84
+ req . flash ( "error" , err . message ) ;
85
+ return res . redirect ( "back" ) ;
86
+ }
87
+ else
88
+ {
89
+ res . render ( "posts/show" , { post :foundPost } ) ;
90
+ }
91
+ } ) ;
92
+ } ) ;
93
+ // Edit Post Route
94
+ app . get ( "/f/:channel/:id/edit" , isLoggedIn , ( req , res ) => {
95
+ Post . findById ( req . params . id , ( err , foundPost ) => {
96
+ if ( err )
97
+ {
98
+ req . flash ( "error" , err . message ) ;
99
+ return res . redirect ( "back" ) ;
100
+ }
101
+ else
102
+ {
103
+ res . render ( "posts/edit" , { post :foundPost } ) ;
104
+ }
105
+ } ) ;
106
+ } ) ;
107
+ app . put ( "/f/:channel/:id" , isLoggedIn , ( req , res ) => {
108
+ Post . findByIdAndUpdate ( req . params . id , req . body . post , ( err , updatedPost ) => {
109
+ if ( err )
110
+ {
111
+ req . flash ( "error" , err . message ) ;
112
+ return res . redirect ( "back" ) ;
113
+ }
114
+ else
115
+ {
116
+ res . redirect ( "/f/" + req . params . channel + "/" + req . params . id ) ;
117
+ }
118
+ } ) ;
119
+ } ) ;
120
+ // Comment Routes
121
+ app . get ( "/f/:channel/:id/comments/new" , isLoggedIn , ( req , res ) => {
122
+ Post . findById ( req . params . id , ( err , foundPost ) => {
123
+ if ( err )
124
+ {
125
+ req . flash ( "error" , err . message ) ;
126
+ return res . redirect ( "back" ) ;
127
+ }
128
+ else
129
+ {
130
+ res . render ( "comments/new" , { post :foundPost } ) ;
131
+ }
132
+ } ) ;
133
+ } ) ;
134
+ app . post ( "/f/:channel/:id/comments/new" , isLoggedIn , ( req , res ) => {
135
+ Post . findById ( req . params . id , ( err , foundPost ) => {
136
+ if ( err )
137
+ {
138
+ req . flash ( "error" , err . message ) ;
139
+ return res . redirect ( "back" ) ;
140
+ }
141
+ else
142
+ {
143
+ Comment . create ( req . body . comment , ( err , comment ) => {
144
+ if ( err )
145
+ {
146
+ req . flash ( "error" , err . message ) ;
147
+ return res . redirect ( "back" ) ;
148
+ }
149
+ else
150
+ {
151
+ comment . author . id = req . user . _id ;
152
+ comment . author . username = req . user . username ;
153
+ comment . parents . push ( comment . _id ) ;
154
+ comment . markModified ( 'parents' ) ;
155
+ comment . save ( ) ;
156
+ foundPost . comments . push ( comment ) ;
157
+ foundPost . markModified ( 'comments' ) ;
158
+ foundPost . save ( ) ;
159
+ req . flash ( "success" , "Successfully added comment" ) ;
160
+ res . redirect ( "/f/" + req . params . channel + "/" + req . params . id ) ;
161
+ }
162
+ } )
163
+ }
164
+ } )
165
+ } )
78
166
// ================
79
167
// AUTH ROUTES
80
168
// ================
0 commit comments