Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/models/report.go
akuma06 713ab02450 Added configor as a new library (#1126)
* Added configor as a new library

Now config is a singleton. You only need to do config.Get() instead of doing config.Conf.

* Forgot godep save 🐤

* Fix accidental removal of }
2017-07-10 22:11:05 +10:00

67 lignes
1,9 Kio
Go

package models
import (
"net/http"
"time"
"github.com/NyaaPantsu/nyaa/config"
)
// TorrentReport model
// User can be null (anonymous reports)
// FIXME can't preload field Torrents for models.TorrentReport
type TorrentReport struct {
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"`
CreatedAt time.Time `gorm:"column:created_at"`
Torrent *Torrent `gorm:"AssociationForeignKey:TorrentID;ForeignKey:torrent_id"`
User *User `gorm:"AssociationForeignKey:UserID;ForeignKey:user_id"`
}
// TableName : Return the name of torrent report table
func (report TorrentReport) TableName() string {
return config.Get().Models.ReportsTableName
}
// TorrentReportJSON : Json struct of torrent report model
type TorrentReportJSON struct {
ID uint `json:"id"`
Description string `json:"description"`
Torrent TorrentJSON `json:"torrent"`
User UserJSON `json:"user"`
}
// ToJSON : conversion to json of a torrent report
func (report *TorrentReport) ToJSON() TorrentReportJSON {
t := TorrentJSON{}
if report.Torrent != nil { // FIXME: report.Torrent should never be nil
t = report.Torrent.ToJSON()
}
u := UserJSON{}
if report.User != nil {
u = report.User.ToJSON()
}
json := TorrentReportJSON{report.ID, report.Description, t, u}
return json
}
// TorrentReportsToJSON : Conversion of multiple reports to json
func TorrentReportsToJSON(reports []TorrentReport) []TorrentReportJSON {
json := make([]TorrentReportJSON, len(reports))
for i := range reports {
json[i] = reports[i].ToJSON()
}
return json
}
// Delete : Delete torrent report
func (report *TorrentReport) Delete(definitely bool) (int, error) {
if definitely {
return 0, ORM.Unscoped().Delete(report).Error
}
return http.StatusOK, nil
}