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

67 lignes
1,9 Kio
Go
Brut Vue normale Historique

package models
2017-05-10 20:42:20 +02:00
2017-05-11 00:10:42 +02:00
import (
"net/http"
2017-05-11 00:10:42 +02:00
"time"
"github.com/NyaaPantsu/nyaa/config"
2017-05-11 00:10:42 +02:00
)
// TorrentReport model
// User can be null (anonymous reports)
// FIXME can't preload field Torrents for models.TorrentReport
2017-05-10 20:42:20 +02:00
type TorrentReport struct {
2017-10-29 04:47:39 +01:00
ID uint `gorm:"column:torrent_report_id;primary_key"`
2017-05-10 21:09:37 +02:00
Description string `gorm:"column:type"`
2017-10-29 04:47:39 +01:00
Message string `gorm:"column:message"`
2017-05-10 21:09:37 +02:00
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
}
// 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 {
2017-05-10 20:42:20 +02:00
ID uint `json:"id"`
Description string `json:"description"`
2017-10-29 04:47:39 +01:00
Message string `json:"message"`
2017-05-10 20:42:20 +02:00
Torrent TorrentJSON `json:"torrent"`
2017-05-10 21:09:37 +02:00
User UserJSON `json:"user"`
2017-05-10 20:42:20 +02:00
}
// 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()
}
2017-10-29 04:47:39 +01:00
json := TorrentReportJSON{report.ID, report.Description, report.Message, t, u}
2017-05-10 20:42:20 +02:00
return json
}
// TorrentReportsToJSON : Conversion of multiple reports to json
func TorrentReportsToJSON(reports []TorrentReport) []TorrentReportJSON {
json := make([]TorrentReportJSON, len(reports))
2017-05-10 20:42:20 +02:00
for i := range reports {
json[i] = reports[i].ToJSON()
2017-05-10 20:42:20 +02:00
}
return json
}
// Delete : Delete torrent report
2017-10-29 05:31:16 +01:00
func (report *TorrentReport) Delete() (int, error) {
return http.StatusOK, ORM.Unscoped().Delete(report).Error
}