|
|
@@ -24,33 +24,6 @@ var templates map[string]*template.Template
|
|
|
const debug = true
|
|
|
var db *mysql.Client
|
|
|
|
|
|
-// given an id ('abcd1234'), return the pid (1)
|
|
|
-func getpid(id string) int {
|
|
|
- query, err := db.Prepare("SELECT `pid` FROM `playlist` WHERE `id` = ?;")
|
|
|
- if err != nil {
|
|
|
- return -1
|
|
|
- }
|
|
|
- err = query.BindParams(id)
|
|
|
- if err != nil {
|
|
|
- return -1
|
|
|
- }
|
|
|
- err = query.Execute()
|
|
|
- if err != nil {
|
|
|
- return -1
|
|
|
- }
|
|
|
- var pid int
|
|
|
- query.BindResult(&pid)
|
|
|
- _, err = query.Fetch()
|
|
|
- if err != nil {
|
|
|
- return -1
|
|
|
- }
|
|
|
- err = query.FreeResult()
|
|
|
- if err != nil {
|
|
|
- return -1
|
|
|
- }
|
|
|
- return pid
|
|
|
-}
|
|
|
-
|
|
|
func home(w http.ResponseWriter, r *http.Request) {
|
|
|
fmt.Fprintf(w, "path is %s", r.URL.Path[1:])
|
|
|
}
|
|
|
@@ -91,7 +64,7 @@ func add(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- sql :="INSERT INTO `song` (`pid`,`yid`,`title`,`user`) VALUES(%d,'%s','%s','%s')"
|
|
|
+ sql := "INSERT INTO `song` (`pid`,`yid`,`title`,`user`) VALUES(%d,'%s','%s','%s')"
|
|
|
sql = fmt.Sprintf(sql, pid,
|
|
|
db.Escape(q.Get("yid")),
|
|
|
db.Escape(q.Get("title")),
|
|
|
@@ -115,7 +88,7 @@ func remove(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- sql :="DELETE FROM `song` WHERE `pid` = %d AND yid = '%s'"
|
|
|
+ sql := "DELETE FROM `song` WHERE `pid` = %d AND yid = '%s'"
|
|
|
sql = fmt.Sprintf(sql, pid, q.Get("yid"))
|
|
|
err := db.Query(sql)
|
|
|
if err != nil {
|
|
|
@@ -127,11 +100,78 @@ func remove(w http.ResponseWriter, r *http.Request) {
|
|
|
addUpdate(pid, removeAction, &Song{Yid: q.Get("yid")})
|
|
|
}
|
|
|
|
|
|
+func move(w http.ResponseWriter, r *http.Request) {
|
|
|
+ q := r.URL.Query()
|
|
|
+ pid := getpid(q.Get("pid"))
|
|
|
+ if pid == -1 {
|
|
|
+ http.Error(w, "invalid pid", http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ direction, err := strconv.Atoui(q.Get("direction"))
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ err = db.Start()
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ order, err := queryInt("SELECT `order` FROM `song` WHERE `yid` = ? AND `pid` = ?",
|
|
|
+ q.Get("yid"), pid)
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newOrder := order
|
|
|
+ if direction == moveUpAction && order > 0 {
|
|
|
+ newOrder--
|
|
|
+ } else if direction == moveDownAction {
|
|
|
+ newOrder++
|
|
|
+ } else {
|
|
|
+ http.Error(w, "invalid direction or cannot move up", http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ sql := "UPDATE `song` SET `order` = %d WHERE `order` = %d AND pid = %d"
|
|
|
+ sql = fmt.Sprintf(sql, order, newOrder, pid)
|
|
|
+ err = db.Query(sql)
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ } else if db.AffectedRows != 1 {
|
|
|
+ db.Rollback()
|
|
|
+ http.Error(w, "invalid direction for this song", http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // there are now two songs with that order, so also check yid
|
|
|
+ sql = "UPDATE `song` SET `order` = %d WHERE `order` = %d AND pid = %d AND yid = '%s'"
|
|
|
+ sql = fmt.Sprintf(sql, newOrder, order, pid, q.Get("yid"))
|
|
|
+ err = db.Query(sql)
|
|
|
+ if err != nil {
|
|
|
+ db.Rollback()
|
|
|
+ http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ err = db.Commit()
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Write([]byte("1"))
|
|
|
+ addUpdate(pid, direction, &Song{Yid: q.Get("yid")})
|
|
|
+}
|
|
|
+
|
|
|
func poll(w http.ResponseWriter, r *http.Request) {
|
|
|
q := r.URL.Query()
|
|
|
timestamp := q.Get("timestamp")
|
|
|
if timestamp == "0" {
|
|
|
- query, err := db.Prepare("SELECT `yid`,`title`,`user` FROM `playlist` JOIN `song` WHERE `id` = ?;")
|
|
|
+ query, err := db.Prepare("SELECT `yid`,`title`,`user` FROM `playlist` JOIN `song` WHERE `id` = ? ORDER BY `order` ASC")
|
|
|
if err != nil {
|
|
|
http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
return
|
|
|
@@ -227,6 +267,7 @@ func main() {
|
|
|
http.HandleFunc("/p/", playlist)
|
|
|
http.HandleFunc("/add/", add)
|
|
|
http.HandleFunc("/remove/", remove)
|
|
|
+ http.HandleFunc("/move/", move)
|
|
|
http.HandleFunc("/poll/", poll)
|
|
|
err = http.ListenAndServe("localhost:8000", nil)
|
|
|
if err != nil {
|