From c680bce12701298901ccd5cb3f0c366799f6a1c6 Mon Sep 17 00:00:00 2001 From: raylu Date: Fri, 5 Aug 2011 01:45:13 -0700 Subject: add works also, `name` -> `title` in the db and global db conn --- main.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++------- static/base.css | 11 ++++++++- static/script.js | 21 ++++++++++++++++- templates/p.html | 7 +++--- 4 files changed, 94 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index de62f7a..a64aeb4 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( type Song struct { Yid string - Name string + Title string User string } type Playlist struct { @@ -20,22 +20,19 @@ type Playlist struct { var templates map[string]*template.Template const debug = true +var db *mysql.Client func home(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "path is %s", r.URL.Path[1:]) } + func playlist(w http.ResponseWriter, r *http.Request) { id := r.URL.Path[len("/p/"):] if len(id) != 8 { http.Redirect(w, r, "/", 303) return } - db, err := mysql.DialTCP("raylu.net", "audio", "audio", "audio") - if err != nil { - http.Error(w, err.String(), http.StatusInternalServerError) - return - } - query, err := db.Prepare("SELECT `yid`,`name`,`user` FROM `playlist` JOIN `song` WHERE `id` = ?;") + query, err := db.Prepare("SELECT `yid`,`title`,`user` FROM `playlist` JOIN `song` WHERE `id` = ?;") if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) return @@ -54,7 +51,7 @@ func playlist(w http.ResponseWriter, r *http.Request) { playlist := Playlist{Id: id, Songs: make([]*Song, 0, 2)} for { song := new(Song) - query.BindResult(&song.Yid, &song.Name, &song.User) + query.BindResult(&song.Yid, &song.Title, &song.User) eof, err := query.Fetch() if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) @@ -65,6 +62,11 @@ func playlist(w http.ResponseWriter, r *http.Request) { } playlist.Songs = append(playlist.Songs, song) } + err = query.FreeResult() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } if debug { t, err := template.ParseFile("templates/p.html") @@ -86,6 +88,48 @@ func playlist(w http.ResponseWriter, r *http.Request) { } } +func add(w http.ResponseWriter, r *http.Request) { + query, err := db.Prepare("SELECT `pid` FROM `playlist` WHERE `id` = ?;") + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + q := r.URL.Query() + err = query.BindParams(q.Get("pid")) + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + err = query.Execute() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + var pid int + query.BindResult(&pid) + _, err = query.Fetch() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + err = query.FreeResult() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + + 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")), + db.Escape(q.Get("user"))) + err = db.Query(sql) + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } +} + func main() { templates = make(map[string]*template.Template) for _, path := range []string{"p"} { @@ -97,7 +141,15 @@ func main() { templates[path] = t } + var err os.Error + db, err = mysql.DialTCP("raylu.net", "audio", "audio", "audio") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + http.HandleFunc("/", home) http.HandleFunc("/p/", playlist) + http.HandleFunc("/add/", add) http.ListenAndServe(":8000", nil) } diff --git a/static/base.css b/static/base.css index 656d8ef..c932d6f 100644 --- a/static/base.css +++ b/static/base.css @@ -33,7 +33,8 @@ input:focus { } input[type="submit"] { background-color: #111; - padding: 2px 5px; + padding: 1px 5px; + line-height: 27px; } label { cursor: pointer; @@ -70,6 +71,7 @@ header label[for="song"] { header input#song { left: 390px; top: 50px; + width: 420px; text-align: center; } header input[type="submit"] { @@ -100,6 +102,13 @@ div#results ul li { div#results ul li:hover { background-color: #024; } +div#results ul li a { + display: block; + height: 100%; +} +div#results ul li a:hover { + text-decoration: none; +} div#results ul li img { height: 50px; width: 66px; diff --git a/static/script.js b/static/script.js index c55aff6..4766387 100644 --- a/static/script.js +++ b/static/script.js @@ -124,7 +124,12 @@ function search(query) { for (var i = 0; i < entries.length; i++) { var title = entries[i].title.$t; var id = entries[i].media$group.yt$videoid.$t; - items.push('
  • ' + id + '' + title + '
  • '); + var html = '
  • '; + html += ''; + html += '' + id + ''; + html += title; + html += '
  • '; + items.push(html); } var results = $('#results ul').html(items.join('\n')); $('#results').slideDown(100); @@ -132,3 +137,17 @@ function search(query) { ); return false; } + +function add(id, title) { + params = { + 'pid': pid, + 'yid': id, + 'title': title, + 'user': $('#user').val() + } + $.getJSON('/add/', params, + function(data) { + alert(data); + } + ); +} diff --git a/templates/p.html b/templates/p.html index 1098e0b..c6f6004 100644 --- a/templates/p.html +++ b/templates/p.html @@ -15,7 +15,7 @@
    - +