-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow_work_bookcase.go
More file actions
95 lines (77 loc) · 2.62 KB
/
show_work_bookcase.go
File metadata and controls
95 lines (77 loc) · 2.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
package endpoints
import (
"fantlab/core/converters"
"fantlab/core/db"
"fantlab/core/helpers"
"fantlab/pb"
"net/http"
"strconv"
"google.golang.org/protobuf/proto"
)
func (api *API) ShowWorkBookcase(r *http.Request) (int, proto.Message) {
params := struct {
// id книжной полки
BookcaseId uint64 `http:"id,path"`
// номер страницы (>0, по умолчанию - 1)
Page uint64 `http:"page,query"`
// кол-во элементов на странице ([5..50], по умолчанию - 50)
Limit uint64 `http:"limit,query"`
// сортировать по: порядку - order (по умолчанию, если иное не задано в настройках полки), автору - author, названию - title, оригинальному названию - orig_title, году - year, количеству оценок - mark_count, средней оценке - avg_mark
SortBy string `http:"sort,query"`
}{
Page: 1,
Limit: api.services.AppConfig().BookcaseItemInPage,
}
api.bindParams(¶ms, r)
if params.BookcaseId == 0 {
return api.badParam("id")
}
if params.Page == 0 {
return api.badParam("page")
}
if !helpers.IsValidLimit(params.Limit) {
return api.badParam("limit")
}
sortBy := params.SortBy
if len(sortBy) != 0 {
if _, ok := db.WorkSortMap[sortBy]; !ok {
return api.badParam("sort")
}
}
dbBookcase, err := api.services.DB().FetchTypedBookcase(r.Context(), db.BookcaseWorkType, 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 dbBookcase.BookcaseShared == 0 && userId != dbBookcase.UserId {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.BookcaseId, 10),
}
}
if len(sortBy) == 0 {
for order, defaultSort := range db.WorkDefaultSortMap {
if defaultSort == dbBookcase.DefaultSort {
sortBy = order
break
}
}
}
offset := params.Limit * (params.Page - 1)
dbResponse, err := api.services.DB().FetchWorkBookcase(r.Context(), dbBookcase.BookcaseId, params.Limit, offset, sortBy, userId)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
editionBookcase := converters.GetWorkBookcase(dbResponse, dbBookcase, params.Page, params.Limit)
return http.StatusOK, editionBookcase
}