-
Notifications
You must be signed in to change notification settings - Fork 62
/
service.go
112 lines (93 loc) · 3.2 KB
/
service.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
// 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 character
import (
"context"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/errgo"
"github.com/bangumi/server/internal/model"
)
func NewService(c domain.CharacterRepo, s domain.SubjectRepo, p domain.PersonRepo) domain.CharacterService {
return service{repo: c, s: s, p: p}
}
type service struct {
repo domain.CharacterRepo
s domain.SubjectRepo
p domain.PersonRepo
}
func (s service) Get(ctx context.Context, id uint32) (model.Character, error) {
return s.repo.Get(ctx, id) //nolint:wrapcheck
}
func (s service) GetPersonRelated(
ctx context.Context, personID model.PersonIDType,
) ([]model.PersonCharacterRelation, error) {
relations, err := s.repo.GetPersonRelated(ctx, personID)
if err != nil {
return nil, errgo.Wrap(err, "CharacterRepo.GetPersonRelated")
}
if len(relations) == 0 {
return []model.PersonCharacterRelation{}, nil
}
var characterIDs = make([]model.CharacterIDType, len(relations))
var subjectIDs = make([]model.SubjectIDType, len(relations))
for i, relation := range relations {
characterIDs[i] = relation.CharacterID
subjectIDs[i] = relation.SubjectID
}
characters, err := s.repo.GetByIDs(ctx, characterIDs...)
if err != nil {
return nil, errgo.Wrap(err, "CharacterRepo.GetByIDs")
}
subjects, err := s.s.GetByIDs(ctx, subjectIDs...)
if err != nil {
return nil, errgo.Wrap(err, "SubjectRepo.GetByIDs")
}
person, err := s.p.Get(ctx, personID)
if err != nil {
return nil, errgo.Wrap(err, "PersonRepo.GetByIDs")
}
var results = make([]model.PersonCharacterRelation, len(relations))
for i, rel := range relations {
results[i] = model.PersonCharacterRelation{
Character: characters[rel.CharacterID],
Person: person,
Subject: subjects[rel.SubjectID],
}
}
return results, nil
}
func (s service) GetSubjectRelated(
ctx context.Context, subjectID model.SubjectIDType,
) ([]model.SubjectCharacterRelation, error) {
relations, err := s.repo.GetSubjectRelated(ctx, subjectID)
if err != nil {
return nil, errgo.Wrap(err, "CharacterRepo.GetSubjectRelated")
}
var characterIDs = make([]model.CharacterIDType, len(relations))
for i, relation := range relations {
characterIDs[i] = relation.CharacterID
}
characters, err := s.repo.GetByIDs(ctx, characterIDs...)
if err != nil {
return nil, errgo.Wrap(err, "CharacterRepo.GetByIDs")
}
var results = make([]model.SubjectCharacterRelation, len(relations))
for i, rel := range relations {
results[i] = model.SubjectCharacterRelation{
Character: characters[rel.CharacterID],
TypeID: rel.TypeID,
}
}
return results, nil
}