Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

Stop using categories & status DB tables

Also fixes the incorrect JSON keys in API responses
Cette révision appartient à :
sfan5 2017-05-06 23:46:53 +02:00
Parent 565b71d7e4
révision b7a62c2f20
10 fichiers modifiés avec 34 ajouts et 87 suppressions

Voir le fichier

@ -30,7 +30,7 @@ func GormInit(conf *config.Config) (*gorm.DB, error) {
if config.Environment == "DEVELOPMENT" {
db.LogMode(true)
// db.DropTable(&model.User{}, "UserFollower")
db.AutoMigrate(&model.Torrents{}, &model.Categories{}, &model.Sub_Categories{}, &model.Statuses{})
db.AutoMigrate(&model.Torrents{})
// db.AutoMigrate(&model.User{}, &model.Role{}, &model.Connection{}, &model.Language{}, &model.Article{}, &model.Location{}, &model.Comment{}, &model.File{})
// db.Model(&model.User{}).AddIndex("idx_user_token", "token")
@ -54,4 +54,4 @@ func GormInit(conf *config.Config) (*gorm.DB, error) {
// handler.Setup(&relation, "users_followers", m1Type, m2Type)
return db, err
}
}

Voir le fichier

@ -20,31 +20,11 @@ type Feed struct {
Timestamp string
}
type Categories struct {
Id int `gorm:"column:category_id"`
Name string `gorm:"column:category_name"`
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
Sub_Categories []Sub_Categories `gorm:"ForeignKey:parent_id;AssociationForeignKey:category_id"`
}
type Sub_Categories struct {
Id int `gorm:"column:sub_category_id"`
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"`
}
type Statuses struct {
Status_id int
Status_name string
Torrents []Torrents `gorm:"ForeignKey:status_id;AssociationForeignKey:status_id"`
}
type Torrents struct {
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"`
Category int `gorm:"column:category_id"`
Sub_Category int `gorm:"column:sub_category_id"`
Status int `gorm:"column:status_id"`
Hash string `gorm:"column:torrent_hash"`
Date int64 `gorm:"column:date"`
@ -52,9 +32,6 @@ type Torrents struct {
Filesize int64 `gorm:"column:filesize"`
Description []byte `gorm:"column:description"`
Comments []byte `gorm:"column:comments"`
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"`
}
/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
@ -63,17 +40,10 @@ JSON Models Oject
--------------------------------------------------------------------------------------------------------------
*/
type CategoryJson struct {
Id string `json: "id"`
Name string `json: "category"`
Torrents []TorrentsJson `json: "torrents"`
QueryRecordCount int `json: "queryRecordCount"`
TotalRecordCount int `json: "totalRecordCount"`
}
type SubCategoryJson struct {
Id string `json: "id"`
Name string `json: "category"`
type ApiResultJson struct {
Torrents []TorrentsJson `json:"torrents"`
QueryRecordCount int `json:"queryRecordCount"`
TotalRecordCount int `json:"totalRecordCount"`
}
type CommentsJson struct {
@ -87,17 +57,17 @@ type CommentsJson struct {
}
type TorrentsJson struct {
Id string `json: "id"` // Is there a need to put the ID?
Name string `json: "name"`
Status int `json: "status"`
Hash string `json: "hash"`
Date string `json: "date"`
Filesize string `json: "filesize"`
Description template.HTML `json: "description"`
Comments []CommentsJson `json: "comments"`
Sub_Category SubCategoryJson `json: "sub_category"`
Category CategoryJson `json: "category"`
Magnet template.URL `json: "magnet"`
Id string `json:"id"`
Name string `json:"name"`
Status int `json:"status"`
Hash string `json:"hash"`
Date string `json:"date"`
Filesize string `json:"filesize"`
Description template.HTML `json:"description"`
Comments []CommentsJson `json:"comments"`
Sub_Category string `json:"sub_category"`
Category string `json:"category"`
Magnet template.URL `json:"magnet"`
}
/* Model Conversion to Json */
@ -115,23 +85,11 @@ func (t *Torrents) ToJson() TorrentsJson {
Filesize: util.FormatFilesize(t.Filesize),
Description: template.HTML(util.UnZlib(t.Description)),
Comments: b,
Sub_Category: t.Sub_Categories.ToJson(),
Category: t.Categories.ToJson(),
Sub_Category: strconv.Itoa(t.Sub_Category),
Category: strconv.Itoa(t.Category),
Magnet: util.Safe(magnet)}
return res
}
func (c *Sub_Categories) ToJson() SubCategoryJson {
return SubCategoryJson{
Id: strconv.Itoa(c.Id),
Name: html.UnescapeString(c.Name)}
}
func (c *Categories) ToJson() CategoryJson {
return CategoryJson{
Id: strconv.Itoa(c.Id),
Name: html.UnescapeString(c.Name)}
}
/* Complete the functions when necessary... */

Voir le fichier

@ -16,7 +16,7 @@ func ApiHandler(w http.ResponseWriter, r *http.Request) {
page := vars["page"]
pagenum, _ := strconv.Atoi(html.EscapeString(page))
b := model.CategoryJson{Torrents: []model.TorrentsJson{}}
b := model.ApiResultJson{Torrents: []model.TorrentsJson{}}
maxPerPage := 50
nbTorrents := 0
@ -41,7 +41,7 @@ func ApiViewHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
b := model.CategoryJson{Torrents: []model.TorrentsJson{}}
b := model.ApiResultJson{Torrents: []model.TorrentsJson{}}
torrent, err := torrentService.GetTorrentById(id)
res := torrent.ToJson()
@ -56,4 +56,4 @@ func ApiViewHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}

Voir le fichier

@ -39,7 +39,7 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
}
navigationTorrents := templates.Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), templates.NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)}
htv := HomeTemplateVariables{b, templates.NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)}
err := homeTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {

Voir le fichier

@ -7,7 +7,6 @@ import (
"strconv"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/templates"
"github.com/ewhal/nyaa/util/search"
"github.com/gorilla/mux"
@ -43,7 +42,7 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) {
search_param.Order,
false,
}
htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)}
htv := HomeTemplateVariables{b, searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)}
err := searchTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {

Voir le fichier

@ -31,7 +31,6 @@ type ViewTemplateVariables struct {
type HomeTemplateVariables struct {
ListTorrents []model.TorrentsJson
ListCategories []model.Categories
Search templates.SearchForm
Navigation templates.Navigation
URL *url.URL // For parsing Url in templates

Voir le fichier

@ -43,7 +43,7 @@ func GetFeeds() []model.Feed {
func GetTorrentById(id string) (model.Torrents, error) {
var torrent model.Torrents
if db.ORM.Where("torrent_id = ?", id).Preload("Sub_Categories").Find(&torrent).RecordNotFound() {
if db.ORM.Where("torrent_id = ?", id).Find(&torrent).RecordNotFound() {
return torrent, errors.New("Article is not found.")
}
@ -69,7 +69,7 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
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).Find(&torrents)
return torrents, count
}
@ -104,18 +104,6 @@ func GetAllTorrentsDB() ([]model.Torrents, int) {
return GetTorrentsOrderBy(nil, "", 0, 0)
}
/* Function to get all categories with/without torrents (bool)
*/
func GetAllCategories(populatedWithTorrents bool) []model.Categories {
var categories []model.Categories
if populatedWithTorrents {
db.ORM.Preload("Torrents").Preload("Sub_Categories").Find(&categories)
} else {
db.ORM.Preload("Sub_Categories").Find(&categories)
}
return categories
}
func CreateWhereParams(conditions string, params ...string) WhereParams {
whereParams := WhereParams{}
whereParams.Conditions = conditions

Voir le fichier

@ -53,6 +53,9 @@
<h2>Why is your shit written in Go?</h2>
<p>It's the author's favorite programming language.</p>
<br />
<img src="https://my.mixtape.moe/omrskw.png">
<br />
<h2>nyaa.pantsu.cat and sukebei.pantsu.cat do not host any files.</h2>
</div>

Voir le fichier

@ -24,8 +24,8 @@
{{if eq .Status 4}}aplus{{end}}">
<!-- forced width because the <td> gets bigger randomly otherwise -->
<td style="width:80px">
<a href="{{$.URL.Parse (printf "/search?c=%s_%s" .Category.Id .Sub_Category.Id) }}">
<img src="{{$.URL.Parse (printf "/img/torrents/%s.png" .Sub_Category.Id) }}">
<a href="{{$.URL.Parse (printf "/search?c=%s_%s" .Category .Sub_Category) }}">
<img src="{{$.URL.Parse (printf "/img/torrents/%s.png" .Sub_Category) }}">
</a>
</td>
<td class="name">

Voir le fichier

@ -11,7 +11,7 @@
<td>Name</td>
<td>
{{.Name}}
<img style="float:right" src="{{$.URL.Parse (printf "/img/torrents/%s.png" .Sub_Category.Id) }}">
<img style="float:right" src="{{$.URL.Parse (printf "/img/torrents/%s.png" .Sub_Category) }}">
</td>
</tr>
<tr>