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 torrents
import (
"errors"
"net/http"
"strconv"
"strings"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/search/structs"
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
)
/ * Function to interact with Models
*
* Get the torrents with where clause
*
* /
// FindByID : get a torrent with its id
func FindByID ( id uint ) ( torrent models . Torrent , err error ) {
tmp := models . ORM . Where ( "torrent_id = ?" , id ) . Preload ( "Scrape" ) . Preload ( "Comments" )
2017-06-29 01:06:30 +02:00
if id > config . Conf . Models . LastOldTorrentID {
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
tmp = tmp . Preload ( "FileList" )
}
2017-06-29 01:06:30 +02:00
if id <= config . Conf . Models . LastOldTorrentID && ! config . IsSukebei ( ) {
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
// only preload old comments if they could actually exist
tmp = tmp . Preload ( "OldComments" )
}
err = tmp . Error
if err != nil {
return
}
if tmp . Find ( & torrent ) . RecordNotFound ( ) {
err = errors . New ( "Article is not found" )
return
}
2017-07-04 02:07:25 +02:00
torrent . ParseLanguages ( )
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
// GORM relly likes not doing its job correctly
// (or maybe I'm just retarded)
torrent . Uploader = new ( models . User )
models . ORM . Where ( "user_id = ?" , torrent . UploaderID ) . Find ( torrent . Uploader )
torrent . OldUploader = ""
if torrent . ID <= config . Conf . Models . LastOldTorrentID && torrent . UploaderID == 0 {
var tmp models . UserUploadsOld
if ! models . ORM . Where ( "torrent_id = ?" , torrent . ID ) . Find ( & tmp ) . RecordNotFound ( ) {
torrent . OldUploader = tmp . Username
}
}
for i := range torrent . Comments {
torrent . Comments [ i ] . User = new ( models . User )
err = models . ORM . Where ( "user_id = ?" , torrent . Comments [ i ] . UserID ) . Find ( torrent . Comments [ i ] . User ) . Error
if err != nil {
return
}
}
return
}
// FindRawByID : Get torrent with id without user or comments
// won't fetch user or comments
func FindRawByID ( id uint ) ( torrent models . Torrent , err error ) {
err = nil
if models . ORM . Where ( "torrent_id = ?" , id ) . Find ( & torrent ) . RecordNotFound ( ) {
err = errors . New ( "Torrent is not found" )
}
2017-07-04 02:07:25 +02:00
torrent . ParseLanguages ( )
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
return
}
// FindRawByHash : Get torrent with id without user or comments
// won't fetch user or comments
func FindRawByHash ( hash string ) ( torrent models . Torrent , err error ) {
err = nil
if models . ORM . Where ( "torrent_hash = ?" , hash ) . Find ( & torrent ) . RecordNotFound ( ) {
err = errors . New ( "Torrent is not found" )
}
2017-07-04 02:07:25 +02:00
torrent . ParseLanguages ( )
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
return
}
// FindOrderByNoCount : Get torrents based on search without counting and user
2017-07-02 16:54:55 +02:00
func FindOrderByNoCount ( parameters * structs . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] models . Torrent , err error ) {
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
torrents , _ , err = findOrderBy ( parameters , orderBy , limit , offset , false , false , false )
return
}
// FindOrderBy : Get torrents based on search without user
2017-07-02 16:54:55 +02:00
func FindOrderBy ( parameters * structs . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] models . Torrent , count int , err error ) {
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
torrents , count , err = findOrderBy ( parameters , orderBy , limit , offset , true , false , false )
return
}
// FindWithUserOrderBy : Get torrents based on search with user
2017-07-02 16:54:55 +02:00
func FindWithUserOrderBy ( parameters * structs . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] models . Torrent , count int , err error ) {
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
torrents , count , err = findOrderBy ( parameters , orderBy , limit , offset , true , true , false )
return
}
2017-07-02 16:54:55 +02:00
func findOrderBy ( parameters * structs . WhereParams , orderBy string , limit int , offset int , countAll bool , withUser bool , deleted bool ) (
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
torrents [ ] models . Torrent , count int , err error ,
) {
var conditionArray [ ] string
var params [ ] interface { }
if parameters != nil { // if there is where parameters
if len ( parameters . Conditions ) > 0 {
conditionArray = append ( conditionArray , parameters . Conditions )
}
params = parameters . Params
}
if ! deleted {
conditionArray = append ( conditionArray , "deleted_at IS NULL" )
} else {
conditionArray = append ( conditionArray , "deleted_at NOT NULL" )
}
conditions := strings . Join ( conditionArray , " AND " )
if countAll {
err = models . ORM . Unscoped ( ) . Model ( & torrents ) . Where ( conditions , params ... ) . Count ( & count ) . Error
if err != nil {
return
}
}
// build custom db query for performance reasons
dbQuery := "SELECT * FROM " + config . Conf . Models . TorrentsTableName
if conditions != "" {
dbQuery = dbQuery + " WHERE " + conditions
}
if orderBy == "" { // default OrderBy
orderBy = "torrent_id DESC"
}
dbQuery = dbQuery + " ORDER BY " + orderBy
if limit != 0 || offset != 0 { // if limits provided
dbQuery = dbQuery + " LIMIT " + strconv . Itoa ( limit ) + " OFFSET " + strconv . Itoa ( offset )
}
dbQ := models . ORM . Preload ( "Scrape" )
if withUser {
dbQ = dbQ . Preload ( "Uploader" )
}
if countAll {
dbQ = dbQ . Preload ( "Comments" )
}
err = dbQ . Preload ( "FileList" ) . Raw ( dbQuery , params ... ) . Find ( & torrents ) . Error
return
}
2017-06-29 01:06:30 +02:00
// Find obtain a list of torrents matching 'parameters' from the
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
// database. The list will be of length 'limit' and in default order.
// GetTorrents returns the first records found. Later records may be retrieved
// by providing a positive 'offset'
2017-07-02 16:54:55 +02:00
func Find ( parameters structs . WhereParams , limit int , offset int ) ( [ ] models . Torrent , int , error ) {
2017-06-29 01:06:30 +02:00
return FindOrderBy ( & parameters , "" , limit , offset )
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
}
// FindDB : Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db)
2017-07-02 16:54:55 +02:00
func FindDB ( parameters structs . WhereParams ) ( [ ] models . Torrent , int , error ) {
2017-06-29 01:06:30 +02:00
return FindOrderBy ( & parameters , "" , 0 , 0 )
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
}
// FindAllOrderBy : Get all torrents ordered by parameters
func FindAllOrderBy ( orderBy string , limit int , offset int ) ( [ ] models . Torrent , int , error ) {
2017-06-29 01:06:30 +02:00
return FindOrderBy ( nil , orderBy , limit , offset )
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
}
// FindAll : Get all torrents without order
func FindAll ( limit int , offset int ) ( [ ] models . Torrent , int , error ) {
2017-06-29 01:06:30 +02:00
return FindOrderBy ( nil , "" , limit , offset )
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
}
// GetAllInDB : Get all torrents
func GetAllInDB ( ) ( [ ] models . Torrent , int , error ) {
2017-06-29 01:06:30 +02:00
return FindOrderBy ( nil , "" , 0 , 0 )
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
}
// ToggleBlock ; Lock/Unlock a torrent based on id
func ToggleBlock ( id uint ) ( models . Torrent , int , error ) {
var torrent models . Torrent
if models . ORM . Unscoped ( ) . Model ( & torrent ) . First ( & torrent , id ) . RecordNotFound ( ) {
return torrent , http . StatusNotFound , errors . New ( "Torrent is not found" )
}
2017-07-04 02:07:25 +02:00
torrent . ParseLanguages ( )
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
if torrent . Status == models . TorrentStatusBlocked {
torrent . Status = models . TorrentStatusNormal
} else {
torrent . Status = models . TorrentStatusBlocked
}
if models . ORM . Unscoped ( ) . Model ( & torrent ) . UpdateColumn ( & torrent ) . Error != nil {
return torrent , http . StatusInternalServerError , errors . New ( "Torrent was not updated" )
}
return torrent , http . StatusOK , nil
}
// FindDeleted : Gets deleted torrents based on search params
2017-07-02 16:54:55 +02:00
func FindDeleted ( parameters * structs . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] models . Torrent , count int , err error ) {
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
torrents , count , err = findOrderBy ( parameters , orderBy , limit , offset , true , true , true )
return
}