Skip to content

Commit

Permalink
feat: add collect & uncollect person
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed May 17, 2024
1 parent 09fb430 commit 13c869f
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 34 deletions.
15 changes: 15 additions & 0 deletions internal/collections/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ type Repo interface {
episodeIDs []model.EpisodeID, collection collection.EpisodeCollection,
at time.Time,
) (collection.UserSubjectEpisodesCollection, error)

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

AddPersonCollect(
ctx context.Context, userID model.UserID,
cat string, targetID model.PersonID,
) error

RemovePersonCollect(
ctx context.Context, userID model.UserID,
cat string, targetID model.PersonID,
) error
}

type Update struct {
Expand Down
8 changes: 8 additions & 0 deletions internal/collections/domain/collection/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ type UserEpisodeCollection struct {
}

type UserSubjectEpisodesCollection map[model.EpisodeID]UserEpisodeCollection

type UserPersonCollection struct {
ID uint32
Category string
TargetID model.PersonID
UserID model.UserID
CreatedAt time.Time
}
57 changes: 57 additions & 0 deletions internal/collections/infra/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,63 @@ func (r mysqlRepo) updateCollectionTime(obj *dao.SubjectCollection,
return nil
}

func (r mysqlRepo) GetPersonCollect(
ctx context.Context, userID model.UserID,
cat string, targetID model.PersonID,
) (collection.UserPersonCollection, error) {
c, err := r.q.PersonCollect.WithContext(ctx).
Where(r.q.PersonCollect.UserID.Eq(userID), r.q.PersonCollect.Category.Eq(cat),
r.q.PersonCollect.TargetID.Eq(targetID)).Take()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return collection.UserPersonCollection{}, gerr.ErrNotFound
}
return collection.UserPersonCollection{}, errgo.Wrap(err, "dal")
}

return collection.UserPersonCollection{
ID: c.ID,
Category: c.Category,
TargetID: c.TargetID,
UserID: c.UserID,
CreatedAt: time.Unix(int64(c.CreatedTime), 0),
}, nil
}

func (r mysqlRepo) AddPersonCollect(
ctx context.Context, userID model.UserID,
cat string, targetID model.PersonID,
) error {
var table = r.q.PersonCollect
err := table.WithContext(ctx).Create(&dao.PersonCollect{
UserID: userID,
Category: cat,
TargetID: targetID,
CreatedTime: uint32(time.Now().Unix()),
})
if err != nil {
r.log.Error("failed to create person collection record", zap.Error(err))
return errgo.Wrap(err, "dal")
}

return nil
}

func (r mysqlRepo) RemovePersonCollect(
ctx context.Context, userID model.UserID,
cat string, targetID model.PersonID,
) error {
_, err := r.q.PersonCollect.WithContext(ctx).
Where(r.q.PersonCollect.UserID.Eq(userID), r.q.PersonCollect.Category.Eq(cat),
r.q.PersonCollect.TargetID.Eq(targetID)).Delete()
if err != nil {
r.log.Error("failed to delete person collection record", zap.Error(err))
return errgo.Wrap(err, "dal")
}

return nil
}

func (r mysqlRepo) UpdateEpisodeCollection(
ctx context.Context,
userID model.UserID,
Expand Down
87 changes: 87 additions & 0 deletions internal/collections/infra/mysql_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,93 @@ func TestMysqlRepo_UpdateSubjectCollectionType(t *testing.T) {
require.Zero(t, r.DoneTime)
require.Zero(t, r.OnHoldTime)
}

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

const uid model.UserID = 35000
const cat = "prsn"
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()
require.NoError(t, err)
})

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

r, err := repo.GetPersonCollect(context.Background(), uid, cat, mid)
require.NoError(t, err)
require.Len(t, r, 1)
}

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

const uid model.UserID = 36000
const cat = "prsn"
const mid model.PersonID = 13000

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 := repo.AddPersonCollect(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)
}

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

const uid model.UserID = 37000
const cat = "prsn"
const mid model.PersonID = 14000

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 := table.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()
require.NoError(t, err)
require.NotZero(t, r.ID)

err = repo.RemovePersonCollect(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.Zero(t, r.ID)
}

func TestMysqlRepo_UpdateEpisodeCollection(t *testing.T) {
test.RequireEnv(t, test.EnvMysql)
t.Parallel()
Expand Down
157 changes: 157 additions & 0 deletions internal/mocks/CollectionRepo.go

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

Loading

0 comments on commit 13c869f

Please sign in to comment.