对接
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
kratoserrors "github.com/go-kratos/kratos/v3/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrFollowNotFound = kratoserrors.NotFound("LIVE_FOLLOW_NOT_FOUND", "follow relation not found")
|
||||
ErrFollowInvalid = kratoserrors.BadRequest("LIVE_FOLLOW_INVALID_ARGUMENT", "invalid follow argument")
|
||||
)
|
||||
|
||||
type Follow struct {
|
||||
ID uint
|
||||
UserID uint
|
||||
RoomID uint
|
||||
AnchorUserID uint
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type FollowRepo interface {
|
||||
Get(context.Context, uint, uint) (*Follow, error)
|
||||
Create(context.Context, *Follow) (*Follow, error)
|
||||
Delete(context.Context, uint, uint) error
|
||||
}
|
||||
|
||||
type FollowUsecase struct{ repo FollowRepo }
|
||||
|
||||
func NewFollowUsecase(repo FollowRepo) *FollowUsecase { return &FollowUsecase{repo: repo} }
|
||||
|
||||
func (uc *FollowUsecase) Get(ctx context.Context, userID, roomID uint) (*Follow, error) {
|
||||
if userID == 0 || roomID == 0 {
|
||||
return nil, ErrFollowInvalid
|
||||
}
|
||||
return uc.repo.Get(ctx, userID, roomID)
|
||||
}
|
||||
|
||||
func (uc *FollowUsecase) Follow(ctx context.Context, userID, roomID uint) (*Follow, error) {
|
||||
if userID == 0 || roomID == 0 {
|
||||
return nil, ErrFollowInvalid
|
||||
}
|
||||
if existing, err := uc.repo.Get(ctx, userID, roomID); err == nil {
|
||||
return existing, nil
|
||||
} else if !errors.Is(err, ErrFollowNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
return uc.repo.Create(ctx, &Follow{UserID: userID, RoomID: roomID})
|
||||
}
|
||||
|
||||
func (uc *FollowUsecase) Unfollow(ctx context.Context, userID, roomID uint) error {
|
||||
if userID == 0 || roomID == 0 {
|
||||
return ErrFollowInvalid
|
||||
}
|
||||
return uc.repo.Delete(ctx, userID, roomID)
|
||||
}
|
||||
Reference in New Issue
Block a user