tsk-13: Retrieve scheduled message endpoint (#36)
Closes #13 Reviewed-on: phoenix/textsender-api#36 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -47,6 +47,32 @@ func (m *MockScheduledMessageStore) Get(ctx context.Context, id uuid.UUID) (*sch
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageStore) GetWithUserId(ctx context.Context, userId uuid.UUID) ([]*scheduling.ScheduledMessage, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
if userId == uuid.Nil {
|
||||
return nil, fmt.Errorf("User Id is nil")
|
||||
} else {
|
||||
var schMsgs []*scheduling.ScheduledMessage
|
||||
for _, schMsg := range m.ScheduledMessages {
|
||||
if schMsg.UserId == userId {
|
||||
schMsgs = append(schMsgs, schMsg)
|
||||
}
|
||||
}
|
||||
|
||||
if len(schMsgs) == 0 {
|
||||
return nil, nil
|
||||
} else {
|
||||
return schMsgs, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageStore) CreateScheduledMessage(ctx context.Context, schedMsg *scheduling.ScheduledMessage) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
type ScheduledMessageStore interface {
|
||||
Get(ctx context.Context, id uuid.UUID) (*scheduling.ScheduledMessage, error)
|
||||
GetWithUserId(ctx context.Context, userId uuid.UUID) ([]*scheduling.ScheduledMessage, error)
|
||||
CreateScheduledMessage(ctx context.Context, schedMsg *scheduling.ScheduledMessage) error
|
||||
UpdateStatus(ctx context.Context, id uuid.UUID, updatedStatus string) (*string, error)
|
||||
}
|
||||
@@ -44,6 +45,33 @@ func (s *PGScheduledMessageStore) Get(ctx context.Context, id uuid.UUID) (*sched
|
||||
return &schMsg, nil
|
||||
}
|
||||
|
||||
func (s *PGScheduledMessageStore) GetWithUserId(ctx context.Context, userId uuid.UUID) ([]*scheduling.ScheduledMessage, error) {
|
||||
query := `
|
||||
SELECT id, scheduled, created, status, user_id FROM scheduled_messages WHERE user_id = $1
|
||||
`
|
||||
rows, err := s.db.Query(ctx, query, userId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error retrieving rows from scheduled_messages: %w", err)
|
||||
}
|
||||
|
||||
var schMsgs []*scheduling.ScheduledMessage
|
||||
|
||||
for rows.Next() {
|
||||
var schMsg scheduling.ScheduledMessage
|
||||
if err := rows.Scan(&schMsg.Id, &schMsg.Scheduled, &schMsg.Created, &schMsg.Status, &schMsg.UserId); err != nil {
|
||||
return nil, fmt.Errorf("Error fetching data from row: %w", err)
|
||||
} else {
|
||||
schMsgs = append(schMsgs, &schMsg)
|
||||
}
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return schMsgs, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PGScheduledMessageStore) CreateScheduledMessage(ctx context.Context, schedMsg *scheduling.ScheduledMessage) error {
|
||||
query := `
|
||||
INSERT INTO scheduled_messages (scheduled, status, user_id)
|
||||
|
||||
Reference in New Issue
Block a user