-
Notifications
You must be signed in to change notification settings - Fork 62
/
mysql_repo.go
189 lines (157 loc) · 5.41 KB
/
mysql_repo.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
// 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 group
import (
"context"
"errors"
"time"
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/bangumi/server/internal/dal/dao"
"github.com/bangumi/server/internal/dal/query"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/logger/log"
)
var _ domain.GroupRepo = mysqlRepo{}
func NewMysqlRepo(q *query.Query, log *zap.Logger) (domain.GroupRepo, error) {
return mysqlRepo{q: q, log: log.Named("group.mysqlRepo")}, nil
}
type mysqlRepo struct {
q *query.Query
log *zap.Logger
}
func (r mysqlRepo) getIDByName(ctx context.Context, name string) (model.GroupID, error) {
g, err := r.q.Group.WithContext(ctx).Where(r.q.Group.Name.Eq(name)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, domain.ErrNotFound
}
r.log.Error("un-expected error when getting single group", zap.Error(err), zap.String("group_name", name))
return 0, errgo.Wrap(err, "dal")
}
return g.ID, nil
}
func (r mysqlRepo) GetByID(ctx context.Context, id model.GroupID) (model.Group, error) {
g, err := r.q.Group.WithContext(ctx).Where(r.q.Group.ID.Eq(id)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return model.Group{}, domain.ErrNotFound
}
r.log.Error("un-expected error when getting single group", zap.Error(err), id.Zap())
return model.Group{}, errgo.Wrap(err, "dal")
}
return convertDao(g), nil
}
func (r mysqlRepo) GetByName(ctx context.Context, name string) (model.Group, error) {
g, err := r.q.Group.WithContext(ctx).Where(r.q.Group.Name.Eq(name)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return model.Group{}, domain.ErrNotFound
}
r.log.Error("un-expected error when getting single group", zap.Error(err), zap.String("group_name", name))
return model.Group{}, errgo.Wrap(err, "dal")
}
return convertDao(g), nil
}
func (r mysqlRepo) CountMembersByName(
ctx context.Context, name string, memberType domain.GroupMemberType,
) (int64, error) {
id, err := r.getIDByName(ctx, name)
if err != nil {
return 0, err
}
return r.countMembersByID(ctx, id, memberType)
}
func (r mysqlRepo) countMembersByID(
ctx context.Context, id model.GroupID, memberType domain.GroupMemberType,
) (int64, error) {
q := r.q.GroupMember.WithContext(ctx).Where(r.q.GroupMember.GroupID.Eq(id))
switch memberType {
case domain.GroupMemberMod:
q = q.Where(r.q.GroupMember.Moderator.Is(true))
case domain.GroupMemberNormal:
q = q.Where(r.q.GroupMember.Moderator.Is(false))
case domain.GroupMemberAll:
// do nothing
}
c, err := q.Count()
if err != nil {
r.log.Error("un-expected error when counting group member", zap.Error(err), log.GroupID(id))
return 0, errgo.Wrap(err, "dal")
}
return c, nil
}
func (r mysqlRepo) listMembersByID(
ctx context.Context, id model.GroupID, memberType domain.GroupMemberType, limit, offset int,
) ([]model.GroupMember, error) {
q := r.q.GroupMember.WithContext(ctx).Where(r.q.GroupMember.GroupID.Eq(id))
switch memberType {
case domain.GroupMemberMod:
q = q.Where(r.q.GroupMember.Moderator.Is(true))
case domain.GroupMemberNormal:
q = q.Where(r.q.GroupMember.Moderator.Is(false))
case domain.GroupMemberAll:
// do nothing
}
c, err := q.Limit(limit).Offset(offset).Order(r.q.GroupMember.CreatedTime.Desc()).Find()
if err != nil {
r.log.Error("un-expected error when counting group member", zap.Error(err), log.GroupID(id))
return nil, errgo.Wrap(err, "dal")
}
members := make([]model.GroupMember, len(c))
for i, member := range c {
members[i] = convertMember(member)
}
return members, nil
}
func (r mysqlRepo) ListMembersByName(
ctx context.Context, name string, memberType domain.GroupMemberType, limit, offset int,
) ([]model.GroupMember, error) {
id, err := r.getIDByName(ctx, name)
if err != nil {
return nil, err
}
return r.listMembersByID(ctx, id, memberType, limit, offset)
}
func (r mysqlRepo) CountMembersByID(
ctx context.Context, id model.GroupID, memberType domain.GroupMemberType,
) (int64, error) {
return r.countMembersByID(ctx, id, memberType)
}
func (r mysqlRepo) ListMembersByID(
ctx context.Context, id model.GroupID, memberType domain.GroupMemberType, limit, offset int,
) ([]model.GroupMember, error) {
return r.listMembersByID(ctx, id, memberType, limit, offset)
}
func convertDao(g *dao.Group) model.Group {
return model.Group{
Name: g.Name,
NSFW: g.Nsfw,
ID: g.ID,
Description: g.Description,
Icon: g.Icon,
MemberCount: int64(g.Members),
Title: g.Title,
CreatedAt: time.Unix(int64(g.CreatedTime), 0),
}
}
func convertMember(m *dao.GroupMember) model.GroupMember {
return model.GroupMember{
UserID: m.UserID,
Mod: m.Moderator,
JoinAt: time.Unix(int64(m.CreatedTime), 0),
}
}