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 }