-
Notifications
You must be signed in to change notification settings - Fork 62
/
mysql_repository.go
145 lines (125 loc) · 4.03 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
// 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 person
import (
"context"
"errors"
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/bangumi/server/dal/dao"
"github.com/bangumi/server/dal/query"
"github.com/bangumi/server/domain"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/generic/slice"
)
type mysqlRepo struct {
q *query.Query
log *zap.Logger
}
func NewMysqlRepo(q *query.Query, log *zap.Logger) Repo {
return mysqlRepo{q: q, log: log.Named("person.mysqlRepo")}
}
func (r mysqlRepo) Get(ctx context.Context, id model.PersonID) (model.Person, error) {
p, err := r.q.Person.WithContext(ctx).Joins(r.q.Person.Fields).Where(r.q.Person.ID.Eq(id)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return model.Person{}, domain.ErrNotFound
}
r.log.Error("unexpected error happened", zap.Error(err))
return model.Person{}, errgo.Wrap(err, "dal")
}
return convertDao(p), nil
}
func (r mysqlRepo) GetSubjectRelated(
ctx context.Context, subjectID model.SubjectID,
) ([]domain.SubjectPersonRelation, error) {
relations, err := r.q.PersonSubjects.WithContext(ctx).
Where(r.q.PersonSubjects.SubjectID.Eq(subjectID)).Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")
}
var rel = make([]domain.SubjectPersonRelation, len(relations))
for i, relation := range relations {
rel[i] = domain.SubjectPersonRelation{
PersonID: relation.PersonID,
TypeID: relation.PrsnPosition,
}
}
return rel, nil
}
func (r mysqlRepo) GetCharacterRelated(
ctx context.Context,
characterID model.CharacterID,
) ([]domain.PersonCharacterRelation, error) {
relations, err := r.q.Cast.WithContext(ctx).
Where(r.q.Cast.CharacterID.Eq(characterID)).
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([]domain.PersonCharacterRelation, 0, len(relations))
for _, relation := range relations {
results = append(results, domain.PersonCharacterRelation{
CharacterID: relation.CharacterID,
PersonID: relation.PersonID,
SubjectID: relation.SubjectID,
})
}
return results, nil
}
func (r mysqlRepo) GetByIDs(ctx context.Context, ids []model.PersonID) (map[model.PersonID]model.Person, error) {
u, err := r.q.Person.WithContext(ctx).Joins(r.q.Person.Fields).
Where(r.q.Person.ID.In(slice.ToValuer(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.PersonID]model.Person, len(ids))
for _, p := range u {
result[p.ID] = convertDao(p)
}
return result, nil
}
func convertDao(p *dao.Person) model.Person {
return model.Person{
Redirect: p.Redirect,
Type: p.Type,
ID: p.ID,
Name: p.Name,
Image: p.Img,
Infobox: p.Infobox,
Summary: p.Summary,
Locked: p.Ban != 0,
CollectCount: p.Collects,
CommentCount: p.Comment,
//
Producer: p.Producer,
Mangaka: p.Mangaka,
Artist: p.Artist,
Seiyu: p.Seiyu,
Writer: p.Writer,
Illustrator: p.Illustrator,
Actor: p.Actor,
//
FieldBloodType: p.Fields.Bloodtype,
FieldGender: p.Fields.Gender,
FieldBirthYear: p.Fields.BirthYear,
FieldBirthMon: p.Fields.BirthMon,
FieldBirthDay: p.Fields.BirthDay,
}
}