Эх сурвалжийг харах

fix 2 bugs related to empty playlists

raylu 14 жил өмнө
parent
commit
b5f572860a
2 өөрчлөгдсөн 8 нэмэгдсэн , 4 устгасан
  1. 5 3
      main.go
  2. 3 1
      static/script.js

+ 5 - 3
main.go

@@ -70,7 +70,9 @@ func add(w http.ResponseWriter, r *http.Request) {
 		http.Error(w, err.String(), http.StatusInternalServerError)
 		return
 	}
-	maxOrder, err := queryInt(db, "SELECT MAX(`order`) FROM `song` WHERE pid = ?", pid)
+	// unfortunately, MAX(`order`) returns NULL when there is nothing
+	// so query for COUNT(`sid`)
+	count, err := queryInt(db, "SELECT COUNT(`sid`) FROM `song` WHERE `pid` = ?", pid)
 	if err != nil {
 		db.Rollback()
 		http.Error(w, err.String(), http.StatusInternalServerError)
@@ -78,7 +80,7 @@ func add(w http.ResponseWriter, r *http.Request) {
 	}
 	_, err = prepare(db,
 			"INSERT INTO `song` (`pid`,`yid`,`title`,`user`,`order`) VALUES(?, ?, ?, ?, ?)",
-			pid, q.Get("yid"), q.Get("title"), q.Get("user"), maxOrder + 1)
+			pid, q.Get("yid"), q.Get("title"), q.Get("user"), count)
 	if err != nil {
 		db.Rollback()
 		http.Error(w, err.String(), http.StatusInternalServerError)
@@ -222,7 +224,7 @@ func move(w http.ResponseWriter, r *http.Request) {
 func poll(w http.ResponseWriter, r *http.Request) {
 	q := r.URL.Query()
 	timestamp := q.Get("timestamp")
-	if timestamp == "0" {
+	if timestamp == "-1" {
 		db := <-dbPool
 		defer func () {dbPool <- db}()
 

+ 3 - 1
static/script.js

@@ -4,7 +4,7 @@ var player;
 var litColor = '#eee';
 var dimmedColor = '#555';
 var interval;
-var timestamp = '0';
+var timestamp = '-1';
 
 $(document).ready(function () {
 	$('body').click(function (e) {
@@ -68,6 +68,8 @@ function handleUpdates(data) {
 			break;
 		}
 	}
+	if (timestamp == '-1')
+		timestamp = '0';
 	getUpdates();
 }