-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_bookcase_item_comment.go
More file actions
87 lines (69 loc) · 2.11 KB
/
Copy pathedit_bookcase_item_comment.go
File metadata and controls
87 lines (69 loc) · 2.11 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
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
package endpoints
import (
"fantlab/core/converters"
"fantlab/core/db"
"fantlab/pb"
"net/http"
"strconv"
"strings"
"google.golang.org/protobuf/proto"
)
func (api *API) EditBookcaseItemComment(r *http.Request) (int, proto.Message) {
var params struct {
// id item-а книжной полки
BookcaseItemId uint64 `http:"id,path"`
// текст комментария
Comment string `http:"comment,form"`
}
api.bindParams(¶ms, r)
if params.BookcaseItemId == 0 {
return api.badParam("id")
}
dbBookcaseItem, err := api.services.DB().FetchBookcaseItem(r.Context(), params.BookcaseItemId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.BookcaseItemId, 10),
}
}
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
dbBookcase, err := api.services.DB().FetchBookcase(r.Context(), dbBookcaseItem.BookcaseId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.BookcaseItemId, 10),
}
}
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
userId := api.getUserId(r)
if userId != dbBookcase.UserId {
if dbBookcase.BookcaseShared == 1 {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Невозможно отредактировать чужую книжную полку",
}
} else {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.BookcaseItemId, 10),
}
}
}
text := strings.TrimSpace(params.Comment)
err = api.services.DB().UpdateBookcaseItemComment(r.Context(), dbBookcaseItem.BookcaseItemId, text)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
response := converters.GetItemComment(text)
return http.StatusOK, response
}