-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_response.go
77 lines (60 loc) · 2.2 KB
/
edit_response.go
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
package endpoints
import (
"fantlab/core/db"
"fantlab/core/helpers"
"fantlab/pb"
"fmt"
"google.golang.org/protobuf/proto"
"net/http"
"strconv"
"strings"
)
func (api *API) EditResponse(r *http.Request) (int, proto.Message) {
var params struct {
// id отзыва
ResponseId uint64 `http:"id,path"`
// новый текст отзыва
Response string `http:"response,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,
}
}
formattedResponse := api.services.AppConfig().Smiles.RemoveFromString(strings.TrimSpace(params.Response))
if uint64(len(formattedResponse)) < api.services.AppConfig().MinResponseLength {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: fmt.Sprintf("Текст сообщения слишком короткий (меньше %d символов после удаления смайлов)", api.services.AppConfig().MinResponseLength),
}
}
userId := api.getUserId(r)
userCanEditAnyResponses := api.isPermissionGranted(r, pb.Auth_Claims_PERMISSION_CAN_EDIT_ANY_RESPONSES)
if !(userId == dbResponse.UserId || userCanEditAnyResponses) {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Вы не можете отредактировать данный отзыв",
}
}
err = api.services.DB().UpdateResponse(r.Context(), dbResponse.ResponseId, formattedResponse, dbResponse.UserId)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
_ = api.services.DeleteUserCache(r.Context(), dbResponse.UserId)
_ = api.services.DeleteHomepageResponsesCache(r.Context())
helpers.DeleteResponseTextCache(dbResponse.ResponseId)
return http.StatusOK, &pb.Common_SuccessResponse{}
}