Parent directory

db.go

3398 bytes
 1package db
 2
 3import (
 4	"context"
 5	"errors"
 6	"time"
 7
 8	"github.com/TheEdgeOfRage/ytrssil-api/models"
 9)
10
11var (
12	ErrChannelExists     = errors.New("channel already exists")
13	ErrChannelNotFound   = errors.New("no channel with that ID found")
14	ErrAlreadySubscribed = errors.New("already subscribed to channel")
15	ErrVideoExists       = errors.New("video already exists")
16)
17
18// DB represents a database layer for getting video and channel data
19type DB interface {
20	// ListChannels lists all channels from the database
21	ListChannels(ctx context.Context) ([]models.Channel, error)
22	// SubscribeToChannel will start fetching new videos from that channel
23	SubscribeToChannel(ctx context.Context, channel models.Channel) error
24	// UnsubscribeToChannel will stop fetching videos from that channel
25	UnsubscribeFromChannel(ctx context.Context, channelID string) error
26	// ToggleChannelShorts enables or disables shorts for a channel
27	ToggleChannelShorts(ctx context.Context, channelID string, enableShorts bool) error
28	// GetChannelByID returns a channel by its ID
29	GetChannelByID(ctx context.Context, channelID string) (*models.Channel, error)
30
31	// GetNewVideos returns a list of unwatched videos from all subscribed channels
32	GetNewVideos(ctx context.Context, sortDesc bool) ([]models.Video, error)
33	// GetWatchedVideos returns a list of all watched videos
34	GetWatchedVideos(ctx context.Context, sortDesc bool, limit int, offset int) ([]models.Video, error)
35	// HasVideo returns true if the video with the given ID exists in the DB
36	HasVideo(ctx context.Context, videoID string) (bool, error)
37	// AddVideo adds a newly published video to the database
38	AddVideo(ctx context.Context, video models.Video, channelID string, isDiscarded bool) error
39	// DiscardVideo marks a video as discarded (e.g., shorts filtered out)
40	DiscardVideo(ctx context.Context, videoID string) error
41	// SetVideoWatchTime sets or unsets the watch timestamp of a video
42	SetVideoWatchTime(ctx context.Context, videoID string, watchTime *time.Time) error
43	// SetVideoProgress sets or unsets the watch progress of a video
44	SetVideoProgress(ctx context.Context, videoID string, progress int) (*models.Video, error)
45	// GetVideo returns a single video by ID
46	GetVideo(ctx context.Context, videoID string) (*models.Video, error)
47	// SetVideoDownloadStatus sets the download status for a video
48	SetVideoDownloadStatus(ctx context.Context, videoID string, status string) error
49	// SetVideoDownloadCompleted marks a video download as completed
50	SetVideoDownloadCompleted(ctx context.Context, videoID string, filePath string) error
51	// SetVideoDownloadFailed marks a video download as failed with an error message
52	SetVideoDownloadFailed(ctx context.Context, videoID string, errorMsg string) error
53	// GetVideosForCleanup returns videos that were downloaded and watched older than the given duration
54	GetVideosForCleanup(ctx context.Context, olderThan time.Duration) ([]models.Video, error)
55	// GetLiveVideos returns unwatched videos that are currently live
56	GetLiveVideos(ctx context.Context) ([]models.Video, error)
57	// UpdateVideoLiveStatus updates the live status and duration of a video
58	UpdateVideoLiveStatus(ctx context.Context, videoID string, isLive bool, duration int) error
59	// DeleteVideoFile clears the download fields for a video
60	DeleteVideoFile(ctx context.Context, videoID string) error
61
62	// Close closes the DB connection
63	Close()
64}