Skip to content

Commit

Permalink
feat: add get & list person collect
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed May 18, 2024
1 parent 91b3894 commit c0cc25b
Show file tree
Hide file tree
Showing 13 changed files with 725 additions and 89 deletions.
19 changes: 16 additions & 3 deletions internal/collections/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,33 @@ type Repo interface { //nolint:interfacebloat
at time.Time,
) (collection.UserSubjectEpisodesCollection, error)

GetPersonCollect(
GetPersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) (collection.UserPersonCollection, error)

AddPersonCollect(
AddPersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) error

RemovePersonCollect(
RemovePersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) error

CountPersonCollections(
ctx context.Context,
userID model.UserID,
cat collection.PersonCollectCategory,
) (int64, error)

ListPersonCollection(
ctx context.Context,
userID model.UserID,
cat collection.PersonCollectCategory,
limit, offset int,
) ([]collection.UserPersonCollection, error)
}

type Update struct {
Expand Down
52 changes: 49 additions & 3 deletions internal/collections/infra/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (r mysqlRepo) updateCollectionTime(obj *dao.SubjectCollection,
return nil
}

func (r mysqlRepo) GetPersonCollect(
func (r mysqlRepo) GetPersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) (collection.UserPersonCollection, error) {
Expand All @@ -484,7 +484,7 @@ func (r mysqlRepo) GetPersonCollect(
}, nil
}

func (r mysqlRepo) AddPersonCollect(
func (r mysqlRepo) AddPersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) error {
Expand All @@ -503,7 +503,7 @@ func (r mysqlRepo) AddPersonCollect(
return nil
}

func (r mysqlRepo) RemovePersonCollect(
func (r mysqlRepo) RemovePersonCollection(
ctx context.Context, userID model.UserID,
cat collection.PersonCollectCategory, targetID model.PersonID,
) error {
Expand All @@ -518,6 +518,52 @@ func (r mysqlRepo) RemovePersonCollect(
return nil
}

func (r mysqlRepo) CountPersonCollections(
ctx context.Context,
userID model.UserID,
cat collection.PersonCollectCategory,
) (int64, error) {
q := r.q.PersonCollect.WithContext(ctx).
Where(r.q.PersonCollect.UserID.Eq(userID), r.q.PersonCollect.Category.Eq(string(cat)))

c, err := q.Count()
if err != nil {
return 0, errgo.Wrap(err, "dal")

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

View check run for this annotation

Codecov / codecov/patch

internal/collections/infra/mysql_repo.go#L531

Added line #L531 was not covered by tests
}

return c, nil
}

func (r mysqlRepo) ListPersonCollection(
ctx context.Context,
userID model.UserID,
cat collection.PersonCollectCategory,
limit, offset int,
) ([]collection.UserPersonCollection, error) {
q := r.q.PersonCollect.WithContext(ctx).
Order(r.q.PersonCollect.CreatedTime.Desc()).
Where(r.q.PersonCollect.UserID.Eq(userID), r.q.PersonCollect.Category.Eq(string(cat))).Limit(limit).Offset(offset)

collections, err := q.Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")

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
}

var results = make([]collection.UserPersonCollection, len(collections))
for i, c := range collections {
results[i] = collection.UserPersonCollection{
ID: c.ID,
Category: c.Category,
TargetID: c.TargetID,
UserID: c.UserID,
CreatedAt: time.Unix(int64(c.CreatedTime), 0),
}
}

return results, nil
}

func (r mysqlRepo) UpdateEpisodeCollection(
ctx context.Context,
userID model.UserID,
Expand Down
90 changes: 87 additions & 3 deletions internal/collections/infra/mysql_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func TestMysqlRepo_GetPersonCollect(t *testing.T) {
})
require.NoError(t, err)

r, err := repo.GetPersonCollect(context.Background(), uid, cat, mid)
r, err := repo.GetPersonCollection(context.Background(), uid, cat, mid)
require.NoError(t, err)
require.Equal(t, uid, r.UserID)
require.Equal(t, mid, r.TargetID)
Expand All @@ -555,7 +555,7 @@ func TestMysqlRepo_AddPersonCollect(t *testing.T) {
require.NoError(t, err)
})

err := repo.AddPersonCollect(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()
Expand Down Expand Up @@ -590,9 +590,93 @@ func TestMysqlRepo_RemovePersonCollect(t *testing.T) {
require.NoError(t, err)
require.NotZero(t, r.ID)

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

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

func TestMysqlRepo_CountPersonCollections(t *testing.T) {
t.Parallel()
test.RequireEnv(t, test.EnvMysql)

const uid model.UserID = 41000
const cat = "prsn"

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

for i := 0; i < 5; i++ {
err := q.PersonCollect.
WithContext(context.Background()).
Create(&dao.PersonCollect{
UserID: uid,
TargetID: model.PersonID(i + 100),
Category: cat,
CreatedTime: uint32(time.Now().Unix()),
})
require.NoError(t, err)
}

count, err := repo.CountPersonCollections(context.Background(), uid, cat)
require.NoError(t, err)
require.EqualValues(t, 5, count)
}

func TestMysqlRepo_ListPersonCollection(t *testing.T) {
t.Parallel()
test.RequireEnv(t, test.EnvMysql)

const uid model.UserID = 42000
const cat = "prsn"

repo, q := getRepo(t)

var err error
test.RunAndCleanup(t, func() {
_, err = q.PersonCollect.
WithContext(context.Background()).
Where(q.PersonCollect.UserID.Eq(uid)).
Delete()
require.NoError(t, err)
})

data, err := repo.ListPersonCollection(context.Background(), uid, collection.PersonCollectCategory(cat), 5, 0)
require.NoError(t, err)
require.Len(t, data, 0)

for i := 0; i < 5; i++ {
err = q.PersonCollect.
WithContext(context.Background()).
Create(&dao.PersonCollect{
UserID: uid,
TargetID: model.PersonID(i + 100),
Category: cat,
CreatedTime: uint32(time.Now().Unix()),
})
require.NoError(t, err)
}

for i := 0; i < 2; i++ {
err = q.PersonCollect.
WithContext(context.Background()).
Create(&dao.PersonCollect{
UserID: uid,
TargetID: model.PersonID(i + 200),
Category: cat,
CreatedTime: uint32(time.Now().Unix()),
})
require.NoError(t, err)
}

data, err = repo.ListPersonCollection(context.Background(), uid, collection.PersonCollectCategory(cat), 5, 0)
require.NoError(t, err)
require.Len(t, data, 5)
}
Loading

0 comments on commit c0cc25b

Please sign in to comment.