-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_response.go
More file actions
67 lines (51 loc) · 1.76 KB
/
delete_response.go
File metadata and controls
67 lines (51 loc) · 1.76 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
package endpoints
import (
"fantlab/core/db"
"fantlab/core/helpers"
"fantlab/pb"
"net/http"
"strconv"
"google.golang.org/protobuf/proto"
)
func (api *API) DeleteResponse(r *http.Request) (int, proto.Message) {
var params struct {
// id отзыва
ResponseId uint64 `http:"id,path"`
}
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,
}
}
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().DeleteResponse(r.Context(), dbResponse.ResponseId, dbResponse.WorkId, dbResponse.UserId)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
_ = api.services.DeleteWorkStatCache(r.Context(), dbResponse.WorkId)
_ = api.services.DeleteUserResponseCache(r.Context(), dbResponse.UserId, dbResponse.WorkId)
_ = api.services.DeleteUserCache(r.Context(), dbResponse.UserId)
_ = api.services.DeleteHomepageResponsesCache(r.Context())
helpers.DeleteResponseTextCache(dbResponse.ResponseId)
return http.StatusOK, &pb.Common_SuccessResponse{}
}