Skip to content

Commit

Permalink
fix: update stats for person collection
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed May 22, 2024
1 parent e8c5712 commit d7d1c2d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
54 changes: 48 additions & 6 deletions internal/collections/infra/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,28 +488,70 @@ func (r mysqlRepo) AddPersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) error {
var table = r.q.PersonCollect
err := table.WithContext(ctx).Create(&dao.PersonCollect{
collect := &dao.PersonCollect{
UserID: userID,
Category: string(cat),
TargetID: targetID,
CreatedTime: uint32(time.Now().Unix()),
}
err := r.q.Transaction(func(tx *query.Query) error {
switch cat {
case collection.PersonCollectCategoryCharacter:
character, err := tx.Character.WithContext(ctx).Where(tx.Character.ID.Eq(targetID)).Take()
if err != nil {
r.log.Error("failed to get character", zap.Error(err))
return err

Check warning on line 503 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L499-L503

Added lines #L499 - L503 were not covered by tests
}
character.Collects += 1
tx.Character.WithContext(ctx).Save(character)

Check failure on line 506 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `(github.com/bangumi/server/dal/query.characterDo).Save` is not checked (errcheck)

Check warning on line 506 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L505-L506

Added lines #L505 - L506 were not covered by tests
case collection.PersonCollectCategoryPerson:
person, err := tx.Person.WithContext(ctx).Where(tx.Person.ID.Eq(targetID)).Take()
if err != nil {
r.log.Error("failed to get person", zap.Error(err))
return err

Check warning on line 511 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L510-L511

Added lines #L510 - L511 were not covered by tests
}
person.Collects += 1
tx.Person.WithContext(ctx).Save(person)

Check failure on line 514 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `(github.com/bangumi/server/dal/query.personDo).Save` is not checked (errcheck)
}
return tx.PersonCollect.WithContext(ctx).Create(collect)
})
if err != nil {
r.log.Error("failed to create person collection record", zap.Error(err))
return errgo.Wrap(err, "dal")

Check warning on line 520 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L519-L520

Added lines #L519 - L520 were not covered by tests
}

return nil
}

func (r mysqlRepo) RemovePersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) error {
_, err := r.q.PersonCollect.WithContext(ctx).
Where(r.q.PersonCollect.UserID.Eq(userID), r.q.PersonCollect.Category.Eq(string(cat)),
r.q.PersonCollect.TargetID.Eq(targetID)).Delete()
err := r.q.Transaction(func(tx *query.Query) error {
switch cat {
case collection.PersonCollectCategoryCharacter:
character, err := tx.Character.WithContext(ctx).Where(tx.Character.ID.Eq(targetID)).Take()
if err != nil {
r.log.Error("failed to get character", zap.Error(err))
return err

Check warning on line 535 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L531-L535

Added lines #L531 - L535 were not covered by tests
}
character.Collects -= 1
tx.Character.WithContext(ctx).Save(character)

Check failure on line 538 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `(github.com/bangumi/server/dal/query.characterDo).Save` is not checked (errcheck)

Check warning on line 538 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L537-L538

Added lines #L537 - L538 were not covered by tests
case collection.PersonCollectCategoryPerson:
person, err := tx.Person.WithContext(ctx).Where(tx.Person.ID.Eq(targetID)).Take()
if err != nil {
r.log.Error("failed to get person", zap.Error(err))
return err

Check warning on line 543 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L542-L543

Added lines #L542 - L543 were not covered by tests
}
person.Collects -= 1
tx.Person.WithContext(ctx).Save(person)

Check failure on line 546 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `(github.com/bangumi/server/dal/query.personDo).Save` is not checked (errcheck)
}
_, err := tx.PersonCollect.WithContext(ctx).Where(
tx.PersonCollect.UserID.Eq(userID),
tx.PersonCollect.Category.Eq(string(cat)),
tx.PersonCollect.TargetID.Eq(targetID),
).Delete()
return err
})
if err != nil {
r.log.Error("failed to delete person collection record", zap.Error(err))
return errgo.Wrap(err, "dal")

Check warning on line 557 in internal/collections/infra/mysql_repo.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L556-L557

Added lines #L556 - L557 were not covered by tests
Expand Down
41 changes: 32 additions & 9 deletions internal/collections/infra/mysql_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,12 @@ func TestMysqlRepo_GetPersonCollect(t *testing.T) {
const mid model.PersonID = 12000

repo, q := getRepo(t)
table := q.PersonCollect
test.RunAndCleanup(t, func() {
_, err := table.WithContext(context.TODO()).Where(table.UserID.Eq(uid)).Delete()
_, err := q.PersonCollect.WithContext(context.TODO()).Where(q.PersonCollect.UserID.Eq(uid)).Delete()
require.NoError(t, err)
})

err := table.WithContext(context.Background()).Create(&dao.PersonCollect{
err := q.PersonCollect.WithContext(context.Background()).Create(&dao.PersonCollect{
UserID: uid,
Category: cat,
TargetID: mid,
Expand All @@ -547,20 +546,33 @@ func TestMysqlRepo_AddPersonCollect(t *testing.T) {
const uid model.UserID = 40000
const cat = "prsn"
const mid model.PersonID = 13000
const collects uint32 = 10

repo, q := getRepo(t)
table := q.PersonCollect
test.RunAndCleanup(t, func() {
_, err := table.WithContext(context.TODO()).Where(table.UserID.Eq(uid)).Delete()
require.NoError(t, err)
_, err = q.Person.WithContext(context.TODO()).Where(q.Person.ID.Eq(mid)).Delete()
require.NoError(t, err)
})

err := q.Person.WithContext(context.Background()).Create(&dao.Person{
ID: mid,
Collects: collects,
})
require.NoError(t, err)

err := repo.AddPersonCollection(context.Background(), uid, cat, mid)
err = repo.AddPersonCollection(context.Background(), uid, cat, mid)
require.NoError(t, err)

r, err := table.WithContext(context.TODO()).Where(table.UserID.Eq(uid)).Take()
require.NoError(t, err)
require.NotZero(t, r.ID)

p, err := q.Person.WithContext(context.Background()).Where(q.Person.ID.Eq(mid)).Take()
require.NoError(t, err)
require.Equal(t, collects+1, p.Collects)
}

func TestMysqlRepo_RemovePersonCollect(t *testing.T) {
Expand All @@ -570,31 +582,42 @@ func TestMysqlRepo_RemovePersonCollect(t *testing.T) {
const uid model.UserID = 41000
const cat = "prsn"
const mid model.PersonID = 14000
const collects uint32 = 10

repo, q := getRepo(t)
table := q.PersonCollect
test.RunAndCleanup(t, func() {
_, err := table.WithContext(context.TODO()).Where(table.UserID.Eq(uid)).Delete()
_, err := q.PersonCollect.WithContext(context.TODO()).Where(q.PersonCollect.UserID.Eq(uid)).Delete()
require.NoError(t, err)
_, err = q.Person.WithContext(context.TODO()).Where(q.Person.ID.Eq(mid)).Delete()
require.NoError(t, err)
})

err := table.WithContext(context.Background()).Create(&dao.PersonCollect{
err := q.Person.WithContext(context.Background()).Create(&dao.Person{
ID: mid,
Collects: collects,
})
require.NoError(t, err)
err = q.PersonCollect.WithContext(context.Background()).Create(&dao.PersonCollect{
UserID: uid,
Category: cat,
TargetID: mid,
CreatedTime: uint32(time.Now().Unix()),
})
require.NoError(t, err)

r, err := table.WithContext(context.TODO()).Where(table.UserID.Eq(uid)).Take()
r, err := q.PersonCollect.WithContext(context.TODO()).Where(q.PersonCollect.UserID.Eq(uid)).Take()
require.NoError(t, err)
require.NotZero(t, r.ID)

err = repo.RemovePersonCollection(context.Background(), uid, cat, mid)
require.NoError(t, err)

_, err = table.WithContext(context.TODO()).Where(table.UserID.Eq(uid)).Take()
_, err = q.PersonCollect.WithContext(context.TODO()).Where(q.PersonCollect.UserID.Eq(uid)).Take()
require.ErrorIs(t, err, gorm.ErrRecordNotFound)

p, err := q.Person.WithContext(context.Background()).Where(q.Person.ID.Eq(mid)).Take()
require.NoError(t, err)
require.Equal(t, collects-1, p.Collects)
}

func TestMysqlRepo_CountPersonCollections(t *testing.T) {
Expand Down

0 comments on commit d7d1c2d

Please sign in to comment.