-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_work_genres.go
More file actions
118 lines (93 loc) · 3 KB
/
Copy pathset_work_genres.go
File metadata and controls
118 lines (93 loc) · 3 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
package endpoints
import (
"fantlab/core/db"
"fantlab/core/helpers"
"fantlab/pb"
"fmt"
"net/http"
"strings"
"google.golang.org/protobuf/proto"
)
func (api *API) SetWorkGenres(r *http.Request) (int, proto.Message) {
var params struct {
// айди произведения
WorkId uint64 `http:"id,path"`
// айди жанров, разделённые запятыми
GenreIds string `http:"genres,form"`
}
api.bindParams(¶ms, r)
if params.WorkId == 0 {
return api.badParam("id")
}
genreIds := helpers.ParseUints(strings.Split(params.GenreIds, ","))
if genreIds == nil {
return api.badParam("genres")
}
userId := api.getUserId(r)
// проверяем что произведение существует
{
workExists, err := api.services.DB().WorkExists(r.Context(), params.WorkId)
if err != nil && !db.IsNotFoundError(err) {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
if !workExists {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: fmt.Sprintf("Произведение с идентификатором %d не найдено", params.WorkId),
}
}
}
// проверяем что пользователь выставил оценку произведению
{
mark, err := api.services.DB().GetWorkUserMark(r.Context(), params.WorkId, userId)
if err != nil && !db.IsNotFoundError(err) {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
if mark == 0 {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Вы еще не оценили это произведение",
}
}
}
// получаем дерево жанры
genreTree := api.services.GetGenreTree(r.Context())
if genreTree == nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
// проверяем что выбраны жанры из обязательных групп
{
err := genreTree.CheckRequiredGroupsForGenreIds(genreIds)
if err != nil {
return http.StatusBadRequest, &pb.Error_Response{
Status: pb.Error_VALIDATION_FAILED,
Context: err.Error(),
}
}
}
// получаем идентификаторы всех выбранных жанров + родительские
genreIdTable := genreTree.SelectGenreIdsWithParents(genreIds)
// сохраняем выбор в базе
{
genreIds = nil
for id := range genreIdTable {
genreIds = append(genreIds, id)
}
err := api.services.DB().GenreVote(r.Context(), params.WorkId, userId, genreIds)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
}
// сбрасываем кэш юзера (для перла)
_ = api.services.DeleteUserCache(r.Context(), userId)
// успех
return http.StatusOK, &pb.Common_SuccessResponse{}
}