|
@@ -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() {
|
|
|
|