-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_follow.go
89 lines (69 loc) · 1.94 KB
/
user_follow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (h *Handler) userFollowPostHandler() gin.HandlerFunc {
return func (c *gin.Context) {
s := h.sessionFromRequest(c)
id := c.PostForm("user_id")
if err := h.Service.FollowUser(s.User.ID, id); err != nil {
h.Service.Logger.Print(err)
h.pushErr("follow_user_err", err)
http.Redirect(c.Writer, c.Request, c.Request.Referer(), http.StatusFound)
return
}
http.Redirect(c.Writer, c.Request, c.Request.Referer(), http.StatusFound)
}
}
func (h *Handler) userUnfollowDeleteHandler() gin.HandlerFunc {
return func (c *gin.Context) {
s := h.sessionFromRequest(c)
id := c.PostForm("user_id")
if err := h.Service.UnfollowUser(s.User.ID, id); err != nil {
h.Service.Logger.Print(err)
h.pushErr("follow_user_err", err)
http.Redirect(c.Writer, c.Request, c.Request.Referer(), http.StatusFound)
return
}
http.Redirect(c.Writer, c.Request, c.Request.Referer(), http.StatusFound)
}
}
func (h *Handler) userFollowingGetHandler() gin.HandlerFunc {
return func (c *gin.Context) {
s := h.sessionFromRequest(c)
username := c.Param("username")
users, err := h.Service.Queries.GetFollowingByUsername(username)
if err != nil {
h.Service.Logger.Print(err)
c.HTML(http.StatusOK, "err.html", gin.H{
"Session": s,
"Error": err.Error(),
})
return
}
c.HTML(http.StatusOK, "following.html", gin.H{
"Session": s,
"Users": users,
})
}
}
func (h *Handler) userFollowersGetHandler() gin.HandlerFunc {
return func (c *gin.Context) {
s := h.sessionFromRequest(c)
username := c.Param("username")
users, err := h.Service.Queries.GetFollowersByUsername(username)
if err != nil {
h.Service.Logger.Print(err)
c.HTML(http.StatusOK, "err.html", gin.H{
"Session": s,
"Error": err.Error(),
})
return
}
c.HTML(http.StatusOK, "followers.html", gin.H{
"Session": s,
"Users": users,
})
}
}