Track artist popularity
authorJacob Casper <dev@jacobcasper.com>
Fri, 17 Apr 2020 02:14:20 +0000 (21:14 -0500)
committerJacob Casper <dev@jacobcasper.com>
Fri, 17 Apr 2020 02:14:20 +0000 (21:14 -0500)
migrations/005-alter_artist_add_popularity.sql [new file with mode: 0644]
routes/artist/artist.go

diff --git a/migrations/005-alter_artist_add_popularity.sql b/migrations/005-alter_artist_add_popularity.sql
new file mode 100644 (file)
index 0000000..fdd4e49
--- /dev/null
@@ -0,0 +1,3 @@
+ALTER TABLE ARTIST
+ADD COLUMN POPULARITY INT NOT NULL DEFAULT 0
+CHECK (POPULARITY >= 0 AND POPULARITY <= 100);
index 33122f5..d26f66d 100644 (file)
@@ -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
                                }