-
Notifications
You must be signed in to change notification settings - Fork 62
/
collection.go
193 lines (159 loc) · 5.18 KB
/
collection.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 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 handler
import (
"errors"
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/errgo"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/strparse"
"github.com/bangumi/server/internal/web/res"
"github.com/bangumi/server/internal/web/res/code"
)
func (h Handler) ListCollection(c *fiber.Ctx) error {
v := h.getHTTPAccessor(c)
page, err := getPageQuery(c, defaultPageLimit, defaultMaxPageLimit)
if err != nil {
return err
}
username := c.Params("username")
if username == "" {
return fiber.NewError(http.StatusBadRequest, "missing require parameters `username`")
}
subjectType, err := parseSubjectType(c.Query("subject_type"))
if err != nil {
return fiber.NewError(http.StatusBadRequest, err.Error())
}
collectionType, err := parseCollectionType(c.Query("type"))
if err != nil {
return fiber.NewError(http.StatusBadRequest, "bad query 'type': "+err.Error())
}
u, err := h.u.GetByName(c.Context(), username)
if err != nil {
if errors.Is(err, domain.ErrNotFound) {
return fiber.NewError(http.StatusNotFound, "user doesn't exist or has been removed")
}
return errgo.Wrap(err, "user.GetByName")
}
var showPrivate = u.ID == v.ID
return h.listCollection(c, u, subjectType, collectionType, page, showPrivate)
}
func (h Handler) listCollection(
c *fiber.Ctx,
u model.User,
subjectType model.SubjectType,
collectionType uint8,
page pageQuery,
showPrivate bool,
) error {
count, err := h.u.CountCollections(c.Context(), u.ID, subjectType, collectionType, showPrivate)
if err != nil {
return errgo.Wrap(err, "user.CountCollections")
}
if count == 0 {
return c.JSON(res.Paged{Data: []int{}, Total: count, Limit: page.Limit, Offset: page.Offset})
}
if err = page.check(count); err != nil {
return err
}
collections, err := h.u.ListCollections(c.Context(),
u.ID, subjectType, collectionType, showPrivate, page.Limit, page.Offset)
if err != nil {
return errgo.Wrap(err, "user.ListCollections")
}
var data = make([]res.Collection, len(collections))
for i, collection := range collections {
c := res.Collection{
SubjectID: collection.SubjectID,
SubjectType: collection.SubjectType,
Rate: collection.Rate,
Type: collection.Type,
Tags: collection.Tags,
EpStatus: collection.EpStatus,
VolStatus: collection.VolStatus,
UpdatedAt: collection.UpdatedAt,
Private: collection.Private,
Comment: nilString(collection.Comment),
}
data[i] = c
}
return c.JSON(res.Paged{
Data: data,
Total: count,
Limit: page.Limit,
Offset: page.Offset,
})
}
func parseCollectionType(s string) (uint8, error) {
if s == "" {
return 0, nil
}
t, err := strparse.Uint8(s)
if err != nil {
return 0, fiber.NewError(http.StatusBadRequest, "bad collection type: "+strconv.Quote(s))
}
switch t {
case 1, 2, 3, 4, 5: //nolint:gomnd
return t, nil
}
return 0, fiber.NewError(http.StatusBadRequest, strconv.Quote(s)+"is not a valid collection type")
}
func (h Handler) GetCollection(c *fiber.Ctx) error {
username := c.Params("username")
if username == "" {
return res.HTTPError(c, code.BadRequest, "missing require parameters `username`")
}
subjectID, err := parseSubjectID(c.Params("subject_id"))
if err != nil {
return err
}
return h.getCollection(c, username, subjectID)
}
func (h Handler) getCollection(c *fiber.Ctx, username string, subjectID model.SubjectIDType) error {
const notFoundMessage = "subject is not collected by user"
v := h.getHTTPAccessor(c)
u, err := h.u.GetByName(c.Context(), username)
if err != nil {
if errors.Is(err, domain.ErrNotFound) {
return fiber.NewError(code.NotFound, "user doesn't exist or has been removed")
}
return errgo.Wrap(err, "user.GetByName")
}
var showPrivate = u.ID == v.ID
collection, err := h.u.GetCollection(c.Context(), u.ID, subjectID)
if err != nil {
if errors.Is(err, domain.ErrNotFound) {
return res.HTTPError(c, code.NotFound, notFoundMessage)
}
return errgo.Wrap(err, "user.GetCollection")
}
if !showPrivate && collection.Private {
return res.HTTPError(c, code.NotFound, notFoundMessage)
}
return res.JSON(c, res.Collection{
SubjectID: collection.SubjectID,
SubjectType: collection.SubjectType,
Rate: collection.Rate,
Type: collection.Type,
Tags: collection.Tags,
EpStatus: collection.EpStatus,
VolStatus: collection.VolStatus,
UpdatedAt: collection.UpdatedAt,
Private: collection.Private,
Comment: nilString(collection.Comment),
})
}