Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Updated Models.go (simplified + orderBy param)

Added a new main function to get Torrents with orderBy parameter
Added a new function to get all torrents with orderBy parameter
Simplifying the other function that get torrents so they pass through one main function that manage db requests.
Commented AutoMigrate functions, it emits warning when you execute it after having migrated once
Cette révision appartient à :
akuma06 2017-05-04 17:16:20 +02:00
Parent d3fee1eae6
révision 59f820a264
2 fichiers modifiés avec 39 ajouts et 19 suppressions

Voir le fichier

@ -23,8 +23,8 @@ func getDBHandle() *gorm.DB {
dbInit, err := gorm.Open("sqlite3", "./nyaa.db")
// Migrate the schema of Torrents
dbInit.AutoMigrate(&Torrents{})
dbInit.AutoMigrate(&Sub_Categories{})
// dbInit.AutoMigrate(&Torrents{})
// dbInit.AutoMigrate(&Sub_Categories{})
checkErr(err)
return dbInit
@ -63,6 +63,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
return
}
}
func singleapiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

Voir le fichier

@ -13,13 +13,13 @@ type Categories struct {
Category_id int
Category_name string
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
Sub_category []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"`
Sub_Categories []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"`
}
type Sub_Categories struct {
Sub_category_id int
Sub_category_name string
Parent_id int
Sub_category_id int `gorm:"column:sub_category_id"`
Sub_category_name string `gorm:"column:Sub_category_name"`
Parent_id int `gorm:"column:parent_id"`
Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
}
@ -89,32 +89,51 @@ func getTorrentById(id string) (Torrents, error) {
return torrent, nil
}
func getTorrents(parameters WhereParams, limit int, offset int) []Torrents {
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) []Torrents {
var torrents []Torrents
db.Limit(limit).Offset(offset).Order("torrent_id DESC").Where(parameters.conditions, parameters.params...).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
var dbQuery *gorm.DB
if (parameters != nil) { // if there is where parameters
dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...)
} else {
dbQuery = db.Model(&torrents)
}
if (orderBy == "") { orderBy = "torrent_id DESC" } // Default OrderBy
if limit != 0 || offset != 0 { // if limits provided
dbQuery = dbQuery.Limit(limit).Offset(offset)
}
dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
return torrents
}
/* Functions to simplify the get parameters of the main function
*
* Get Torrents with where parameters and limits, order by default
*/
func getTorrents(parameters WhereParams, limit int, offset int) []Torrents {
return getTorrentsOrderBy(&parameters, "", limit, offset)
}
/* Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db)
*/
func getTorrentsDB(parameters WhereParams) []Torrents {
var torrents []Torrents
db.Where(parameters.conditions, parameters.params...).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
return torrents
return getTorrentsOrderBy(&parameters, "", 0, 0)
}
/* Function to get all torrents
*/
func getAllTorrents(limit int, offset int) []Torrents {
var torrents []Torrents
db.Model(&torrents).Limit(limit).Offset(offset).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
func getAllTorrentsOrderBy(orderBy string, limit int, offset int) [] Torrents {
return getTorrentsOrderBy(nil, orderBy, limit, offset)
}
return torrents
func getAllTorrents(limit int, offset int) []Torrents {
return getTorrentsOrderBy(nil, "", limit, offset)
}
func getAllTorrentsDB() []Torrents {
var torrents []Torrents
db.Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
return torrents
return getTorrentsOrderBy(nil, "", 0, 0)
}
/* Function to get all categories with/without torrents (bool)
@ -150,4 +169,4 @@ func createWhereParams(conditions string, params ...string) WhereParams {
return whereParams
}
/* Complete the functions when necessary... */
/* Complete the functions when necessary... */