Skip to content

Commit 2abd750

Browse files
committed
Fix multiple posts pt2
"Refreshposts" and the "posts" template files confliceted with each other. On user login, the "posts" template would load the latest post, but lose the data after submitting 2 posts. Data was lost between the addposts func and vetlogin func. The vetlogin func used the posts template, but the addposts func used the refresh template. Making sure the addposts and vetlogin funcs both used a single template fixed the issue of the a single user post not consistently showing. To fix the issue of multiple posts not being shown required the posts template file to take a map filled with posts and loop over them. Changes made to the Posts file - rewrote the posts file to loop over a map of user posts Changes made to the main file - rewrote vetLogin func to render a map filled with the user's posts along with posts tempalte - rewrote addPosts func to render the posts template alongside a map filled with all the user's posts.
1 parent 06fde84 commit 2abd750

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

main.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,20 @@ func vetLogin(c echo.Context) error {
121121

122122
c.SetCookie(ck)
123123
c.Request().AddCookie(ck)
124-
return c.Render(http.StatusOK, "posts", "Add Posts to Your Feed")
124+
125+
uid, uidErr := accts.UserIDByEmail(usr)
126+
if uidErr != nil {
127+
return echo.NewHTTPError(http.StatusInternalServerError, err)
128+
}
129+
130+
userPosts, qryErr := posts.UserPostsByID(uid)
131+
if qryErr != nil {
132+
return echo.NewHTTPError(http.StatusInternalServerError, qryErr)
133+
}
134+
135+
return c.Render(http.StatusOK, "posts", map[string]any{
136+
"Posts": userPosts,
137+
})
125138
}
126139

127140
// this funct is only for testing purpsos
@@ -170,17 +183,30 @@ func addPosts(c echo.Context) error {
170183
return retrievalErr
171184
}
172185

173-
newPost, err := posts.CreatePosts(myPost, claims.Email)
186+
_, err := posts.CreatePosts(myPost, claims.Email)
174187

175188
// handle errs where creating a post fails
176189
if err != nil {
177190
return echo.NewHTTPError(http.StatusBadRequest, err)
178191
}
179192

193+
// get userid by email
194+
uid, uidErr := accts.UserIDByEmail(claims.Email)
195+
if uidErr != nil {
196+
return echo.NewHTTPError(http.StatusInternalServerError, err)
197+
}
198+
199+
userPosts, qryErr := posts.UserPostsByID(uid)
200+
if qryErr != nil {
201+
return echo.NewHTTPError(http.StatusInternalServerError, qryErr)
202+
}
203+
180204
// pass list of the user posts
181205
// to their posts page
182206

183-
return c.Render(http.StatusOK, "refresh", newPost.Content)
207+
return c.Render(http.StatusOK, "posts", map[string]any{
208+
"Posts": userPosts,
209+
})
184210
}
185211

186212
func main() {

ui/html/pages/posts.tmpl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@
1313

1414
<p> This is the User's Posts page</p>
1515
<a href="/"> sign out</a><br>
16-
17-
<form action= "/refresh" method="post">
16+
17+
<form action= "/refresh" method="post">
1818
<label for="content">write post</label><br>
1919
<textarea id="content" name="content" rows="8" cols="50" maxlength="140"></textarea><br>
2020
<input type="submit" value="submit">
2121
</form>
2222

2323

2424
<h3> My Feed</h3>
25-
26-
<hr>
27-
<p>{{.}}</p>
2825
<hr>
29-
30-
</body>
31-
</html>
3226

27+
{{range .Posts}}
28+
<div style="margin-bottom: 5rem">
29+
<p><strong>{{.Content}}</strong></p>
30+
</div>
31+
<hr>
32+
{{end}}
33+
34+
35+
</body>
36+
</html>
3337
{{end}}

0 commit comments

Comments
 (0)