From d0bf7b4354a6a8f51300985c39a8afca625ad8fa Mon Sep 17 00:00:00 2001 From: Nicolas Duhamel Date: Tue, 16 Feb 2016 20:17:15 +0100 Subject: [PATCH] Update Show.IsTracked --- shows.go | 42 +++++++++++++++++++++++++++++++++++------- shows_test.go | 8 ++++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/shows.go b/shows.go index fff7897..cbd8939 100644 --- a/shows.go +++ b/shows.go @@ -44,7 +44,9 @@ var Schema = sqly.Schema{ Sql: ` CREATE TABLE shows_tracked ( shows_id integer NOT NULL REFERENCES shows (id) ON DELETE CASCADE, - users_id integer NOT NULL REFERENCES users (id) ON DELETE CASCADE + users_id integer NOT NULL REFERENCES users (id) ON DELETE CASCADE, + season integer NOT NULL, + episode integer NOT NULL ); `}, }, @@ -64,17 +66,22 @@ const ( getEpisodesQuery = `SELECT title, season, episode FROM episodes WHERE shows_id=$1;` getShowWithUserQuery = ` - SELECT id, imdbid, title, - EXISTS (SELECT 1 FROM shows_tracked WHERE shows_id=shows.id AND users_id=$2) AS tracked - FROM shows WHERE imdbid=$1; - ` + SELECT + shows.id, + shows.title, + shows.imdbid, + COALESCE(shows_tracked.season,0) AS trackedseason, + COALESCE(shows_tracked.episode,0) AS trackedepisode + FROM shows LEFT JOIN shows_tracked ON shows.id=shows_tracked.shows_id AND shows_tracked.users_id=$2 + WHERE shows.imdbid=$1;` ) type Show struct { sqly.BaseTable polochon.Show - Episodes []*Episode - Tracked bool + Episodes []*Episode + TrackedSeason int + TrackedEpisode int } func Get(db *sqlx.DB, imdbID string) (*Show, error) { @@ -96,6 +103,27 @@ func GetAsUser(db *sqlx.DB, user *users.User, imdbID string) (*Show, error) { return s, nil } +// GetDetails retrieves details for the show, first try to +// get info from db, if not exists, use polochon.Detailer +// and save informations in the database for future use +func (s *Show) GetDetails(db *sqlx.DB) error { + return nil +} + +// GetDetailsAsUser like GetDetails but with User context +func (s *Show) GetDetailsAsUser(db *sqlx.DB, user *users.User) error { + return nil +} + +// IsTracked returns true if the show is tracked use this function +// after retrieve the show with GetAsUser or other *AsUser functions +func (s *Show) IsTracked() bool { + if s.TrackedSeason != 0 && s.TrackedEpisode != 0 { + return true + } + return false +} + func (s *Show) Add(db *sqlx.DB) error { var id int r, err := db.NamedQuery(addShowQuery, s) diff --git a/shows_test.go b/shows_test.go index f2f484e..459d4ab 100644 --- a/shows_test.go +++ b/shows_test.go @@ -160,12 +160,12 @@ func TestTrackedShow(t *testing.T) { if err != nil { t.Fatal(err) } - if show1.Tracked { + if show1.IsTracked() { t.Fatal("Tracked must be false here") } - q := `INSERT INTO shows_tracked (shows_id, users_id) VALUES ($1, $2);` - _, err = db.Exec(q, show1.ID, u.ID) + q := `INSERT INTO shows_tracked (shows_id, users_id, season, episode) VALUES ($1, $2, $3, $4);` + _, err = db.Exec(q, show1.ID, u.ID, 1, 1) if err != nil { t.Fatal(err) } @@ -173,7 +173,7 @@ func TestTrackedShow(t *testing.T) { if err != nil { t.Fatal(err) } - if !show2.Tracked { + if !show2.IsTracked() { t.Fatal("Tracked must be true here") } })