Update Show.IsTracked
This commit is contained in:
parent
6234231e71
commit
d0bf7b4354
40
shows.go
40
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
|
||||
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)
|
||||
|
@ -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")
|
||||
}
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user