basic sort
Cette révision appartient à :
Parent
b485506716
révision
ccc727bfd2
3 fichiers modifiés avec 82 ajouts et 49 suppressions
13
index.html
13
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>
|
||||
|
@ -91,7 +100,7 @@
|
|||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
|
||||
<div class="container">
|
||||
<table class="table">
|
||||
<tr>
|
||||
|
|
46
main.go
46
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,39 +160,39 @@ 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",
|
||||
Link: &feeds.Link{Href: "https://nyaa.pantsu.cat/"},
|
||||
Created: created_as_time,
|
||||
Title: "Nyaa Pantsu",
|
||||
Link: &feeds.Link{Href: "https://nyaa.pantsu.cat/"},
|
||||
Created: created_as_time,
|
||||
}
|
||||
feed.Items = []*feeds.Item{}
|
||||
feed.Items = make( []*feeds.Item, len(torrents))
|
||||
feed.Items = make([]*feeds.Item, len(torrents))
|
||||
|
||||
for i, _ := range torrents {
|
||||
timestamp_as_time, err := time.Parse("2006-01-02 15:04:05", torrents[i].Timestamp)
|
||||
if err == nil {
|
||||
feed.Items[i] = &feeds.Item{
|
||||
feed.Items[i] = &feeds.Item{
|
||||
// need a torrent view first
|
||||
//Id: URL + torrents[i].Hash,
|
||||
Title: torrents[i].Name,
|
||||
Link: &feeds.Link{Href: string(torrents[i].Magnet)},
|
||||
Title: torrents[i].Name,
|
||||
Link: &feeds.Link{Href: string(torrents[i].Magnet)},
|
||||
Description: "",
|
||||
Created: timestamp_as_time,
|
||||
Updated: timestamp_as_time,
|
||||
Created: timestamp_as_time,
|
||||
Updated: timestamp_as_time,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rss, err := feed.ToRss()
|
||||
if err == nil {
|
||||
w.Write( []byte( rss ) )
|
||||
w.Write([]byte(rss))
|
||||
} else {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
|
72
models.go
72
models.go
|
@ -10,35 +10,45 @@ import (
|
|||
)
|
||||
|
||||
type Feed struct {
|
||||
Id int
|
||||
Name string
|
||||
Hash string
|
||||
Magnet string
|
||||
Timestamp string
|
||||
Id int
|
||||
Name string
|
||||
Hash string
|
||||
Magnet string
|
||||
Timestamp string
|
||||
}
|
||||
|
||||
type Categories struct {
|
||||
Category_id int
|
||||
Category_name string
|
||||
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
|
||||
Sub_Categories []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"`
|
||||
Category_id int
|
||||
Category_name string
|
||||
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
|
||||
Sub_Categories []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -93,15 +105,15 @@ func getFeeds() []Feed {
|
|||
rows, err := db.DB().
|
||||
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 ) {
|
||||
"ORDER BY `timestamp` desc LIMIT 50")
|
||||
if err == nil {
|
||||
for rows.Next() {
|
||||
item := Feed{}
|
||||
rows.Scan( &item.Id, &item.Name, &item.Hash, &item.Timestamp )
|
||||
rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
|
||||
magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(item.Hash) + "&dn=" + item.Name + trackers
|
||||
item.Magnet = magnet
|
||||
// memory hog
|
||||
result = append( result, item )
|
||||
result = append(result, item)
|
||||
}
|
||||
rows.Close()
|
||||
}
|
||||
|
@ -121,23 +133,25 @@ func getTorrentById(id string) (Torrents, error) {
|
|||
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) []Torrents {
|
||||
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)
|
||||
}
|
||||
dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
|
||||
dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
|
||||
return torrents
|
||||
}
|
||||
|
||||
/* Functions to simplify the get parameters of the main function
|
||||
*
|
||||
/* 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 {
|
||||
|
@ -153,7 +167,7 @@ func getTorrentsDB(parameters WhereParams) []Torrents {
|
|||
/* Function to get all torrents
|
||||
*/
|
||||
|
||||
func getAllTorrentsOrderBy(orderBy string, limit int, offset int) [] Torrents {
|
||||
func getAllTorrentsOrderBy(orderBy string, limit int, offset int) []Torrents {
|
||||
return getTorrentsOrderBy(nil, orderBy, limit, 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
|
||||
|
@ -198,4 +212,4 @@ func createWhereParams(conditions string, params ...string) WhereParams {
|
|||
return whereParams
|
||||
}
|
||||
|
||||
/* Complete the functions when necessary... */
|
||||
/* Complete the functions when necessary... */
|
||||
|
|
Référencer dans un nouveau ticket