From 30baa977ecf2af70a4be2b7ea49cc7ceefa3e4c5 Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Thu, 16 Apr 2020 21:14:20 -0500 Subject: [PATCH] Track artist popularity --- migrations/005-alter_artist_add_popularity.sql | 3 +++ routes/artist/artist.go | 15 ++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 migrations/005-alter_artist_add_popularity.sql diff --git a/migrations/005-alter_artist_add_popularity.sql b/migrations/005-alter_artist_add_popularity.sql new file mode 100644 index 0000000..fdd4e49 --- /dev/null +++ b/migrations/005-alter_artist_add_popularity.sql @@ -0,0 +1,3 @@ +ALTER TABLE ARTIST +ADD COLUMN POPULARITY INT NOT NULL DEFAULT 0 +CHECK (POPULARITY >= 0 AND POPULARITY <= 100); diff --git a/routes/artist/artist.go b/routes/artist/artist.go index 33122f5..d26f66d 100644 --- a/routes/artist/artist.go +++ b/routes/artist/artist.go @@ -24,12 +24,12 @@ func Index(env *env.Env) routes.Handler { if artistId != "" { artist := types.Artist{} row := env.Db.Db.QueryRow(` -SELECT ID, NAME +SELECT ID, NAME, POPULARITY FROM ARTIST WHERE ID = ?`, artistId, ) - if err := row.Scan(&artist.ID, &artist.Name); err != nil { + if err := row.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -44,7 +44,7 @@ WHERE ID = ?`, } rows, err := env.Db.Db.Query(` -SELECT ID, NAME +SELECT ID, NAME, POPULARITY FROM ARTIST LIMIT 20`, ) @@ -57,7 +57,7 @@ LIMIT 20`, artists := make([]types.Artist, 0) for rows.Next() { artist := types.Artist{} - if err := rows.Scan(&artist.ID, &artist.Name); err != nil { + if err := rows.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -104,10 +104,11 @@ func Add(env *env.Env) routes.Handler { defer env.Db.Mu.Unlock() env.Db.Db.Exec(` INSERT INTO ARTIST -(ID, NAME) -VALUES (?, ?)`, +(ID, NAME, POPULARITY) +VALUES (?, ?, ?)`, artist.ID, artist.Name, + artist.Popularity, ) for _, genre := range artist.Genres { @@ -183,7 +184,7 @@ WHERE g.NAME = lower(?) artists := make([]types.Artist, 0) for rows.Next() { artist := types.Artist{} - if err := rows.Scan(&artist.ID, &artist.Name); err != nil { + if err := rows.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } -- 2.20.1