tsk-19: Update status of scheduled message endpoint (#32)
Closes #19 Reviewed-on: phoenix/textsender-api#32 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -50,6 +50,33 @@ func (m *MockScheduledMessageEventStore) Get(ctx context.Context, id uuid.UUID)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageEventStore) GetWithScheduleMessageId(ctx context.Context, schMsgId uuid.UUID) ([]*scheduling.ScheduledMessageEvent, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
if schMsgId == uuid.Nil {
|
||||
return nil, fmt.Errorf("Scheduled message Id is nil")
|
||||
}
|
||||
|
||||
var events []*scheduling.ScheduledMessageEvent
|
||||
|
||||
for _, event := range m.ScheduledMessageEvents {
|
||||
if event.ScheduledMessageId == schMsgId {
|
||||
events = append(events, event)
|
||||
}
|
||||
}
|
||||
|
||||
if len(events) > 0 {
|
||||
return events, nil
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageEventStore) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -65,3 +65,28 @@ func (m *MockScheduledMessageStore) CreateScheduledMessage(ctx context.Context,
|
||||
m.ScheduledMessagesByKey[key] = schedMsg
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageStore) UpdateStatus(ctx context.Context, id uuid.UUID, updatedStatus string) (*string, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
if id == uuid.Nil {
|
||||
return nil, fmt.Errorf("Id is nil")
|
||||
} else {
|
||||
if schMsg := m.ScheduledMessages[id]; schMsg != nil {
|
||||
key := ScheduledMessageKey{UserId: schMsg.UserId}
|
||||
|
||||
schMsg.Status = updatedStatus
|
||||
m.ScheduledMessages[schMsg.Id] = schMsg
|
||||
m.ScheduledMessagesByKey[key] = schMsg
|
||||
|
||||
return &updatedStatus, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("Scheduled message does not exist")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
type ScheduledMessageEventStore interface {
|
||||
Get(ctx context.Context, id uuid.UUID) (*scheduling.ScheduledMessageEvent, error)
|
||||
GetWithScheduleMessageId(ctx context.Context, schMsgId uuid.UUID) ([]*scheduling.ScheduledMessageEvent, error)
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
CreateScheduledMessageEvent(ctx context.Context, event *scheduling.ScheduledMessageEvent) error
|
||||
Exists(ctx context.Context, event *scheduling.ScheduledMessageEvent) (bool, error)
|
||||
@@ -43,6 +44,34 @@ func (s *PGScheduledMessageEventStore) Get(ctx context.Context, id uuid.UUID) (*
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PGScheduledMessageEventStore) GetWithScheduleMessageId(ctx context.Context, schMsgId uuid.UUID) ([]*scheduling.ScheduledMessageEvent, error) {
|
||||
var events []*scheduling.ScheduledMessageEvent
|
||||
query := `SELECT id, recipient_id, message_id, scheduled_message_id, created FROM scheduled_message_events WHERE scheduled_message_id = $1
|
||||
`
|
||||
|
||||
rows, err := s.db.Query(ctx, query, schMsgId)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error retrieving rows from scheduled_message_Events: %w", err)
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var event scheduling.ScheduledMessageEvent
|
||||
|
||||
if err := rows.Scan(&event.Id, &event.RecipientId, &event.MessageId, &event.ScheduledMessageId, &event.Created); err != nil {
|
||||
return nil, fmt.Errorf("Error fetching data from row: %w", err)
|
||||
} else {
|
||||
events = append(events, &event)
|
||||
}
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *PGScheduledMessageEventStore) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
query := `
|
||||
DELETE FROM scheduled_message_events WHERE id = $1
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
type ScheduledMessageStore interface {
|
||||
Get(ctx context.Context, id uuid.UUID) (*scheduling.ScheduledMessage, error)
|
||||
CreateScheduledMessage(ctx context.Context, schedMsg *scheduling.ScheduledMessage) error
|
||||
UpdateStatus(ctx context.Context, id uuid.UUID, updatedStatus string) (*string, error)
|
||||
}
|
||||
|
||||
type PGScheduledMessageStore struct {
|
||||
@@ -54,3 +55,18 @@ func (s *PGScheduledMessageStore) CreateScheduledMessage(ctx context.Context, sc
|
||||
&schedMsg.Id, &schedMsg.Created,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *PGScheduledMessageStore) UpdateStatus(ctx context.Context, id uuid.UUID, updatedStatus string) (*string, error) {
|
||||
query := `
|
||||
UPDATE scheduled_messages set status = $1 WHERE id = $2
|
||||
RETURNING status
|
||||
`
|
||||
|
||||
var returnedStatus string
|
||||
|
||||
if err := s.db.QueryRow(ctx, query, updatedStatus, id).Scan(&returnedStatus); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &returnedStatus, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user