/
subject.go
92 lines (84 loc) · 2.74 KB
/
subject.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package index
import (
"errors"
"net/http"
"github.com/labstack/echo/v4"
"github.com/trim21/errgo"
"github.com/bangumi/server/domain/gerr"
"github.com/bangumi/server/web/req"
"github.com/bangumi/server/web/res"
)
func (h Handler) AddIndexSubject(c echo.Context) error {
var reqData req.IndexAddSubject
if err := c.Echo().JSONSerializer.Deserialize(c, &reqData); err != nil {
return res.JSONError(c, err)
}
return h.addOrUpdateIndexSubject(c, reqData)
}
func (h Handler) UpdateIndexSubject(c echo.Context) error {
var reqData req.IndexSubjectInfo
if err := c.Echo().JSONSerializer.Deserialize(c, &reqData); err != nil {
return res.JSONError(c, err)
}
subjectID, err := req.ParseID(c.Param("subject_id"))
if err != nil {
return errgo.Wrap(err, "subject id is invalid")
}
return h.addOrUpdateIndexSubject(c, req.IndexAddSubject{
SubjectID: subjectID,
IndexSubjectInfo: &reqData,
})
}
func (h Handler) addOrUpdateIndexSubject(c echo.Context, payload req.IndexAddSubject) error {
if err := h.ensureValidStrings(payload.Comment); err != nil {
return err
}
indexID, err := req.ParseID(c.Param("id"))
if err != nil {
return err
}
index, err := h.ensureIndexPermission(c, indexID)
if err != nil {
return err
}
indexSubject, err := h.i.AddOrUpdateIndexSubject(c.Request().Context(),
index.ID, payload.SubjectID, payload.SortKey, payload.Comment)
if err != nil {
if errors.Is(err, gerr.ErrSubjectNotFound) {
return res.NotFound("subject not found")
}
return errgo.Wrap(err, "failed to edit subject in the index")
}
return c.JSON(http.StatusOK, indexSubjectToResp(*indexSubject))
}
func (h Handler) RemoveIndexSubject(c echo.Context) error {
indexID, err := req.ParseID(c.Param("id"))
if err != nil {
return err
}
index, err := h.ensureIndexPermission(c, indexID)
if err != nil {
return err
}
subjectID, err := req.ParseID(c.Param("subject_id"))
if err != nil {
return errgo.Wrap(err, "subject id is invalid")
}
if err = h.i.DeleteIndexSubject(c.Request().Context(), index.ID, subjectID); err != nil {
return errgo.Wrap(err, "failed to delete subject from index")
}
return nil
}