Files
schedtxt_api/internal/store/mock/contact_store.go
T
phoenixandphoenix a51979e724 tsk-58: Update names of Contact endpoint (#67)
Closes #58

Reviewed-on: phoenix/textsender-api#67
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2026-01-07 20:24:30 +00:00

147 lines
3.0 KiB
Go

package mock
import (
"context"
"errors"
"fmt"
"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
}
func (m *MockContactStore) UpdateNames(ctx context.Context, con *contact.Contact, firstname *string, lastname *string, nickname *string) (int64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.Error != nil {
return 0, m.Error
} else if firstname == nil && lastname == nil && nickname == nil {
return 0, fmt.Errorf("Names are not provided")
} else {
if c, exists := m.Contacts[*con.Id]; !exists {
return 0, fmt.Errorf("Contact does not exist")
} else {
if firstname != nil {
c.Firstname = firstname
}
if lastname != nil {
c.Lastname = lastname
}
if nickname != nil {
c.Nickname = nickname
}
}
}
return 0, nil
}