This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
package models
2017-05-10 20:42:20 +02:00
2017-05-11 00:10:42 +02:00
import (
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
"net/http"
2017-05-11 00:10:42 +02:00
"time"
2017-05-20 16:26:22 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-05-11 00:10:42 +02:00
)
2017-05-26 12:12:52 +02:00
// TorrentReport model
2017-05-20 16:26:22 +02:00
// User can be null (anonymous reports)
2017-07-01 23:09:35 +02:00
// 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
}
2017-05-26 12:12:52 +02:00
// TableName : Return the name of torrent report table
func ( report TorrentReport ) TableName ( ) string {
2017-07-10 14:11:05 +02:00
return config . Get ( ) . Models . ReportsTableName
2017-05-20 16:26:22 +02:00
}
2017-05-26 12:12:52 +02:00
// 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
}
2017-05-26 12:12:52 +02:00
// ToJSON : conversion to json of a torrent report
func ( report * TorrentReport ) ToJSON ( ) TorrentReportJSON {
t := TorrentJSON { }
2017-05-20 16:26:22 +02:00
if report . Torrent != nil { // FIXME: report.Torrent should never be nil
2017-05-12 20:43:38 +02:00
t = report . Torrent . ToJSON ( )
}
2017-05-26 12:12:52 +02:00
u := UserJSON { }
2017-05-12 20:43:38 +02:00
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
}
2017-05-26 12:12:52 +02:00
// 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 {
2017-05-26 12:12:52 +02:00
json [ i ] = reports [ i ] . ToJSON ( )
2017-05-10 20:42:20 +02:00
}
return json
}
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
// 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
}