From 48eb6397e499096767073b5ee9d0a6f3bb5636d4 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 8 May 2017 21:18:48 +0200 Subject: [PATCH] Cleanup --- model/torrent.go | 9 ++------- service/torrent/torrent.go | 9 ++++++++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/model/torrent.go b/model/torrent.go index b10fda82..21377669 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -4,7 +4,6 @@ import ( "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/util" -// "encoding/json" "html" "html/template" "strconv" @@ -40,11 +39,7 @@ type Torrents struct { Comments []Comment `gorm:"ForeignKey:torrent_id"` } -/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically --------------------------------------------------------------------------------------------------------------- -JSON Models Oject --------------------------------------------------------------------------------------------------------------- -*/ +/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically */ type ApiResultJson struct { Torrents []TorrentsJson `json:"torrents"` @@ -81,7 +76,7 @@ func (t *Torrents) ToJson() TorrentsJson { commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date}) } for _, c := range t.Comments { - commentsJson = append(commentsJson, CommentsJson{Username: (*c.User).Username, Content: template.HTML(c.Content), Date: c.CreatedAt}) + commentsJson = append(commentsJson, CommentsJson{Username: c.User.Username, Content: template.HTML(c.Content), Date: c.CreatedAt}) } res := TorrentsJson{ Id: strconv.FormatUint(uint64(t.Id), 10), diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index 6f729d28..6cca1586 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -45,9 +45,16 @@ func GetFeeds() []model.Feed { func GetTorrentById(id string) (model.Torrents, error) { var torrent model.Torrents - if db.ORM.Where("torrent_id = ?", id).Preload("Comments").Preload("OldComments").Find(&torrent).RecordNotFound() { + if db.ORM.Where("torrent_id = ?", id). + Preload("Comments").Preload("OldComments"). + Find(&torrent).RecordNotFound() { return torrent, errors.New("Article is not found.") } + // .Preload("Comments.User") doesn't work + for i := range torrent.Comments { + torrent.Comments[i].User = new(model.User) + db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User) + } return torrent, nil }