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"
2017-07-05 17:06:24 +02:00
"time"
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
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
2017-07-05 17:06:24 +02:00
"github.com/NyaaPantsu/nyaa/utils/cache"
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
2017-07-05 17:06:24 +02:00
func FindByID ( id uint ) ( * models . Torrent , error ) {
2017-07-09 16:21:34 +02:00
torrent := & models . Torrent { ID : id }
2017-07-05 17:06:24 +02:00
var err error
2017-07-09 16:21:34 +02:00
if found , ok := cache . C . Get ( torrent . Identifier ( ) ) ; ok {
2017-07-05 17:06:24 +02:00
return found . ( * models . Torrent ) , nil
2017-07-10 14:11:05 +02:00
2017-07-05 17:06:24 +02:00
}
2017-07-10 14:11:05 +02:00
2017-07-14 17:53:37 +02:00
tmp := models . ORM . Where ( "torrent_id = ?" , id ) . Preload ( "Scrape" ) . Preload ( "Uploader" ) . Preload ( "Comments" )
2017-07-10 14:11:05 +02:00
if id > config . Get ( ) . 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-07-10 14:11:05 +02:00
if id <= config . Get ( ) . 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 {
2017-07-05 17:06:24 +02:00
return torrent , err
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
}
2017-07-05 17:06:24 +02:00
if tmp . Find ( torrent ) . RecordNotFound ( ) {
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
err = errors . New ( "Article is not found" )
2017-07-05 17:06:24 +02:00
return torrent , err
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
}
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 . OldUploader = ""
2017-07-10 14:11:05 +02:00
if torrent . ID <= config . Get ( ) . Models . LastOldTorrentID && torrent . UploaderID == 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
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 )
2017-07-05 17:06:24 +02:00
models . ORM . Where ( "user_id = ?" , torrent . Comments [ i ] . UserID ) . Find ( torrent . Comments [ i ] . User )
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
}
2017-07-09 16:21:34 +02:00
cache . C . Set ( torrent . Identifier ( ) , torrent , 5 * time . Minute )
2017-07-05 17:06:24 +02:00
return torrent , nil
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
}
// 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
}
2017-07-15 01:44:09 +02:00
// FindUnscopeByID : Get torrent with ID deleted or not
func FindUnscopeByID ( id uint ) ( torrent models . Torrent , err error ) {
err = nil
if models . ORM . Unscoped ( ) . Where ( "torrent_id = ?" , id ) . Preload ( "Uploader" ) . Find ( & torrent ) . RecordNotFound ( ) {
err = errors . New ( "Torrent is not found" )
}
torrent . ParseLanguages ( )
return
}
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
// 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 " )
2017-07-06 23:54:21 +02:00
/ * if found , ok := cache . C . Get ( fmt . Sprintf ( "%v" , parameters ) ) ; ok {
2017-07-05 19:50:44 +02:00
torrentCache := found . ( * structs . TorrentCache )
torrents = torrentCache . Torrents
count = torrentCache . Count
2017-07-05 17:06:24 +02:00
return
2017-07-06 23:54:21 +02:00
} * /
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 countAll {
err = models . ORM . Unscoped ( ) . Model ( & torrents ) . Where ( conditions , params ... ) . Count ( & count ) . Error
if err != nil {
return
}
}
// build custom db query for performance reasons
2017-07-10 14:11:05 +02:00
dbQuery := "SELECT * FROM " + config . Get ( ) . Models . TorrentsTableName
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 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
2017-07-06 23:54:21 +02:00
// cache.C.Set(fmt.Sprintf("%v", parameters), &structs.TorrentCache{torrents, count}, 5*time.Minute) // Cache shouldn't be done here but in search util
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
}
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
}