2017-05-05 14:20:51 +02:00
package torrentService
2017-05-07 03:10:35 +02:00
2017-05-05 14:20:51 +02:00
import (
2017-05-07 03:10:35 +02:00
"errors"
"github.com/ewhal/nyaa/config"
2017-05-05 14:20:51 +02:00
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
2017-05-07 03:10:35 +02:00
"github.com/ewhal/nyaa/util"
2017-05-08 15:50:18 +02:00
"strconv"
2017-05-08 16:53:15 +02:00
"strings"
2017-05-05 14:20:51 +02:00
)
type WhereParams struct {
2017-05-05 14:51:19 +02:00
Conditions string // Ex : name LIKE ? AND category_id LIKE ?
Params [ ] interface { }
2017-05-05 14:20:51 +02:00
}
/ * Function to interact with Models
*
* Get the torrents with where clause
*
* /
// don't need raw SQL once we get MySQL
func GetFeeds ( ) [ ] model . Feed {
var result [ ] model . Feed
rows , err := db . ORM . DB ( ) .
Query (
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
"ORDER BY `timestamp` desc LIMIT 50" )
if err == nil {
for rows . Next ( ) {
item := model . Feed { }
rows . Scan ( & item . Id , & item . Name , & item . Hash , & item . Timestamp )
2017-05-07 03:10:35 +02:00
magnet := util . InfoHashToMagnet ( strings . TrimSpace ( item . Hash ) , item . Name , config . Trackers ... )
2017-05-05 14:20:51 +02:00
item . Magnet = magnet
// memory hog
result = append ( result , item )
}
rows . Close ( )
}
return result
}
func GetTorrentById ( id string ) ( model . Torrents , error ) {
var torrent model . Torrents
2017-05-08 04:06:11 +02:00
if db . ORM . Where ( "torrent_id = ?" , id ) . Preload ( "Comments" ) . Find ( & torrent ) . RecordNotFound ( ) {
2017-05-05 14:20:51 +02:00
return torrent , errors . New ( "Article is not found." )
}
return torrent , nil
}
func GetTorrentsOrderBy ( parameters * WhereParams , orderBy string , limit int , offset int ) ( [ ] model . Torrents , int ) {
var torrents [ ] model . Torrents
var count int
2017-05-07 21:04:21 +02:00
conditions := "torrent_hash IS NOT NULL" // filter out broken entries
if strings . HasPrefix ( orderBy , "filesize" ) {
// torrents w/ NULL filesize fuck up the sorting on postgres
// TODO: fix this properly
conditions += " AND filesize IS NOT NULL"
}
2017-05-05 14:20:51 +02:00
var params [ ] interface { }
if parameters != nil { // if there is where parameters
2017-05-08 16:53:15 +02:00
if len ( parameters . Conditions ) > 0 {
conditions += " AND " + parameters . Conditions
}
2017-05-05 14:51:19 +02:00
params = parameters . Params
2017-05-05 14:20:51 +02:00
}
db . ORM . Model ( & torrents ) . Where ( conditions , params ... ) . Count ( & count )
2017-05-08 15:50:18 +02:00
dbQuery := "SELECT * FROM torrents"
if conditions != "" {
dbQuery = dbQuery + " WHERE " + conditions
}
if strings . Contains ( conditions , "torrent_name" ) {
dbQuery = "WITH t AS (SELECT * FROM torrents WHERE " + conditions + ") SELECT * FROM t"
}
2017-05-05 14:20:51 +02:00
2017-05-07 21:04:21 +02:00
if orderBy == "" { // default OrderBy
2017-05-05 14:20:51 +02:00
orderBy = "torrent_id DESC"
2017-05-07 21:04:21 +02:00
}
2017-05-08 15:50:18 +02:00
dbQuery = dbQuery + " ORDER BY " + orderBy
2017-05-05 14:20:51 +02:00
if limit != 0 || offset != 0 { // if limits provided
2017-05-08 15:50:18 +02:00
dbQuery = dbQuery + " LIMIT " + strconv . Itoa ( limit ) + " OFFSET " + strconv . Itoa ( offset )
2017-05-05 14:20:51 +02:00
}
2017-05-08 15:50:18 +02:00
db . ORM . Raw ( dbQuery , params ... ) . Find ( & torrents )
2017-05-05 14:20:51 +02:00
return torrents , count
}
/ * Functions to simplify the get parameters of the main function
*
* Get Torrents with where parameters and limits , order by default
* /
func GetTorrents ( parameters WhereParams , limit int , offset int ) ( [ ] model . Torrents , int ) {
return GetTorrentsOrderBy ( & parameters , "" , limit , offset )
}
/ * Get Torrents with where parameters but no limit and order by default ( get all the torrents corresponding in the db )
* /
func GetTorrentsDB ( parameters WhereParams ) ( [ ] model . Torrents , int ) {
return GetTorrentsOrderBy ( & parameters , "" , 0 , 0 )
}
/ * Function to get all torrents
* /
func GetAllTorrentsOrderBy ( orderBy string , limit int , offset int ) ( [ ] model . Torrents , int ) {
return GetTorrentsOrderBy ( nil , orderBy , limit , offset )
}
func GetAllTorrents ( limit int , offset int ) ( [ ] model . Torrents , int ) {
return GetTorrentsOrderBy ( nil , "" , limit , offset )
}
func GetAllTorrentsDB ( ) ( [ ] model . Torrents , int ) {
return GetTorrentsOrderBy ( nil , "" , 0 , 0 )
}
func CreateWhereParams ( conditions string , params ... string ) WhereParams {
whereParams := WhereParams { }
2017-05-05 14:51:19 +02:00
whereParams . Conditions = conditions
2017-05-05 14:20:51 +02:00
for i , _ := range params {
2017-05-05 14:51:19 +02:00
whereParams . Params = append ( whereParams . Params , params [ i ] )
2017-05-05 14:20:51 +02:00
}
return whereParams
2017-05-06 22:32:50 +02:00
}