initial commit
This commit is contained in:
124
data/storage/mock.go
Normal file
124
data/storage/mock.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/saved-search/data/types"
|
||||
)
|
||||
|
||||
// MockStorage implements Storage interface for testing
|
||||
type MockStorage struct {
|
||||
mu sync.RWMutex
|
||||
data map[string]*types.SavedSearch
|
||||
counter int
|
||||
}
|
||||
|
||||
// NewMockStorage creates a new mock storage instance
|
||||
func NewMockStorage() *MockStorage {
|
||||
return &MockStorage{
|
||||
data: make(map[string]*types.SavedSearch),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockStorage) CreateSavedSearch(ctx context.Context, search types.SavedSearch) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
search.CreatedAt = now
|
||||
search.UpdatedAt = now
|
||||
|
||||
m.data[search.ID] = &search
|
||||
m.counter++
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockStorage) GetSavedSearch(ctx context.Context, id string) (*types.SavedSearch, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
search, exists := m.data[id]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("saved search not found")
|
||||
}
|
||||
|
||||
// Return a copy to avoid mutation issues
|
||||
searchCopy := *search
|
||||
return &searchCopy, nil
|
||||
}
|
||||
|
||||
func (m *MockStorage) GetSavedSearchesByOwner(ctx context.Context, ownerID string) ([]*types.SavedSearch, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
var searches []*types.SavedSearch
|
||||
for _, search := range m.data {
|
||||
if search.OwnerID == ownerID {
|
||||
searchCopy := *search
|
||||
searches = append(searches, &searchCopy)
|
||||
}
|
||||
}
|
||||
|
||||
return searches, nil
|
||||
}
|
||||
|
||||
func (m *MockStorage) UpdateSavedSearch(ctx context.Context, search types.SavedSearch) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
existing, exists := m.data[search.ID]
|
||||
if !exists {
|
||||
return fmt.Errorf("saved search not found")
|
||||
}
|
||||
|
||||
search.CreatedAt = existing.CreatedAt // Preserve creation time
|
||||
search.UpdatedAt = time.Now()
|
||||
m.data[search.ID] = &search
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockStorage) DeleteSavedSearch(ctx context.Context, id string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if _, exists := m.data[id]; !exists {
|
||||
return fmt.Errorf("saved search not found")
|
||||
}
|
||||
|
||||
delete(m.data, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockStorage) ListSavedSearches(ctx context.Context, ownerID string, limit, offset int) ([]*types.SavedSearch, int64, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
var allSearches []*types.SavedSearch
|
||||
for _, search := range m.data {
|
||||
if ownerID == "" || search.OwnerID == ownerID {
|
||||
searchCopy := *search
|
||||
allSearches = append(allSearches, &searchCopy)
|
||||
}
|
||||
}
|
||||
|
||||
total := int64(len(allSearches))
|
||||
|
||||
// Apply pagination
|
||||
start := offset
|
||||
end := offset + limit
|
||||
|
||||
if start > len(allSearches) {
|
||||
return []*types.SavedSearch{}, total, nil
|
||||
}
|
||||
|
||||
if end > len(allSearches) {
|
||||
end = len(allSearches)
|
||||
}
|
||||
|
||||
return allSearches[start:end], total, nil
|
||||
}
|
||||
Reference in New Issue
Block a user