-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_bookcase.go
More file actions
149 lines (126 loc) · 4.62 KB
/
Copy pathedit_bookcase.go
File metadata and controls
149 lines (126 loc) · 4.62 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package endpoints
import (
"encoding/json"
"fantlab/core/db"
"fantlab/core/helpers"
"fantlab/pb"
"net/http"
"strconv"
"strings"
"google.golang.org/protobuf/proto"
)
func (api *API) EditBookcase(r *http.Request) (int, proto.Message) {
params := struct {
// id полки
BookcaseId uint64 `http:"id,path"`
// название
Title string `http:"title,form"`
// тип полки (sale - на продажу, buy - купить, read - читать, wait - ожидаю, free - прочее)
Type string `http:"type,form"`
// описание, до 50 символов (иначе будет обрезано), может быть пустым
Description string `http:"description,form"`
// приватная?
IsPrivate bool `http:"is_private,form"`
// сортировка, издания по: порядку - order (по умолчанию), автору - author, названию - title, году - year
// произведения по: порядку - order (по умолчанию), автору - author, названию - title, оригинальному названию - orig_title, году - year, количеству оценок - mark_count, средней оценке - avg_mark
// фильмы по: порядку - order (по умолчанию), названию - title, оригинальному названию - orig_title
SortBy string `http:"sort,form"`
// item-ы в формате [{"id1":"comment1"},...,{"idN":"commentN"}], id - это editionId для изданий etc, commentN может быть пустым
Items string `http:"items,form"`
}{
SortBy: "order",
}
api.bindParams(¶ms, r)
title := strings.Join(strings.Fields(params.Title), " ")
if len(title) == 0 {
return api.badParam("title")
}
if _, ok := helpers.BookcaseTypeMap[params.Type]; !ok {
return api.badParam("type")
}
dbBookcase, err := api.services.DB().FetchBookcase(r.Context(), params.BookcaseId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.BookcaseId, 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.BookcaseId, 10),
}
}
}
var itemsInfo []map[uint64]string
err = json.Unmarshal([]byte(params.Items), &itemsInfo)
if err != nil {
return api.badParam("items")
}
itemIds := make([]uint64, 0, len(itemsInfo))
for _, itemInfo := range itemsInfo {
for itemId := range itemInfo {
itemIds = append(itemIds, itemId)
}
}
var dbItemCount int
switch dbBookcase.BookcaseType {
case db.BookcaseEditionType:
if _, ok := db.EditionDefaultSortMap[params.SortBy]; !ok {
return api.badParam("sort")
}
var dbEditions []db.Edition
dbEditions, err = api.services.DB().FetchEditions(r.Context(), itemIds)
dbItemCount = len(dbEditions)
case db.BookcaseWorkType:
if _, ok := db.WorkDefaultSortMap[params.SortBy]; !ok {
return api.badParam("sort")
}
var dbWorks []db.Work
dbWorks, err = api.services.DB().FetchWorks(r.Context(), itemIds)
dbItemCount = len(dbWorks)
case db.BookcaseFilmType:
if _, ok := db.FilmDefaultSortMap[params.SortBy]; !ok {
return api.badParam("sort")
}
var dbFilms []db.Film
dbFilms, err = api.services.DB().FetchFilms(r.Context(), itemIds)
dbItemCount = len(dbFilms)
}
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
if dbItemCount != len(itemIds) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: "Не все id item-ов указаны верно",
}
}
description := strings.TrimSpace(params.Description)
maxDescriptionLength := 50
if len(description) > maxDescriptionLength {
description = description[:maxDescriptionLength]
}
err = api.services.DB().UpdateBookcase(r.Context(), dbBookcase.BookcaseId, dbBookcase.BookcaseType, params.Type,
title, description, params.SortBy, params.IsPrivate, itemsInfo)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
return http.StatusOK, &pb.Common_SuccessResponse{}
}