-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle_community_subscription.go
More file actions
56 lines (44 loc) · 1.29 KB
/
toggle_community_subscription.go
File metadata and controls
56 lines (44 loc) · 1.29 KB
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
package endpoints
import (
"fantlab/core/db"
"fantlab/pb"
"net/http"
"strconv"
"google.golang.org/protobuf/proto"
)
func (api *API) ToggleCommunitySubscription(r *http.Request) (int, proto.Message) {
var params struct {
// айди сообщества
CommunityId uint64 `http:"id,path"`
// подписаться - true, отписаться - false
Subscribe bool `http:"subscribe,form"`
}
api.bindParams(¶ms, r)
if params.CommunityId == 0 {
return api.badParam("id")
}
_, err := api.services.DB().FetchCommunity(r.Context(), params.CommunityId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.CommunityId, 10),
}
}
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
userId := api.getUserId(r)
if params.Subscribe {
err = api.services.DB().UpdateBlogSubscribed(r.Context(), params.CommunityId, userId)
} else {
err = api.services.DB().UpdateBlogUnsubscribed(r.Context(), params.CommunityId, userId)
}
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
return http.StatusOK, &pb.Common_SuccessResponse{}
}