Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

目录直接相关的 API #234

Merged
merged 28 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
019c648
feat(indices): add router
RanKKI Oct 4, 2022
b210d38
feat(indices): create new index
RanKKI Oct 6, 2022
c215d64
feat(indices): update index's infomation
RanKKI Oct 6, 2022
055e32b
feat(indices): delete index
RanKKI Oct 6, 2022
cfa4cc2
feat(indices): update time of index
RanKKI Oct 6, 2022
5bf8aed
style(db): remove useless options
RanKKI Oct 6, 2022
a04bade
style(indices): use 'description' instead of 'desc'
RanKKI Oct 6, 2022
f05dec9
fix(indices): error handler
RanKKI Oct 6, 2022
3cbd443
feat(indices): CURD subjects in the index
RanKKI Oct 7, 2022
b02c841
style(indices): lint error
RanKKI Oct 7, 2022
757c3bf
test(indices): delete/add non-exists index/indexsubject
RanKKI Oct 7, 2022
c494a80
fix(indices): response key & lint error
RanKKI Oct 8, 2022
dbcd78e
remove: delete index
RanKKI Oct 12, 2022
70eca4c
style: use past tense variable name
RanKKI Oct 12, 2022
24a920e
test(indices): test on new index not the exists index in the test DB"
RanKKI Oct 12, 2022
bfae787
docs: api info
RanKKI Oct 12, 2022
e09984d
Revert "remove: delete index"
RanKKI Oct 12, 2022
b99f86c
remove: api of delete index
RanKKI Oct 12, 2022
074a8b2
feat: put non-exists subject in a index will create one
RanKKI Oct 13, 2022
1845ce6
style: DB field name
RanKKI Oct 15, 2022
45a75b6
fix: response on invalid payload & invalidate cache after update
RanKKI Oct 17, 2022
d5da8a9
docs: decompose requried payload and response
RanKKI Oct 17, 2022
14f1eca
Merge branch 'bangumi:master' into feat/indices_part_a
RanKKI Oct 17, 2022
f5298bb
refactor: index package
RanKKI Oct 17, 2022
4323205
refactor: indexID as param
RanKKI Oct 18, 2022
51d9451
feat: all input string should be printable
RanKKI Oct 18, 2022
714b699
fix: lint error
RanKKI Oct 22, 2022
6ce4fa6
Merge branch 'master' into feat/indices_part_a
RanKKI Oct 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/cmd/gen/gorm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,15 @@ func main() {
gen.FieldTrimPrefix("idx_"),
gen.FieldType("idx_id", "uint32"),
gen.FieldType("idx_uid", userIDTypeString),
gen.FieldType("idx_collects", "uint32"),
// 变量重命名
gen.FieldRename("idx_uid", "CreatorID"),
gen.FieldType("idx_collects", "uint32")))
gen.FieldRename("idx_dateline", "CreatedTime"),
gen.FieldRename("idx_lasttouch", "UpdatedTime"),
gen.FieldRename("idx_replies", "ReplyCount"),
gen.FieldRename("idx_collects", "CollectCount"),
gen.FieldRename("idx_subject_total", "SubjectCount"),
))

modelPersonField := g.GenerateModelAs("chii_person_fields", "PersonField",
gen.FieldTrimPrefix("prsn_"),
Expand Down Expand Up @@ -382,6 +389,11 @@ func main() {
gen.FieldRelate(field.BelongsTo, "Subject", modelSubject, &field.RelateConfig{
GORMTag: "foreignKey:idx_rlt_sid;references:subject_id",
}),
// 变量重命名
gen.FieldRename("idx_rlt_rid", "IndexID"),
gen.FieldRename("idx_rlt_sid", "SubjectID"),
gen.FieldRename("idx_rlt_type", "SubjectType"),
gen.FieldRename("idx_rlt_dateline", "CreatedTime"),
))

g.ApplyBasic(g.GenerateModelAs("chii_rev_text", "RevisionText",
Expand Down
3 changes: 3 additions & 0 deletions internal/ctrl/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func New(
person domain.PersonRepo,
character domain.CharacterRepo,
collection domain.CollectionRepo,
index domain.IndexRepo,
timeline domain.TimeLineRepo,
metric tally.Scope,
user domain.UserRepo,
Expand All @@ -53,6 +54,7 @@ func New(
episode: episode,
subject: subject,
character: character,
index: index,
collection: collection,
timeline: timeline,

Expand Down Expand Up @@ -81,6 +83,7 @@ type Ctrl struct {
subject subject.Repo
character domain.CharacterRepo
collection domain.CollectionRepo
index domain.IndexRepo
timeline domain.TimeLineRepo
metricUserQueryCached tally.Counter
metricUserQueryCount tally.Counter
Expand Down
67 changes: 67 additions & 0 deletions internal/ctrl/get_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 ctrl

import (
"context"
"errors"
"time"

"go.uber.org/zap"

"github.com/bangumi/server/internal/ctrl/internal/cachekey"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/web/res"
)

func (ctl Ctrl) GetIndexWithCache(c context.Context, id uint32) (res.Index, bool, error) {
var key = cachekey.Index(id)

var r res.Index
ok, err := ctl.cache.Get(c, key, &r)
if err != nil {
return r, ok, errgo.Wrap(err, "cache.Get")
}

if ok {
return r, ok, nil
}

i, err := ctl.index.Get(c, id)
if err != nil {
if errors.Is(err, domain.ErrNotFound) {
return res.Index{}, false, nil
}

return res.Index{}, false, errgo.Wrap(err, "Index.Get")
}

u, err := ctl.GetUser(c, i.CreatorID)
if err != nil {
if errors.Is(err, domain.ErrNotFound) {
ctl.log.Error("index missing creator", zap.Uint32("index_id", id), i.CreatorID.Zap())
}
return res.Index{}, false, errgo.Wrap(err, "failed to get creator: user.GetByID")
}

r = res.IndexModelToResponse(&i, u)

if e := ctl.cache.Set(c, key, r, time.Hour); e != nil {
ctl.log.Error("can't set response to cache", zap.Error(e))
}

return r, true, nil
}
10 changes: 5 additions & 5 deletions internal/dal/dao/chii_index.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions internal/dal/dao/chii_index_related.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions internal/dal/query/chii_index.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions internal/dal/query/chii_index_related.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/domain/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ var ErrUserNotFound = errgo.MsgNoTrace(ErrNotFound, "user not found")
var ErrSubjectNotCollected = errgo.MsgNoTrace(ErrNotFound, "subject is not collected by user")

var ErrInput = errors.New("input not valid")

var ErrExists = errors.New("item already exists")
9 changes: 9 additions & 0 deletions internal/domain/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ import (

type IndexRepo interface {
Get(ctx context.Context, id model.IndexID) (model.Index, error)
New(ctx context.Context, i *model.Index) error
Update(ctx context.Context, id model.IndexID, title string, desc string) error
Delete(ctx context.Context, id model.IndexID) error

CountSubjects(ctx context.Context, id model.IndexID, subjectType model.SubjectType) (int64, error)
ListSubjects(
ctx context.Context, id model.IndexID, subjectType model.SubjectType, limit, offset int,
) ([]IndexSubject, error)
AddOrUpdateIndexSubject(
ctx context.Context, id model.IndexID, subjectID model.SubjectID, sort uint32, comment string,
) (*IndexSubject, error)
DeleteIndexSubject(
ctx context.Context, id model.IndexID, subjectID model.SubjectID,
) error
}

type IndexSubject struct {
Expand Down
Loading