42 lines
1017 B
Go
42 lines
1017 B
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"hyapp/pkg/xerr"
|
|
activitydomain "hyapp/services/activity-service/internal/domain/activity"
|
|
)
|
|
|
|
// Repository 是 activity-service 的测试用内存存储。
|
|
type Repository struct {
|
|
mu sync.RWMutex
|
|
activities map[string]activitydomain.Activity
|
|
}
|
|
|
|
// NewRepository 创建空活动仓库。
|
|
func NewRepository() *Repository {
|
|
return &Repository{activities: make(map[string]activitydomain.Activity)}
|
|
}
|
|
|
|
// PutActivity 写入活动状态,测试用它准备数据。
|
|
func (r *Repository) PutActivity(activity activitydomain.Activity) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
|
|
r.activities[activity.ActivityID] = activity
|
|
}
|
|
|
|
// GetActivity 查询活动状态。
|
|
func (r *Repository) GetActivity(_ context.Context, activityID string) (activitydomain.Activity, error) {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
activity, ok := r.activities[activityID]
|
|
if !ok {
|
|
return activitydomain.Activity{}, xerr.New(xerr.NotFound, "activity not found")
|
|
}
|
|
|
|
return activity, nil
|
|
}
|