Browse Source

added GetLastTorrents function

TooManySugar 3 năm trước cách đây
mục cha
commit
651b458c44
1 tập tin đã thay đổi với 48 bổ sung0 xóa
  1. 48 0
      cmd/web/connector.go

+ 48 - 0
cmd/web/connector.go

@@ -248,6 +248,54 @@ func (c connection) TakeTorrent(hash string) (Torrent, error) {
 	return torrent, fmt.Errorf("Torrent not found")
 }
 
+
+// Takes 'amount' latest torrents in order of Time desc 
+// (torrents[0] is a newest)
+// If amount is 0 returns all torrents in same order as usual 
+func (c connection) GetLastTorrents(amount int64) (torrents []Torrent, count int64, err error) {
+	db, err := sql.Open("mysql", c.dBOpenStr)
+	if err != nil {
+		return
+	}
+	defer db.Close()
+
+	var act_query string
+
+	act_query = fmt.Sprintf("SELECT * FROM `%s`.`torrents`", c.connProperties.Db)
+	act_query += " ORDER BY `time` DESC" 
+	if amount != 0 {
+		act_query += fmt.Sprintf(" LIMIT %d",  amount)
+	}
+
+	
+	
+	fmt.Println(act_query)
+	rows, err := db.Query(act_query)
+	if err != nil {
+		return
+	}
+	defer rows.Close()
+
+	for rows.Next() {
+		var torrent Torrent
+		if err := rows.Scan(
+			&torrent.ID,
+			&torrent.Hash,
+			&torrent.Name,
+			&torrent.OwnerID,
+			&torrent.Time,
+			&torrent.Deleted,
+		); err != nil {
+			log.Fatal(err)
+		}
+
+		torrents = append(torrents, torrent)
+		count++
+	}
+
+	return
+}
+
 /*
 func main() {