2017-05-10 20:42:20 +02:00
package model
2017-05-11 00:10:42 +02:00
import (
"time"
)
2017-05-10 20:42:20 +02:00
// TODO Add field to specify kind of reports
// TODO Add CreatedAt field
// INFO User can be null (anonymous reports)
// FIXME can't preload field Torrents for model.TorrentReport
type TorrentReport struct {
2017-05-10 21:09:37 +02:00
ID uint ` gorm:"column:torrent_report_id;primary_key" `
Description string ` gorm:"column:type" `
TorrentID uint ` gorm:"column:torrent_id" `
UserID uint ` gorm:"column:user_id" `
2017-05-11 00:10:42 +02:00
CreatedAt time . Time ` gorm:"column:created_at" `
2017-05-10 22:31:15 +02:00
Torrent * Torrent ` gorm:"AssociationForeignKey:TorrentID;ForeignKey:torrent_id" `
User * User ` gorm:"AssociationForeignKey:UserID;ForeignKey:user_id" `
2017-05-10 20:42:20 +02:00
}
type TorrentReportJson struct {
ID uint ` json:"id" `
Description string ` json:"description" `
Torrent TorrentJSON ` json:"torrent" `
2017-05-10 21:09:37 +02:00
User UserJSON ` json:"user" `
2017-05-10 20:42:20 +02:00
}
/* Model Conversion to Json */
2017-05-12 20:43:38 +02:00
func getReportDescription ( d string ) string {
if d == "illegal" {
return "Illegal content"
} else if d == "spam" {
return "Spam / Garbage"
} else if d == "wrongcat" {
return "Wrong category"
} else if d == "dup" {
return "Duplicate / Deprecated"
}
return "???"
}
2017-05-10 20:42:20 +02:00
func ( report * TorrentReport ) ToJson ( ) TorrentReportJson {
2017-05-12 20:43:38 +02:00
// FIXME: report.Torrent and report.User should never be nil
var t TorrentJSON = TorrentJSON { }
if report . Torrent != nil {
t = report . Torrent . ToJSON ( )
}
var u UserJSON = UserJSON { }
if report . User != nil {
u = report . User . ToJSON ( )
}
json := TorrentReportJson { report . ID , getReportDescription ( report . Description ) , t , u }
2017-05-10 20:42:20 +02:00
return json
}
func TorrentReportsToJSON ( reports [ ] TorrentReport ) [ ] TorrentReportJson {
json := make ( [ ] TorrentReportJson , len ( reports ) )
for i := range reports {
json [ i ] = reports [ i ] . ToJson ( )
}
return json
}