basic sort
Cette révision appartient à :
Parent
b485506716
révision
ccc727bfd2
3 fichiers modifiés avec 82 ajouts et 49 suppressions
11
index.html
11
index.html
|
@ -67,8 +67,17 @@
|
|||
<option value="4" {{if eq .Status "4"}}selected{{end}}>A+</option>
|
||||
</select>
|
||||
<input name="q" class="form-control input-sm" placeholder="Search" type="text" value={{.Query}}>
|
||||
<select name="sort" class="form-control input-sm">
|
||||
<option value="torrent_id" {{if eq .Sort "torrent_id"}}selected{{end}}>Id</option>
|
||||
<option value="torrent_name" {{if eq .Sort "torrent_name"}}selected{{end}}>Name</option>
|
||||
<option value="date" {{if eq .Sort "date"}}selected{{end}}>Date</option>
|
||||
<option value="downloads" {{if eq .Sort "downloads"}}selected{{end}}>Downloads</option>
|
||||
</select>
|
||||
<select name="order" class="form-control input-sm">
|
||||
<option value="desc" {{if eq .Order "desc"}}selected{{end}}>Descend</option>
|
||||
<option value="asc" {{if eq .Order "asc"}}selected{{end}}>Ascend</option>
|
||||
</select>
|
||||
<select name="max" class="form-control input-sm" value>
|
||||
<option value="">No Items per Page</option>
|
||||
<option value="5" {{if eq .QueryRecordCount 5}}selected{{end}}>5</option>
|
||||
<option value="10" {{if eq .QueryRecordCount 10}}selected{{end}}>10</option>
|
||||
<option value="15" {{if eq .QueryRecordCount 15}}selected{{end}}>15</option>
|
||||
|
|
26
main.go
26
main.go
|
@ -24,8 +24,7 @@ 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{}, &Categories{}, &Sub_Categories{}, &Statuses{})
|
||||
|
||||
checkErr(err)
|
||||
return dbInit
|
||||
|
@ -99,6 +98,9 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
|||
searchQuery := r.URL.Query().Get("q")
|
||||
cat := r.URL.Query().Get("c")
|
||||
stat := r.URL.Query().Get("s")
|
||||
sort := r.URL.Query().Get("sort")
|
||||
order := r.URL.Query().Get("order")
|
||||
|
||||
catsSplit := strings.Split(cat, "_")
|
||||
// need this to prevent out of index panics
|
||||
var searchCatId, searchSubCatId string
|
||||
|
@ -107,13 +109,21 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
|||
searchCatId = html.EscapeString(catsSplit[0])
|
||||
searchSubCatId = html.EscapeString(catsSplit[1])
|
||||
}
|
||||
if sort == "" {
|
||||
sort = "torrent_id"
|
||||
}
|
||||
if order == "" {
|
||||
order = "desc"
|
||||
}
|
||||
order_by := sort + " " + order
|
||||
|
||||
nbTorrents := 0
|
||||
|
||||
b := []TorrentsJson{}
|
||||
|
||||
torrents := getTorrents(createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?",
|
||||
"%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%"), maxPerPage, maxPerPage*(pagenum-1))
|
||||
parameters := createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?",
|
||||
"%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%")
|
||||
torrents := getTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
||||
|
||||
for i, _ := range torrents {
|
||||
nbTorrents++
|
||||
|
@ -123,7 +133,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
}
|
||||
|
||||
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, maxPerPage, nbTorrents}
|
||||
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, sort, order, maxPerPage, nbTorrents}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
|
@ -150,12 +160,12 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
torrents := getFeeds()
|
||||
created := time.Now().String()
|
||||
if ( len(torrents) > 0 ) {
|
||||
if len(torrents) > 0 {
|
||||
created = torrents[0].Timestamp
|
||||
}
|
||||
created_as_time, err := time.Parse("2006-01-02 15:04:05", created)
|
||||
if err == nil {
|
||||
;
|
||||
|
||||
}
|
||||
feed := &feeds.Feed{
|
||||
Title: "Nyaa Pantsu",
|
||||
|
@ -210,7 +220,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
}
|
||||
|
||||
htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", maxPerPage, nbTorrents}
|
||||
htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", "id", "desc", maxPerPage, nbTorrents}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
|
|
32
models.go
32
models.go
|
@ -25,20 +25,30 @@ type Categories struct {
|
|||
}
|
||||
|
||||
type Sub_Categories struct {
|
||||
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"`
|
||||
Sub_category_id int
|
||||
Sub_category_name string
|
||||
Parent_id int
|
||||
Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
|
||||
}
|
||||
|
||||
type Statuses struct {
|
||||
Status_id int
|
||||
Status_name string
|
||||
Torrents []Torrents `gorm:"ForeignKey:status_id;AssociationForeignKey:status_id"`
|
||||
}
|
||||
|
||||
type Torrents struct {
|
||||
gorm.Model
|
||||
Id int `gorm:"column:torrent_id"`
|
||||
Name string `gorm:"column:torrent_name"`
|
||||
Category_id int `gorm:"column:category_id"`
|
||||
Sub_category_id int `gorm:"column:sub_category_id"`
|
||||
Status int `gorm:"column:status_id"`
|
||||
Status_id int `gorm:"column:status_id"`
|
||||
Hash string `gorm:"column:torrent_hash"`
|
||||
Date int `gorm:"column:date"`
|
||||
Downloads int `gorm:"column:downloads"`
|
||||
Filesize string `gorm:"column:filesize"`
|
||||
Description []byte `gorm:"column:description"`
|
||||
Statuses Statuses `gorm:"ForeignKey:status_id;AssociationForeignKey:status_id"`
|
||||
Categories Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
|
||||
Sub_Categories Sub_Categories `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
|
||||
}
|
||||
|
@ -77,6 +87,8 @@ type HomeTemplateVariables struct {
|
|||
Query string
|
||||
Status string
|
||||
Category string
|
||||
Sort string
|
||||
Order string
|
||||
QueryRecordCount int
|
||||
TotalRecordCount int
|
||||
}
|
||||
|
@ -94,7 +106,7 @@ func getFeeds() []Feed {
|
|||
Query(
|
||||
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
|
||||
"ORDER BY `timestamp` desc LIMIT 50")
|
||||
if ( err == nil ) {
|
||||
if err == nil {
|
||||
for rows.Next() {
|
||||
item := Feed{}
|
||||
rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
|
||||
|
@ -122,13 +134,15 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
|
|||
var torrents []Torrents
|
||||
var dbQuery *gorm.DB
|
||||
|
||||
if (parameters != nil) { // if there is where parameters
|
||||
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 orderBy == "" {
|
||||
orderBy = "torrent_id DESC"
|
||||
} // Default OrderBy
|
||||
if limit != 0 || offset != 0 { // if limits provided
|
||||
dbQuery = dbQuery.Limit(limit).Offset(offset)
|
||||
}
|
||||
|
@ -182,7 +196,7 @@ func (t *Torrents) toJson() TorrentsJson {
|
|||
res := TorrentsJson{
|
||||
Id: strconv.Itoa(t.Id),
|
||||
Name: html.UnescapeString(t.Name),
|
||||
Status: t.Status,
|
||||
Status: t.Status_id,
|
||||
Hash: t.Hash,
|
||||
Magnet: safe(magnet)}
|
||||
return res
|
||||
|
|
Référencer dans un nouveau ticket