|
| 1 | +package episode |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hardhacker/podwise-cli/internal/api" |
| 8 | +) |
| 9 | + |
| 10 | +// MarkResponse represents the API response for mark read/unread operations. |
| 11 | +type MarkResponse struct { |
| 12 | + Success bool `json:"success"` |
| 13 | +} |
| 14 | + |
| 15 | +// MarkAsRead marks an episode as read for the authenticated user. |
| 16 | +// The operation is idempotent - if the episode is already marked as read, |
| 17 | +// the request succeeds silently. |
| 18 | +// |
| 19 | +// Returns an error if: |
| 20 | +// - The episode does not exist (404 not_found) |
| 21 | +// - The API request fails for other reasons |
| 22 | +func MarkAsRead(ctx context.Context, client *api.Client, seq int) error { |
| 23 | + path := fmt.Sprintf("/open/v1/episodes/%d/read", seq) |
| 24 | + |
| 25 | + var resp MarkResponse |
| 26 | + if err := client.Post(ctx, path, nil, &resp); err != nil { |
| 27 | + return fmt.Errorf("mark episode %d as read: %w", seq, err) |
| 28 | + } |
| 29 | + |
| 30 | + if !resp.Success { |
| 31 | + return fmt.Errorf("mark episode %d as read: operation failed", seq) |
| 32 | + } |
| 33 | + |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +// MarkAsUnread marks an episode as unread for the authenticated user. |
| 38 | +// The operation is idempotent - if the episode is already unread or has no |
| 39 | +// read record, the request succeeds silently. |
| 40 | +// |
| 41 | +// Returns an error if: |
| 42 | +// - The episode does not exist (404 not_found) |
| 43 | +// - The API request fails for other reasons |
| 44 | +func MarkAsUnread(ctx context.Context, client *api.Client, seq int) error { |
| 45 | + path := fmt.Sprintf("/open/v1/episodes/%d/unread", seq) |
| 46 | + |
| 47 | + var resp MarkResponse |
| 48 | + if err := client.Post(ctx, path, nil, &resp); err != nil { |
| 49 | + return fmt.Errorf("mark episode %d as unread: %w", seq, err) |
| 50 | + } |
| 51 | + |
| 52 | + if !resp.Success { |
| 53 | + return fmt.Errorf("mark episode %d as unread: operation failed", seq) |
| 54 | + } |
| 55 | + |
| 56 | + return nil |
| 57 | +} |
0 commit comments