-
Notifications
You must be signed in to change notification settings - Fork 62
/
service.go
109 lines (91 loc) · 3.03 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
// Copyright (c) 2022 Trim21 <trim21.me@gmail.com>
//
// 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"
"github.com/bangumi/server/domain"
"github.com/bangumi/server/internal/errgo"
"github.com/bangumi/server/model"
)
func NewService(p domain.PersonRepo, s domain.SubjectRepo) domain.PersonService {
return service{repo: p, s: s}
}
type service struct {
repo domain.PersonRepo
s domain.SubjectRepo
}
func (s service) Get(ctx context.Context, id uint32) (model.Person, error) {
return s.repo.Get(ctx, id) //nolint:wrapcheck
}
func (s service) GetSubjectRelated(
ctx context.Context, subjectID model.SubjectIDType,
) ([]model.SubjectPersonRelation, error) {
relations, err := s.repo.GetSubjectRelated(ctx, subjectID)
if err != nil {
return nil, errgo.Wrap(err, "PersonRepo.GetSubjectRelated")
}
var personIDs = make([]model.PersonIDType, len(relations))
for i, relation := range relations {
personIDs[i] = relation.PersonID
}
persons, err := s.repo.GetByIDs(ctx, personIDs...)
if err != nil {
return nil, errgo.Wrap(err, "PersonRepo.GetByIDs")
}
subject, err := s.s.Get(ctx, subjectID)
if err != nil {
return nil, errgo.Wrap(err, "SubjectRepo.Get")
}
var results = make([]model.SubjectPersonRelation, len(relations))
for i, rel := range relations {
results[i] = model.SubjectPersonRelation{
Person: persons[rel.PersonID],
Subject: subject,
TypeID: rel.TypeID,
}
}
return results, nil
}
func (s service) GetCharacterRelated(
ctx context.Context, characterID model.CharacterIDType,
) ([]model.PersonCharacterRelation, error) {
relations, err := s.repo.GetCharacterRelated(ctx, characterID)
if err != nil {
return nil, errgo.Wrap(err, "PersonRepo.GetCharacterRelated")
}
var personIDs = make([]model.PersonIDType, len(relations))
var subjectIDs = make([]model.SubjectIDType, len(relations))
for i, relation := range relations {
personIDs[i] = relation.PersonID
subjectIDs[i] = relation.SubjectID
}
persons, err := s.repo.GetByIDs(ctx, personIDs...)
if err != nil {
return nil, errgo.Wrap(err, "PersonRepo.GetByIDs")
}
subjects, err := s.s.GetByIDs(ctx, subjectIDs...)
if err != nil {
return nil, errgo.Wrap(err, "SubjectRepo.Get")
}
var results = make([]model.PersonCharacterRelation, len(relations))
for i, rel := range relations {
results[i] = model.PersonCharacterRelation{
Subject: subjects[rel.SubjectID],
Person: persons[rel.PersonID],
}
}
return results, nil
}