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

add works

also, `name` -> `title` in the db and global db conn
raylu 14 жил өмнө
parent
commit
c680bce127
4 өөрчлөгдсөн 94 нэмэгдсэн , 13 устгасан
  1. 60 8
      main.go
  2. 10 1
      static/base.css
  3. 20 1
      static/script.js
  4. 4 3
      templates/p.html

+ 60 - 8
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)
 }

+ 10 - 1
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;

+ 20 - 1
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('<li id="r_' + id + '"><img src="http://i.ytimg.com/vi/' + id  + '/1.jpg" alt="' + id + '">' + title + '</li>');
+				var html = '<li id="r_' + id + '">';
+				html += '<a href="javascript:add(\'' + id + '\', \'' + title + '\')">';
+				html += '<img src="http://i.ytimg.com/vi/' + id  + '/1.jpg" alt="' + id + '">';
+				html +=	title;
+				html += '</a></li>';
+				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);
+		}
+	);
+}

+ 4 - 3
templates/p.html

@@ -15,7 +15,7 @@
 			<input type="text" id="user" value="anonymous" size="16">
 			<br>
 			<label for="song">Add a song:</label>
-			<input type="text" id="song" size="58" autocomplete="off">
+			<input type="text" id="song" autocomplete="off">
 			<input type="submit" value="Search">
 		</form>
 		<div id="results" onblur="$(this).slideUp()"><ul>
@@ -25,7 +25,7 @@
 		{{range .Songs}}
 			<section class="song">
 				<div class="info">
-					<a href="http://www.youtube.com/watch?v={{.Yid}}">{{.Name}}</a>
+					<a href="http://www.youtube.com/watch?v={{.Yid}}">{{.Title}}</a>
 				</div>
 				<div class="user">
 					{{.User}}
@@ -33,7 +33,7 @@
 				<div class="remove">×</div>
 				<br class="clear">
 				<div class="thumb">
-					<img src="http://i.ytimg.com/vi/{{.Yid}}/1.jpg" alt="{{.Name}}">
+					<img src="http://i.ytimg.com/vi/{{.Yid}}/1.jpg" alt="{{.Yid}}">
 				</div>
 				<canvas id="{{.Yid}}" width="700" height="20"></canvas>
 				<br>
@@ -48,6 +48,7 @@
 	</article>
 	{{$first := index .Songs 0}}
 	<script>
+		var pid = '{{.Id}}';
 		var current_id = '{{$first.Yid}}';
 		var ids = [{{range .Songs}}'{{.Yid}}',{{end}}];
 	</script>