GeezerVison is showing github.com — feat: update index and index subjects (#234) · gitLibs/server@c1ee878
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: internal/cmd/gen/gorm/main.go +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -215,8 +215,15 @@ func main() { | 215 | 215 | gen.FieldTrimPrefix("idx_"), | 216 | 216 | gen.FieldType("idx_id", "uint32"), | 217 | 217 | gen.FieldType("idx_uid", userIDTypeString), | | 218 | + gen.FieldType("idx_collects", "uint32"), | | 219 | + // 变量重命名 | 218 | 220 | gen.FieldRename("idx_uid", "CreatorID"), | 219 | | - gen.FieldType("idx_collects", "uint32"))) | | 221 | + gen.FieldRename("idx_dateline", "CreatedTime"), | | 222 | + gen.FieldRename("idx_lasttouch", "UpdatedTime"), | | 223 | + gen.FieldRename("idx_replies", "ReplyCount"), | | 224 | + gen.FieldRename("idx_collects", "CollectCount"), | | 225 | + gen.FieldRename("idx_subject_total", "SubjectCount"), | | 226 | + )) | 220 | 227 |
| 221 | 228 | modelPersonField := g.GenerateModelAs("chii_person_fields", "PersonField", | 222 | 229 | gen.FieldTrimPrefix("prsn_"), | @@ -382,6 +389,11 @@ func main() { | 382 | 389 | gen.FieldRelate(field.BelongsTo, "Subject", modelSubject, &field.RelateConfig{ | 383 | 390 | GORMTag: "foreignKey:idx_rlt_sid;references:subject_id", | 384 | 391 | }), | | 392 | + // 变量重命名 | | 393 | + gen.FieldRename("idx_rlt_rid", "IndexID"), | | 394 | + gen.FieldRename("idx_rlt_sid", "SubjectID"), | | 395 | + gen.FieldRename("idx_rlt_type", "SubjectType"), | | 396 | + gen.FieldRename("idx_rlt_dateline", "CreatedTime"), | 385 | 397 | )) | 386 | 398 |
| 387 | 399 | g.ApplyBasic(g.GenerateModelAs("chii_rev_text", "RevisionText", | |
Collapse file Copy file name to clipboardExpand all lines: internal/ctrl/ctrl.go +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ func New( | 32 | 32 | person domain.PersonRepo, | 33 | 33 | character domain.CharacterRepo, | 34 | 34 | collection domain.CollectionRepo, | | 35 | + index domain.IndexRepo, | 35 | 36 | timeline domain.TimeLineRepo, | 36 | 37 | metric tally.Scope, | 37 | 38 | user domain.UserRepo, | @@ -53,6 +54,7 @@ func New( | 53 | 54 | episode: episode, | 54 | 55 | subject: subject, | 55 | 56 | character: character, | | 57 | + index: index, | 56 | 58 | collection: collection, | 57 | 59 | timeline: timeline, | 58 | 60 |
| @@ -81,6 +83,7 @@ type Ctrl struct { | 81 | 83 | subject subject.Repo | 82 | 84 | character domain.CharacterRepo | 83 | 85 | collection domain.CollectionRepo | | 86 | + index domain.IndexRepo | 84 | 87 | timeline domain.TimeLineRepo | 85 | 88 | metricUserQueryCached tally.Counter | 86 | 89 | metricUserQueryCount tally.Counter | |
Collapse file Copy file name to clipboard +67Lines changed: 67 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// SPDX-License-Identifier: AGPL-3.0-only | | 2 | +// | | 3 | +// This program is free software: you can redistribute it and/or modify | | 4 | +// it under the terms of the GNU Affero General Public License as published | | 5 | +// by the Free Software Foundation, version 3. | | 6 | +// | | 7 | +// This program is distributed in the hope that it will be useful, | | 8 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of | | 9 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | 10 | +// See the GNU Affero General Public License for more details. | | 11 | +// | | 12 | +// You should have received a copy of the GNU Affero General Public License | | 13 | +// along with this program. If not, see <https://www.gnu.org/licenses/> | | 14 | + | | 15 | +package ctrl | | 16 | + | | 17 | +import ( | | 18 | + "context" | | 19 | + "errors" | | 20 | + "time" | | 21 | + | | 22 | + "go.uber.org/zap" | | 23 | + | | 24 | + "github.com/bangumi/server/internal/ctrl/internal/cachekey" | | 25 | + "github.com/bangumi/server/internal/domain" | | 26 | + "github.com/bangumi/server/internal/pkg/errgo" | | 27 | + "github.com/bangumi/server/internal/web/res" | | 28 | +) | | 29 | + | | 30 | +func (ctl Ctrl) GetIndexWithCache(c context.Context, id uint32) (res.Index, bool, error) { | | 31 | + var key = cachekey.Index(id) | | 32 | + | | 33 | + var r res.Index | | 34 | + ok, err := ctl.cache.Get(c, key, &r) | | 35 | + if err != nil { | | 36 | + return r, ok, errgo.Wrap(err, "cache.Get") | | 37 | + } | | 38 | + | | 39 | + if ok { | | 40 | + return r, ok, nil | | 41 | + } | | 42 | + | | 43 | + i, err := ctl.index.Get(c, id) | | 44 | + if err != nil { | | 45 | + if errors.Is(err, domain.ErrNotFound) { | | 46 | + return res.Index{}, false, nil | | 47 | + } | | 48 | + | | 49 | + return res.Index{}, false, errgo.Wrap(err, "Index.Get") | | 50 | + } | | 51 | + | | 52 | + u, err := ctl.GetUser(c, i.CreatorID) | | 53 | + if err != nil { | | 54 | + if errors.Is(err, domain.ErrNotFound) { | | 55 | + ctl.log.Error("index missing creator", zap.Uint32("index_id", id), i.CreatorID.Zap()) | | 56 | + } | | 57 | + return res.Index{}, false, errgo.Wrap(err, "failed to get creator: user.GetByID") | | 58 | + } | | 59 | + | | 60 | + r = res.IndexModelToResponse(&i, u) | | 61 | + | | 62 | + if e := ctl.cache.Set(c, key, r, time.Hour); e != nil { | | 63 | + ctl.log.Error("can't set response to cache", zap.Error(e)) | | 64 | + } | | 65 | + | | 66 | + return r, true, nil | | 67 | +} |
Collapse file Copy file name to clipboardExpand all lines: internal/dal/dao/chii_index.gen.go +5-5Lines changed: 5 additions & 5 deletions Collapse file Copy file name to clipboardExpand all lines: internal/dal/dao/chii_index_related.gen.go +9-9Lines changed: 9 additions & 9 deletions Collapse file Copy file name to clipboardExpand all lines: internal/dal/query/chii_index.gen.go +20-20Lines changed: 20 additions & 20 deletions Collapse file Copy file name to clipboardExpand all lines: internal/dal/query/chii_index_related.gen.go +22-22Lines changed: 22 additions & 22 deletions Collapse file Copy file name to clipboardExpand all lines: internal/domain/error.go +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,3 +31,5 @@ var ErrUserNotFound = errgo.MsgNoTrace(ErrNotFound, "user not found") | 31 | 31 | var ErrSubjectNotCollected = errgo.MsgNoTrace(ErrNotFound, "subject is not collected by user") | 32 | 32 |
| 33 | 33 | var ErrInput = errors.New("input not valid") | | 34 | + | | 35 | +var ErrExists = errors.New("item already exists") |
Collapse file Copy file name to clipboardExpand all lines: internal/domain/index.go +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,11 +23,20 @@ import ( | 23 | 23 |
| 24 | 24 | type IndexRepo interface { | 25 | 25 | Get(ctx context.Context, id model.IndexID) (model.Index, error) | | 26 | + New(ctx context.Context, i *model.Index) error | | 27 | + Update(ctx context.Context, id model.IndexID, title string, desc string) error | | 28 | + Delete(ctx context.Context, id model.IndexID) error | 26 | 29 |
| 27 | 30 | CountSubjects(ctx context.Context, id model.IndexID, subjectType model.SubjectType) (int64, error) | 28 | 31 | ListSubjects( | 29 | 32 | ctx context.Context, id model.IndexID, subjectType model.SubjectType, limit, offset int, | 30 | 33 | ) ([]IndexSubject, error) | | 34 | + AddOrUpdateIndexSubject( | | 35 | + ctx context.Context, id model.IndexID, subjectID model.SubjectID, sort uint32, comment string, | | 36 | + ) (*IndexSubject, error) | | 37 | + DeleteIndexSubject( | | 38 | + ctx context.Context, id model.IndexID, subjectID model.SubjectID, | | 39 | + ) error | 31 | 40 | } | 32 | 41 |
| 33 | 42 | type IndexSubject struct { | |
|