diff --git a/main.go b/main.go index 9528a674..2b211e7f 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/models.go b/models.go index 4735d830..66a36ef0 100644 --- a/models.go +++ b/models.go @@ -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(¶meters, "", 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(¶meters, "", 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... */ \ No newline at end of file