diff --git a/internal/store/mock/contact_store.go b/internal/store/mock/contact_store.go new file mode 100644 index 0000000..f99b1b0 --- /dev/null +++ b/internal/store/mock/contact_store.go @@ -0,0 +1,118 @@ +package mock + +import ( + "context" + "errors" + "sync" + + "git.kundeng.us/phoenix/textsender-models/tx0/contact" + "github.com/google/uuid" +) + +type Key struct { + PhoneNumber string + UserId uuid.UUID +} + +type MockContactStore struct { + Contacts map[uuid.UUID]*contact.Contact + ContactsByKey map[Key]*contact.Contact + mu sync.RWMutex + Error error // Optional: simulate errors +} + +func NewMockContactStore() *MockContactStore { + return &MockContactStore{ + Contacts: make(map[uuid.UUID]*contact.Contact), + ContactsByKey: make(map[Key]*contact.Contact), + } +} + +func (m *MockContactStore) CreateContact(ctx context.Context, con *contact.Contact) error { + m.mu.Lock() + defer m.mu.Unlock() + + if m.Error != nil { + return m.Error + } + + var id uuid.UUID + if con.Id == nil { + id = uuid.New() + con.Id = &id + } + + key := Key{PhoneNumber: con.PhoneNumber, UserId: *con.UserId} + if _, exists := m.ContactsByKey[key]; exists { + return errors.New("Contact with phone number already exists") + } + + m.Contacts[*con.Id] = con + m.ContactsByKey[key] = con + return nil +} + +func (m *MockContactStore) GetContactByID(ctx context.Context, id uuid.UUID) (*contact.Contact, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if m.Error != nil { + return nil, m.Error + } + + if m.Error != nil { + return nil, m.Error + } + + con, exists := m.Contacts[id] + if !exists { + return nil, errors.New("Contact not found") + } + + return con, nil +} + +func (m *MockContactStore) GetContactByPhone(ctx context.Context, phoneNumber string, userId uuid.UUID) (*contact.Contact, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if m.Error != nil { + return nil, m.Error + } + + con, exists := m.ContactsByKey[Key{PhoneNumber: phoneNumber, UserId: userId}] + if !exists { + return nil, errors.New("Contact not found") + } + + return con, nil +} + +func (m *MockContactStore) GetAllContacts(ctx context.Context) ([]*contact.Contact, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if m.Error != nil { + return nil, m.Error + } + + cons := make([]*contact.Contact, 0, len(m.Contacts)) + for _, con := range m.Contacts { + cons = append(cons, con) + } + + return cons, nil +} + +func (m *MockContactStore) ContactExists(ctx context.Context, phoneNumber string, userId uuid.UUID) (bool, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if m.Error != nil { + return false, m.Error + } + + _, exists := m.ContactsByKey[Key{PhoneNumber: phoneNumber, UserId: userId}] + + return exists, nil +}