-
Notifications
You must be signed in to change notification settings - Fork 0
/
showstorage.go
48 lines (42 loc) · 1.01 KB
/
showstorage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package showstorage
import (
"database/sql"
"github.com/jmoiron/sqlx"
)
// Show stores a TV Show name, path to the files, and the next season and episode number.
type Show struct {
Name string `db:"name"`
LatestSeason int `db:"latest_season"`
NextEpisode int `db:"next_episode"`
Path string `dB:"path"`
}
// SetupTable creates the shows table if it does not exist.
func SetupTable(db *sqlx.DB) {
db.MustExec(`
CREATE TABLE IF NOT EXISTS shows (
name VARCHAR,
latest_season INTEGER,
next_episode INTEGER,
path VARCHAR
);
`)
}
// Find looks up a show by name.
func Find(db *sqlx.DB, name string) (*Show, error) {
show := Show{}
err := db.Get(&show, `SELECT * FROM shows WHERE name = ?`, name)
return &show, err
}
// Update stores new data in the database.
func Update(db *sqlx.DB, show *Show) (sql.Result, error) {
return db.NamedExec(
`
UPDATE shows SET
latest_season = :latest_season,
next_episode = :next_episode,
path = :path
WHERE name = :name
`,
&show,
)
}