2017-05-04 05:28:42 +02:00
package main
import (
2017-05-04 12:01:27 +02:00
"errors"
2017-05-04 05:28:42 +02:00
"github.com/jinzhu/gorm"
"html"
"html/template"
"strconv"
2017-05-04 12:31:40 +02:00
"strings"
2017-05-04 05:28:42 +02:00
)
type Categories struct {
2017-05-04 12:01:27 +02:00
Category_id int
Category_name string
Torrents [ ] Torrents ` gorm:"ForeignKey:category_id;AssociationForeignKey:category_id" `
Sub_category [ ] Sub_Categories ` gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id" `
2017-05-04 05:28:42 +02:00
}
type Sub_Categories struct {
2017-05-04 12:01:27 +02:00
Sub_category_id int
Sub_category_name string
Parent_id int
Torrents [ ] Torrents ` gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id" `
2017-05-04 05:28:42 +02:00
}
type Torrents struct {
gorm . Model
2017-05-04 14:01:07 +02:00
Id int ` gorm:"column:torrent_id" `
Name string ` gorm:"column:torrent_name" `
Category_id int ` gorm:"column:category_id" `
Sub_category_id int ` gorm:"column:sub_category_id" `
2017-05-04 12:01:27 +02:00
Status int ` gorm:"column:status_id" `
Hash string ` gorm:"column:torrent_hash" `
Categories Categories ` gorm:"ForeignKey:category_id;AssociationForeignKey:category_id" `
Sub_Categories Sub_Categories ` gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id" `
2017-05-04 05:28:42 +02:00
}
/ * We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
JSON Models Oject
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
type CategoryJson struct {
2017-05-04 12:01:27 +02:00
Category string ` json: "category" `
Torrents [ ] TorrentsJson ` json: "torrents" `
QueryRecordCount int ` json: "queryRecordCount" `
TotalRecordCount int ` json: "totalRecordCount" `
2017-05-04 05:28:42 +02:00
}
type TorrentsJson struct {
2017-05-04 12:01:27 +02:00
Id string ` json: "id" ` // Is there a need to put the ID?
2017-05-04 05:28:42 +02:00
Name string ` json: "name" `
Status int ` json: "status" `
Hash string ` json: "hash" `
Magnet template . URL ` json: "magnet" `
}
type WhereParams struct {
conditions string // Ex : name LIKE ? AND category_id LIKE ?
2017-05-04 12:01:27 +02:00
params [ ] interface { }
2017-05-04 05:28:42 +02:00
}
/* Each Page should have an object to pass to their own template */
type HomeTemplateVariables struct {
2017-05-04 12:01:27 +02:00
ListTorrents [ ] TorrentsJson
ListCategories [ ] Categories
Query string
2017-05-04 14:01:07 +02:00
Status string
2017-05-04 12:01:27 +02:00
Category string
QueryRecordCount int
TotalRecordCount int
2017-05-04 05:28:42 +02:00
}
/ * Function to interact with Models
*
* Get the torrents with where clause
*
2017-05-04 12:01:27 +02:00
* /
2017-05-04 05:28:42 +02:00
func getTorrentById ( id string ) ( Torrents , error ) {
var torrent Torrents
if db . Order ( "torrent_id DESC" ) . First ( & torrent , "id = ?" , html . EscapeString ( id ) ) . RecordNotFound ( ) {
return torrent , errors . New ( "Article is not found." )
}
return torrent , nil
}
2017-05-04 12:01:27 +02:00
func getTorrents ( parameters WhereParams , limit int , offset int ) [ ] Torrents {
2017-05-04 05:28:42 +02:00
var torrents [ ] Torrents
db . Limit ( limit ) . Offset ( offset ) . Order ( "torrent_id DESC" ) . Where ( parameters . conditions , parameters . params ... ) . Preload ( "Categories" ) . Preload ( "Sub_Categories" ) . Find ( & torrents )
return torrents
}
2017-05-04 12:01:27 +02:00
func getTorrentsDB ( parameters WhereParams ) [ ] Torrents {
2017-05-04 05:28:42 +02:00
var torrents [ ] Torrents
db . Where ( parameters . conditions , parameters . params ... ) . Order ( "torrent_id DESC" ) . Preload ( "Categories" ) . Preload ( "Sub_Categories" ) . Find ( & torrents )
return torrents
}
2017-05-04 12:01:27 +02:00
/ * Function to get all torrents
* /
2017-05-04 05:28:42 +02:00
2017-05-04 12:01:27 +02:00
func getAllTorrents ( limit int , offset int ) [ ] Torrents {
2017-05-04 05:28:42 +02:00
var torrents [ ] Torrents
db . Model ( & torrents ) . Limit ( limit ) . Offset ( offset ) . Order ( "torrent_id DESC" ) . Preload ( "Categories" ) . Preload ( "Sub_Categories" ) . Find ( & torrents )
return torrents
}
2017-05-04 12:01:27 +02:00
func getAllTorrentsDB ( ) [ ] Torrents {
2017-05-04 05:28:42 +02:00
var torrents [ ] Torrents
db . Order ( "torrent_id DESC" ) . Preload ( "Categories" ) . Preload ( "Sub_Categories" ) . Find ( & torrents )
return torrents
}
/ * Function to get all categories with / without torrents ( bool )
2017-05-04 12:01:27 +02:00
* /
func getAllCategories ( populatedWithTorrents bool ) [ ] Categories {
2017-05-04 05:28:42 +02:00
var categories [ ] Categories
if populatedWithTorrents {
db . Preload ( "Torrents" ) . Preload ( "Sub_Categories" ) . Find ( & categories )
} else {
db . Preload ( "Sub_Categories" ) . Find ( & categories )
}
return categories
}
2017-05-04 12:01:27 +02:00
func ( t * Torrents ) toJson ( ) TorrentsJson {
2017-05-04 12:30:12 +02:00
magnet := "magnet:?xt=urn:btih:" + strings . TrimSpace ( t . Hash ) + "&dn=" + t . Name + trackers
2017-05-04 05:28:42 +02:00
res := TorrentsJson {
Id : strconv . Itoa ( t . Id ) ,
2017-05-04 12:01:27 +02:00
Name : html . UnescapeString ( t . Name ) ,
2017-05-04 05:28:42 +02:00
Status : t . Status ,
Hash : t . Hash ,
Magnet : safe ( magnet ) }
2017-05-04 12:01:27 +02:00
return res
2017-05-04 05:28:42 +02:00
}
2017-05-04 12:01:27 +02:00
func createWhereParams ( conditions string , params ... string ) WhereParams {
2017-05-04 05:28:42 +02:00
whereParams := WhereParams { }
whereParams . conditions = conditions
for i , _ := range params {
whereParams . params = append ( whereParams . params , params [ i ] )
}
return whereParams
}
2017-05-04 12:01:27 +02:00
/* Complete the functions when necessary... */