-
Notifications
You must be signed in to change notification settings - Fork 62
/
mysql_repository.go
233 lines (201 loc) · 6.58 KB
/
mysql_repository.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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 subject
import (
"context"
"errors"
"math"
"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/model/generic"
"github.com/bangumi/server/internal/person"
"github.com/bangumi/server/internal/pkg/errgo"
)
type mysqlRepo struct {
q *query.Query
log *zap.Logger
}
func NewMysqlRepo(q *query.Query, log *zap.Logger) (domain.SubjectRepo, error) {
return mysqlRepo{q: q, log: log.Named("subject.mysqlRepo")}, nil
}
func (r mysqlRepo) Get(ctx context.Context, id model.SubjectID) (model.Subject, error) {
s, err := r.q.Subject.WithContext(ctx).Preload(r.q.Subject.Fields).Where(r.q.Subject.ID.Eq(id)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return model.Subject{}, domain.ErrNotFound
}
r.log.Error("unexpected error happened", zap.Error(err))
return model.Subject{}, errgo.Wrap(err, "dal")
}
return ConvertDao(s), nil
}
func ConvertDao(s *dao.Subject) model.Subject {
var date string
if !s.Fields.Date.IsZero() {
date = s.Fields.Date.Format("2006-01-02")
}
return model.Subject{
Redirect: s.Fields.Redirect,
Date: date,
ID: s.ID,
Name: s.Name,
NameCN: s.NameCN,
TypeID: s.TypeID,
Image: s.Image,
PlatformID: s.Platform,
Infobox: s.Infobox,
Summary: s.Summary,
Volumes: s.Volumes,
Eps: s.Eps,
Wish: s.Wish,
Collect: s.Collect,
Doing: s.Doing,
OnHold: s.OnHold,
CompatRawTags: s.Fields.Tags,
Dropped: s.Dropped,
Airtime: s.Airtime,
NSFW: s.Nsfw,
Ban: s.Ban,
Rating: rating(s.Fields),
}
}
func rating(f dao.SubjectField) model.Rating {
var total = f.Rate1 + f.Rate2 + f.Rate3 + f.Rate4 + f.Rate5 +
f.Rate6 + f.Rate7 + f.Rate8 + f.Rate9 + f.Rate10
if total == 0 {
return model.Rating{}
}
var score = float64(1*f.Rate1+2*f.Rate2+3*f.Rate3+4*f.Rate4+5*f.Rate5+
6*f.Rate6+7*f.Rate7+8*f.Rate8+9*f.Rate9+10*f.Rate10) / float64(total)
return model.Rating{
Rank: f.Rank,
Total: total,
Count: model.Count{
Field1: f.Rate1,
Field2: f.Rate2,
Field3: f.Rate3,
Field4: f.Rate4,
Field5: f.Rate5,
Field6: f.Rate6,
Field7: f.Rate7,
Field8: f.Rate8,
Field9: f.Rate9,
Field10: f.Rate10,
},
Score: math.Round(score*10) / 10,
}
}
func (r mysqlRepo) GetPersonRelated(
ctx context.Context, personID model.PersonID,
) ([]domain.SubjectPersonRelation, error) {
relations, err := r.q.PersonSubjects.WithContext(ctx).
Joins(r.q.PersonSubjects.Subject).
Joins(r.q.PersonSubjects.Person).
Where(r.q.PersonSubjects.PersonID.Eq(personID)).Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")
}
var rel = make([]domain.SubjectPersonRelation, 0, len(relations))
for _, relation := range relations {
rel = append(rel, domain.SubjectPersonRelation{
SubjectID: relation.SubjectID,
PersonID: relation.PersonID,
TypeID: relation.PrsnPosition,
})
}
return rel, nil
}
func (r mysqlRepo) GetCharacterRelated(
ctx context.Context,
characterID model.CharacterID,
) ([]domain.SubjectCharacterRelation, error) {
relations, err := r.q.CharacterSubjects.WithContext(ctx).
Joins(r.q.CharacterSubjects.Subject).
Where(r.q.CharacterSubjects.CharacterID.Eq(characterID)).Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")
}
var rel = make([]domain.SubjectCharacterRelation, 0, len(relations))
for _, relation := range relations {
rel = append(rel, domain.SubjectCharacterRelation{
SubjectID: relation.SubjectID,
CharacterID: relation.CharacterID,
TypeID: relation.CrtType,
})
}
return rel, nil
}
func (r mysqlRepo) GetSubjectRelated(
ctx context.Context,
subjectID model.SubjectID,
) ([]domain.SubjectInternalRelation, error) {
relations, err := r.q.SubjectRelation.WithContext(ctx).
Joins(r.q.SubjectRelation.Subject).Where(r.q.SubjectRelation.SubjectID.Eq(subjectID)).
Order(r.q.SubjectRelation.Order).Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")
}
var rel = make([]domain.SubjectInternalRelation, 0, len(relations))
for _, relation := range relations {
rel = append(rel, domain.SubjectInternalRelation{
SourceID: subjectID,
DestinationID: relation.Subject.ID,
TypeID: relation.RelationType,
})
}
return rel, nil
}
func (r mysqlRepo) GetByIDs(
ctx context.Context, ids ...model.SubjectID,
) (map[model.SubjectID]model.Subject, error) {
records, err := r.q.Subject.WithContext(ctx).
Joins(r.q.Subject.Fields).Where(r.q.Subject.ID.In(generic.SubjectIDToValuerSlice(ids)...)).Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")
}
var result = make(map[model.SubjectID]model.Subject, len(ids))
for _, s := range records {
result[s.ID] = ConvertDao(s)
}
return result, nil
}
func (r mysqlRepo) GetActors(
ctx context.Context,
subjectID model.SubjectID,
characterIDs ...model.CharacterID,
) (map[model.CharacterID][]model.Person, error) {
relations, err := r.q.Cast.WithContext(ctx).
Preload(r.q.Cast.Person.Fields).
Where(r.q.Cast.CharacterID.In(generic.CharacterIDToValuerSlice(characterIDs)...), r.q.Cast.SubjectID.Eq(subjectID)).
Order(r.q.Cast.PersonID).
Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")
}
var results = make(map[model.CharacterID][]model.Person, len(relations))
for _, relation := range relations {
// TODO: should pre-alloc a big slice and split it as results.
results[relation.CharacterID] = append(results[relation.CharacterID], person.ConvertDao(&relation.Person))
}
return results, nil
}