Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

implemented comments and display

Cette révision appartient à :
Andrew Zhao 2017-05-07 19:06:11 -07:00
Parent 18a470b87f
révision 2e3c45cbc7
5 fichiers modifiés avec 67 ajouts et 42 suppressions

Voir le fichier

@ -6,13 +6,14 @@ import (
// Comment is a comment model.
type Comment struct {
Id int `json:"id"`
Content string `json:"content"`
UserId int `json:"userId"`
TorrentId int
Id int `json:"id"`
Content string `json:"content"`
UserId int `json:"userId"`
Username string `json:"username"` // this is duplicate but it'll be faster rite?
TorrentId int
// LikingCount int `json:"likingCount"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time `json:"deletedAt"`
User User `json:"user"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time `json:"deletedAt"`
User User `json:"user"`
}

Voir le fichier

@ -21,17 +21,18 @@ type Feed struct {
}
type Torrents struct {
Id int `gorm:"column:torrent_id;primary_key"`
Name string `gorm:"column:torrent_name"`
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"`
Downloads int `gorm:"column:downloads"`
Filesize int64 `gorm:"column:filesize"`
Description string `gorm:"column:description"`
Comments []byte `gorm:"column:comments"`
Id int `gorm:"column:torrent_id;primary_key"`
Name string `gorm:"column:torrent_name"`
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"`
Downloads int `gorm:"column:downloads"`
Filesize int64 `gorm:"column:filesize"`
Description string `gorm:"column:description"`
OldComments []byte `gorm:"column:comments"`
Comments []Comment `gorm:"ForeignKey:TorrentId"`
}
/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
@ -41,12 +42,12 @@ JSON Models Oject
*/
type ApiResultJson struct {
Torrents []TorrentsJson `json:"torrents"`
QueryRecordCount int `json:"queryRecordCount"`
TotalRecordCount int `json:"totalRecordCount"`
Torrents []TorrentsJson `json:"torrents"`
QueryRecordCount int `json:"queryRecordCount"`
TotalRecordCount int `json:"totalRecordCount"`
}
type CommentsJson struct {
type OldCommentsJson struct {
C template.HTML `json:"c"`
Us string `json:"us"`
Un string `json:"un"`
@ -56,26 +57,50 @@ type CommentsJson struct {
ID string `json:"id"`
}
type CommentsJson struct {
Content template.HTML `json:"content"`
Username string `json:"username"`
}
type TorrentsJson struct {
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"`
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 */
func (t *Torrents) ToJson() TorrentsJson {
magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...)
b := []CommentsJson{}
_ = json.Unmarshal([]byte(t.Comments), &b)
offset := 0
var commentsJson []CommentsJson
if len(t.OldComments) != 0 {
b := []OldCommentsJson{}
err := json.Unmarshal([]byte(t.OldComments), &b)
if err == nil {
commentsJson = make([]CommentsJson, len(t.Comments)+len(b))
offset = len(b)
for i, commentJson := range b {
commentsJson[i] = CommentsJson{Content: commentJson.C,
Username: commentJson.Un}
}
} else {
commentsJson = make([]CommentsJson, len(t.Comments))
}
} else {
commentsJson = make([]CommentsJson, len(t.Comments))
}
for i, comment := range t.Comments {
commentsJson[i+offset] = CommentsJson{Content: template.HTML(comment.Content), Username: comment.Username}
}
res := TorrentsJson{
Id: strconv.Itoa(t.Id),
Name: html.UnescapeString(t.Name),
@ -84,7 +109,7 @@ func (t *Torrents) ToJson() TorrentsJson {
Date: time.Unix(t.Date, 0).Format(time.RFC3339),
Filesize: util.FormatFilesize2(t.Filesize),
Description: template.HTML(t.Description),
Comments: b,
Comments: commentsJson,
Sub_Category: strconv.Itoa(t.Sub_Category),
Category: strconv.Itoa(t.Category),
Magnet: util.Safe(magnet)}

Voir le fichier

@ -35,8 +35,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
Hash: uploadForm.Infohash,
Date: time.Now().Unix(),
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
Description: uploadForm.Description,
Comments: []byte{}}
Description: uploadForm.Description}
db.ORM.Create(&torrent)
fmt.Printf("%+v\n", torrent)
url, err := Router.Get("view_torrent").URL("id", strconv.Itoa(torrent.Id))

Voir le fichier

@ -45,7 +45,7 @@ func GetFeeds() []model.Feed {
func GetTorrentById(id string) (model.Torrents, error) {
var torrent model.Torrents
if db.ORM.Where("torrent_id = ?", id).Find(&torrent).RecordNotFound() {
if db.ORM.Where("torrent_id = ?", id).Preload("Comments").Find(&torrent).RecordNotFound() {
return torrent, errors.New("Article is not found.")
}

Voir le fichier

@ -49,8 +49,8 @@
</tr>
{{ range .Comments }}
<tr class="comment-row">
<td>{{.Un}}</td>
<td>{{.C}}</td>
<td>{{.Username}}</td>
<td>{{.Content}}</td>
</tr>
{{end}}
</table>