2017-05-05 14:20:51 +02:00
package model
import (
"github.com/ewhal/nyaa/config"
2017-05-06 04:18:24 +02:00
"github.com/ewhal/nyaa/util"
2017-05-05 14:20:51 +02:00
2017-05-08 19:26:29 +02:00
// "encoding/json"
2017-05-05 14:20:51 +02:00
"html"
"html/template"
"strconv"
"strings"
"time"
)
type Feed struct {
Id int
Name string
Hash string
Magnet string
Timestamp string
}
type Torrents struct {
2017-05-08 19:26:29 +02:00
Id uint ` gorm:"column:torrent_id;primary_key" `
Name string ` gorm:"column:torrent_name" `
Hash string ` gorm:"column:torrent_hash" `
Category int ` gorm:"column:category" `
Sub_Category int ` gorm:"column:sub_category" `
Status int ` gorm:"column:status" `
Date time . Time ` gorm:"column:date" `
UploaderId uint ` gorm:"column:uploader" `
Downloads int ` gorm:"column:downloads" `
Stardom int ` gorm:"column:stardom" `
Filesize int64 ` gorm:"column:filesize" `
Description string ` gorm:"column:description" `
WebsiteLink string ` gorm:"column:website_link" `
Uploader * User ` gorm:"ForeignKey:UploaderId" `
OldComments [ ] OldComment ` gorm:"ForeignKey:Id" `
Comments [ ] Comment ` gorm:"ForeignKey:Id" `
2017-05-05 14:20:51 +02:00
}
/ * We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
JSON Models Oject
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
2017-05-06 23:46:53 +02:00
type ApiResultJson struct {
2017-05-08 04:06:11 +02:00
Torrents [ ] TorrentsJson ` json:"torrents" `
QueryRecordCount int ` json:"queryRecordCount" `
TotalRecordCount int ` json:"totalRecordCount" `
2017-05-05 14:20:51 +02:00
}
2017-05-08 04:06:11 +02:00
type OldCommentsJson struct {
2017-05-06 04:18:24 +02:00
C template . HTML ` json:"c" `
Us string ` json:"us" `
Un string ` json:"un" `
UI int ` json:"ui" `
T int ` json:"t" `
Av string ` json:"av" `
ID string ` json:"id" `
}
2017-05-08 04:06:11 +02:00
type CommentsJson struct {
Content template . HTML ` json:"content" `
Username string ` json:"username" `
}
2017-05-05 14:20:51 +02:00
type TorrentsJson struct {
2017-05-08 04:06:11 +02:00
Id string ` json:"id" `
Name string ` json:"name" `
Status int ` json:"status" `
Hash string ` json:"hash" `
Date string ` json:"date" `
Filesize string ` json:"filesize" `
Description template . HTML ` json:"description" `
Comments [ ] CommentsJson ` json:"comments" `
Sub_Category string ` json:"sub_category" `
Category string ` json:"category" `
Magnet template . URL ` json:"magnet" `
2017-05-05 14:20:51 +02:00
}
/* Model Conversion to Json */
func ( t * Torrents ) ToJson ( ) TorrentsJson {
2017-05-07 03:10:35 +02:00
magnet := util . InfoHashToMagnet ( strings . TrimSpace ( t . Hash ) , t . Name , config . Trackers ... )
2017-05-08 19:26:29 +02:00
//offset := 0
2017-05-08 04:06:11 +02:00
var commentsJson [ ] CommentsJson
2017-05-08 19:26:29 +02:00
/ * if len ( t . OldComments ) != 0 {
2017-05-08 04:06:11 +02:00
b := [ ] OldCommentsJson { }
err := json . Unmarshal ( [ ] byte ( t . OldComments ) , & b )
if err == nil {
commentsJson = make ( [ ] CommentsJson , len ( t . Comments ) + len ( b ) )
offset = len ( b )
for i , commentJson := range b {
commentsJson [ i ] = CommentsJson { Content : commentJson . C ,
Username : commentJson . Un }
}
} else {
commentsJson = make ( [ ] CommentsJson , len ( t . Comments ) )
}
} else {
commentsJson = make ( [ ] CommentsJson , len ( t . Comments ) )
}
for i , comment := range t . Comments {
commentsJson [ i + offset ] = CommentsJson { Content : template . HTML ( comment . Content ) , Username : comment . Username }
2017-05-08 19:26:29 +02:00
} * /
2017-05-05 14:20:51 +02:00
res := TorrentsJson {
2017-05-08 19:26:29 +02:00
Id : strconv . FormatUint ( uint64 ( t . Id ) , 10 ) ,
2017-05-05 14:20:51 +02:00
Name : html . UnescapeString ( t . Name ) ,
Status : t . Status ,
Hash : t . Hash ,
2017-05-08 19:26:29 +02:00
Date : t . Date . Format ( time . RFC3339 ) ,
2017-05-07 13:51:59 +02:00
Filesize : util . FormatFilesize2 ( t . Filesize ) ,
2017-05-06 18:41:28 +02:00
Description : template . HTML ( t . Description ) ,
2017-05-08 04:06:11 +02:00
Comments : commentsJson ,
2017-05-06 23:46:53 +02:00
Sub_Category : strconv . Itoa ( t . Sub_Category ) ,
Category : strconv . Itoa ( t . Category ) ,
2017-05-05 14:20:51 +02:00
Magnet : util . Safe ( magnet ) }
return res
}
/* Complete the functions when necessary... */