Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Merge pull request #17 from akuma06/master

Updated Models.go (simplified + orderBy param)
Cette révision appartient à :
Eliot Whalan 2017-05-05 08:15:28 +10:00 révisé par GitHub
révision 8039f13b09
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") dbInit, err := gorm.Open("sqlite3", "./nyaa.db")
// Migrate the schema of Torrents // Migrate the schema of Torrents
dbInit.AutoMigrate(&Torrents{}) // dbInit.AutoMigrate(&Torrents{})
dbInit.AutoMigrate(&Sub_Categories{}) // dbInit.AutoMigrate(&Sub_Categories{})
checkErr(err) checkErr(err)
return dbInit return dbInit
@ -63,6 +63,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
func singleapiHandler(w http.ResponseWriter, r *http.Request) { func singleapiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)

Voir le fichier

@ -13,13 +13,13 @@ type Categories struct {
Category_id int Category_id int
Category_name string Category_name string
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"` 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 { type Sub_Categories struct {
Sub_category_id int Sub_category_id int `gorm:"column:sub_category_id"`
Sub_category_name string Sub_category_name string `gorm:"column:Sub_category_name"`
Parent_id int Parent_id int `gorm:"column:parent_id"`
Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_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 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 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 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 { func getTorrentsDB(parameters WhereParams) []Torrents {
var torrents []Torrents return getTorrentsOrderBy(&parameters, "", 0, 0)
db.Where(parameters.conditions, parameters.params...).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
return torrents
} }
/* Function to get all torrents /* Function to get all torrents
*/ */
func getAllTorrents(limit int, offset int) []Torrents { func getAllTorrentsOrderBy(orderBy string, limit int, offset int) [] Torrents {
var torrents []Torrents return getTorrentsOrderBy(nil, orderBy, limit, offset)
db.Model(&torrents).Limit(limit).Offset(offset).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents) }
return torrents func getAllTorrents(limit int, offset int) []Torrents {
return getTorrentsOrderBy(nil, "", limit, offset)
} }
func getAllTorrentsDB() []Torrents { func getAllTorrentsDB() []Torrents {
var torrents []Torrents return getTorrentsOrderBy(nil, "", 0, 0)
db.Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
return torrents
} }
/* Function to get all categories with/without torrents (bool) /* Function to get all categories with/without torrents (bool)
@ -150,4 +169,4 @@ func createWhereParams(conditions string, params ...string) WhereParams {
return whereParams return whereParams
} }
/* Complete the functions when necessary... */ /* Complete the functions when necessary... */