tsk-8: Create Message endpoint (#15)

Closes #8

Reviewed-on: phoenix/textsender-api#15
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-11-05 19:37:49 +00:00
committed by phoenix
parent 2045465111
commit 507eb9eb09
9 changed files with 230 additions and 3 deletions
+55
View File
@@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
"git.kundeng.us/phoenix/textsender-models/pkg/contact"
"git.kundeng.us/phoenix/textsender-models/pkg/message"
)
type Key struct {
@@ -115,3 +116,57 @@ func (m *MockContactStore) ContactExists(ctx context.Context, phoneNumber string
return exists, nil
}
type MessageKey struct {
Content string
UserId uuid.UUID
}
type MockMessageStore struct {
Messages map[uuid.UUID]*message.Message
MessagesByKey map[MessageKey]*message.Message
mu sync.RWMutex
Error error // Optional: simulate errors
}
func NewMockMessageStore() *MockMessageStore {
return &MockMessageStore{
Messages: make(map[uuid.UUID]*message.Message),
MessagesByKey: make(map[MessageKey]*message.Message),
}
}
func (m *MockMessageStore) CreateMessage(ctx context.Context, msg *message.Message) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.Error != nil {
return m.Error
}
if msg.Id == uuid.Nil {
msg.Id = uuid.New()
}
key := MessageKey{Content: msg.Content, UserId: msg.UserId}
if _, exists := m.MessagesByKey[key]; exists {
return errors.New("Message already exists")
}
m.Messages[msg.Id] = msg
m.MessagesByKey[key] = msg
return nil
}
func (m *MockMessageStore) MessageExists(ctx context.Context, msg *message.Message) (bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.Error != nil {
return false, m.Error
}
_, exists := m.MessagesByKey[MessageKey{Content: msg.Content, UserId: msg.UserId}]
return exists, nil
}