package service import ( "context" "errors" "regexp" "time" "watch-party-backend/internal/repo" ) var ErrInvalidTime = errors.New("invalid start_time (expected HH:MM:SS)") type EpisodeService interface { GetCurrent(ctx context.Context) (repo.Episode, error) SetCurrent(ctx context.Context, id int64, start string) (repo.Episode, error) } type episodeService struct { repo repo.EpisodeRepository } func NewEpisodeService(r repo.EpisodeRepository) EpisodeService { return &episodeService{repo: r} } func (s *episodeService) GetCurrent(ctx context.Context) (repo.Episode, error) { c, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() return s.repo.GetCurrent(c) } func (s *episodeService) SetCurrent(ctx context.Context, id int64, start string) (repo.Episode, error) { // Strict HH:MM:SS (24h, zero-padded) — e.g., 20:07:05 if !isHHMMSS(start) { return repo.Episode{}, ErrInvalidTime } c, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() if err := s.repo.SetCurrent(c, id, start); err != nil { return repo.Episode{}, err } // return the new current row return s.repo.GetCurrent(c) } var hhmmss = regexp.MustCompile(`^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$`) func isHHMMSS(s string) bool { if !hhmmss.MatchString(s) { return false } _, err := time.Parse("15:04:05", s) return err == nil }