-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_work_classification.go
More file actions
60 lines (42 loc) · 1.36 KB
/
get_work_classification.go
File metadata and controls
60 lines (42 loc) · 1.36 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
package endpoints
import (
"fantlab/core/converters"
"fantlab/pb"
"net/http"
"google.golang.org/protobuf/proto"
)
func (api *API) GetWorkClassification(r *http.Request) (int, proto.Message) {
var params struct {
// айди произведения
WorkId uint64 `http:"id,path"`
}
api.bindParams(¶ms, r)
if params.WorkId == 0 {
return api.badParam("id")
}
// получаем список всех жанров
genreTree := api.services.GetGenreTree(r.Context())
if genreTree == nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
// получаем кол-во классификаций
classificationCount, err := api.services.DB().GetWorkClassificationCount(r.Context(), params.WorkId)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
// получаем распределение голосов по жанрам
genreVotes, err := api.services.DB().FetchWorkGenreVotes(r.Context(), params.WorkId)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
// формируем ответ
response := converters.GetWorkClassification(genreTree, classificationCount, genreVotes)
// успех
return http.StatusOK, response
}