/
controller.go
109 lines (97 loc) · 3.42 KB
/
controller.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package follow
import (
"github.com/julienschmidt/httprouter"
"github.com/simonesestito/wasaphoto/service/api"
"github.com/simonesestito/wasaphoto/service/api/route"
"github.com/simonesestito/wasaphoto/service/features/user"
"net/http"
)
type Controller struct {
Service Service
}
func (controller Controller) ListRoutes() []route.Route {
return []route.Route{
route.SecureRoute{
Method: http.MethodPut,
Path: "/users/:userId/followings/:followedId",
Handler: controller.followUser,
},
route.SecureRoute{
Method: http.MethodDelete,
Path: "/users/:userId/followings/:followedId",
Handler: controller.unfollowUser,
},
route.SecureRoute{
Method: http.MethodGet,
Path: "/users/:userId/followers/",
Handler: controller.listFollowers,
},
route.SecureRoute{
Method: http.MethodGet,
Path: "/users/:userId/followings/",
Handler: controller.listFollowings,
},
}
}
func (controller Controller) followUser(w http.ResponseWriter, _ *http.Request, params httprouter.Params, context route.SecureRequestContext) {
args, bodyErr := api.ParseRequestVariables(params, &followerParams{}, context.Logger)
if bodyErr != nil {
http.Error(w, bodyErr.Message, bodyErr.StatusCode)
return
}
if args.UserId != context.UserId {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
err := controller.Service.FollowUser(context.UserId, args.FollowedId)
result := userFollow{
FollowingId: args.FollowedId,
FollowerId: context.UserId,
}
api.HandlePutResult(result, err, w, context.Logger)
}
func (controller Controller) unfollowUser(w http.ResponseWriter, _ *http.Request, params httprouter.Params, context route.SecureRequestContext) {
args, bodyErr := api.ParseRequestVariables(params, &followerParams{}, context.Logger)
if bodyErr != nil {
http.Error(w, bodyErr.Message, bodyErr.StatusCode)
return
}
if args.UserId != context.UserId {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
err := controller.Service.UnfollowUser(context.UserId, args.FollowedId)
api.HandleErrorsResponse(err, w, http.StatusNoContent, context.Logger)
}
func (controller Controller) listFollowers(w http.ResponseWriter, r *http.Request, params httprouter.Params, context route.SecureRequestContext) {
args, bodyErr := api.ParseAllRequestVariables(r, params, &user.IdUserCursor{}, context.Logger)
if bodyErr != nil {
http.Error(w, bodyErr.Message, bodyErr.StatusCode)
return
}
followers, cursor, err := controller.Service.ListFollowersAs(args.UserId, context.UserId, args.PageCursorOrEmpty)
if err != nil {
api.HandleErrorsResponse(err, w, http.StatusOK, context.Logger)
} else {
api.SendJson(w, api.PageResult[user.User]{
NextPageCursor: cursor,
PageData: followers,
}, http.StatusOK, context.Logger)
}
}
func (controller Controller) listFollowings(w http.ResponseWriter, r *http.Request, params httprouter.Params, context route.SecureRequestContext) {
args, bodyErr := api.ParseAllRequestVariables(r, params, &user.IdUserCursor{}, context.Logger)
if bodyErr != nil {
http.Error(w, bodyErr.Message, bodyErr.StatusCode)
return
}
followers, cursor, err := controller.Service.ListFollowingsAs(args.UserId, context.UserId, args.PageCursorOrEmpty)
if err != nil {
api.HandleErrorsResponse(err, w, http.StatusOK, context.Logger)
} else {
api.SendJson(w, api.PageResult[user.User]{
NextPageCursor: cursor,
PageData: followers,
}, http.StatusOK, context.Logger)
}
}