-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvote_response.go
More file actions
86 lines (68 loc) · 2.4 KB
/
vote_response.go
File metadata and controls
86 lines (68 loc) · 2.4 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
package endpoints
import (
"fantlab/core/converters"
"fantlab/core/db"
"fantlab/pb"
"net/http"
"strconv"
"google.golang.org/protobuf/proto"
)
func (api *API) VoteResponse(r *http.Request) (int, proto.Message) {
var params struct {
// id отзыва
ResponseId uint64 `http:"id,path"`
// голос (true - плюс, false - минус)
VotePlus bool `http:"vote_plus,form"`
}
api.bindParams(¶ms, r)
if params.ResponseId == 0 {
return api.badParam("id")
}
dbResponse, err := api.services.DB().FetchResponse(r.Context(), params.ResponseId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.ResponseId, 10),
}
}
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
user := api.getUser(r)
// NOTE Пропущен весь кусок логики относительно Profile->no_vote_minus, поскольку этот запрет на выставление минусов
// задан хардкодом в Auth.pm
if !params.VotePlus && !(user.OwnResponsesRating >= api.services.AppConfig().MinUserOwnResponsesRatingForMinusAbility) {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Вы не можете ставить минусы отзывам",
}
}
if user.UserId == dbResponse.UserId {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Нельзя оценить собственный отзыв",
}
}
responseUserVoteCount, err := api.services.DB().FetchResponseUserVoteCount(r.Context(), user.UserId, dbResponse.ResponseId)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
if responseUserVoteCount > 0 {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Вы уже оценивали данный отзыв",
}
}
dbResponse, err = api.services.DB().UpdateResponseVotes(r.Context(), dbResponse.ResponseId, user.UserId, params.VotePlus)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
response := converters.GetResponseRating(dbResponse)
return http.StatusOK, response
}