package model import ( "context" "sync" "spark-server/internal/module/message/entity" "spark-server/internal/pkg/errs" ) // MemoryRepo is an in-process message store for tests and empty-DSN bring-up. type MemoryRepo struct { mu sync.Mutex byID map[int64]*entity.Message } func NewMemoryRepo() *MemoryRepo { return &MemoryRepo{byID: make(map[int64]*entity.Message)} } func (m *MemoryRepo) Create(_ context.Context, message *entity.Message) error { if message == nil || message.ID <= 0 { return errs.ErrInvalidArgument } m.mu.Lock() defer m.mu.Unlock() if _, ok := m.byID[message.ID]; ok { return errs.ErrDuplicate } cp := *message if cp.Content != nil { cp.Content = append([]byte(nil), message.Content...) } m.byID[message.ID] = &cp return nil } func (m *MemoryRepo) GetByID(_ context.Context, id int64) (*entity.Message, error) { m.mu.Lock() defer m.mu.Unlock() row, ok := m.byID[id] if !ok { return nil, errs.ErrNotFound } cp := *row if cp.Content != nil { cp.Content = append([]byte(nil), row.Content...) } return &cp, nil } func (m *MemoryRepo) All() []*entity.Message { m.mu.Lock() defer m.mu.Unlock() out := make([]*entity.Message, 0, len(m.byID)) for _, row := range m.byID { cp := *row if cp.Content != nil { cp.Content = append([]byte(nil), row.Content...) } out = append(out, &cp) } return out }