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 a6a23ac
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
56 changes: 48 additions & 8 deletions internal/collections/infra/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,30 +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:
if _, err := tx.Character.WithContext(ctx).Where(
tx.Character.ID.Eq(targetID)).UpdateSimple(tx.Character.Collects.Add(1)); err != nil {
r.log.Error("failed to update character collects", 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
}
case collection.PersonCollectCategoryPerson:
if _, err := tx.Person.WithContext(ctx).Where(
tx.Person.ID.Eq(targetID)).UpdateSimple(tx.Person.Collects.Add(1)); err != nil {
r.log.Error("failed to update person collects", zap.Error(err))
return err

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L508-L509

Added lines #L508 - L509 were not covered by tests
}
}
if err := tx.PersonCollect.WithContext(ctx).Create(collect); err != nil {
r.log.Error("failed to create person collection record", zap.Error(err))
return err

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L513-L514

Added lines #L513 - L514 were not covered by tests
}
return nil
})
if err != nil {
r.log.Error("failed to create person collection record", zap.Error(err))
return errgo.Wrap(err, "dal")

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L519

Added line #L519 was 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:
if _, err := tx.Character.WithContext(ctx).Where(
tx.Character.ID.Eq(targetID)).UpdateSimple(tx.Character.Collects.Sub(1)); err != nil {
r.log.Error("failed to update character collects", zap.Error(err))
return err

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L530-L534

Added lines #L530 - L534 were not covered by tests
}
case collection.PersonCollectCategoryPerson:
if _, err := tx.Person.WithContext(ctx).Where(
tx.Person.ID.Eq(targetID)).UpdateSimple(tx.Person.Collects.Sub(1)); err != nil {
r.log.Error("failed to update person collects", zap.Error(err))
return err

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L539-L540

Added lines #L539 - L540 were not covered by tests
}
}
_, err := tx.PersonCollect.WithContext(ctx).Where(
tx.PersonCollect.UserID.Eq(userID),
tx.PersonCollect.Category.Eq(string(cat)),
tx.PersonCollect.TargetID.Eq(targetID),
).Delete()
if err != nil {
r.log.Error("failed to delete person collection record", zap.Error(err))
return err

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L549-L550

Added lines #L549 - L550 were not covered by tests
}
return nil
})
if err != nil {
r.log.Error("failed to delete person collection record", zap.Error(err))
return errgo.Wrap(err, "dal")

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L555

Added line #L555 was 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 a6a23ac

Please sign in to comment.