From 64fc5b394094173c86c408d9e0a23f6d3605a26a Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 8 May 2017 20:07:25 +0200 Subject: [PATCH] Make comments work --- model/comment.go | 8 +++---- model/torrent.go | 43 +++++++++----------------------------- service/torrent/torrent.go | 7 +++---- 3 files changed, 17 insertions(+), 41 deletions(-) diff --git a/model/comment.go b/model/comment.go index 76307e16..1d64a90d 100644 --- a/model/comment.go +++ b/model/comment.go @@ -12,8 +12,8 @@ type Comment struct { CreatedAt time.Time `gorm:"column:created_at"` UpdatedAt time.Time `gorm:"column:updated_at"` - Torrent *Torrents `gorm:"ForeignKey:TorrentId"` - User *User `gorm:"ForeignKey:UserId"` + Torrent *Torrents `gorm:"ForeignKey:torrent_id"` + User *User `gorm:"ForeignKey:user_id"` } type OldComment struct { @@ -22,10 +22,10 @@ type OldComment struct { Content string `gorm:"column:content"` Date time.Time `gorm:"column:date"` - Torrent *Torrents `gorm:"ForeignKey:TorrentId"` + Torrent *Torrents `gorm:"ForeignKey:torrent_id"` } func (c OldComment) TableName() string { - // cba to renamed this in the db + // cba to rename this in the db return "comments_old" } diff --git a/model/torrent.go b/model/torrent.go index 4cc5597d..b10fda82 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -35,9 +35,9 @@ type Torrents struct { Description string `gorm:"column:description"` WebsiteLink string `gorm:"column:website_link"` - Uploader *User `gorm:"ForeignKey:UploaderId"` - OldComments []OldComment `gorm:"ForeignKey:Id"` - Comments []Comment `gorm:"ForeignKey:Id"` + Uploader *User `gorm:"ForeignKey:uploader"` + OldComments []OldComment `gorm:"ForeignKey:torrent_id"` + Comments []Comment `gorm:"ForeignKey:torrent_id"` } /* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically @@ -52,19 +52,10 @@ type ApiResultJson struct { TotalRecordCount int `json:"totalRecordCount"` } -type OldCommentsJson struct { - C template.HTML `json:"c"` - Us string `json:"us"` - Un string `json:"un"` - UI int `json:"ui"` - T int `json:"t"` - Av string `json:"av"` - ID string `json:"id"` -} - type CommentsJson struct { - Content template.HTML `json:"content"` Username string `json:"username"` + Content template.HTML `json:"content"` + Date time.Time `json:"date"` } type TorrentsJson struct { @@ -85,27 +76,13 @@ type TorrentsJson struct { func (t *Torrents) ToJson() TorrentsJson { magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...) - //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 _, c := range t.OldComments { + 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}) } - for i, comment := range t.Comments { - commentsJson[i+offset] = CommentsJson{Content: template.HTML(comment.Content), Username: comment.Username} - }*/ res := TorrentsJson{ Id: strconv.FormatUint(uint64(t.Id), 10), Name: html.UnescapeString(t.Name), diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index eb9a2309..876d09da 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -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).Preload("Comments").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.") } @@ -55,11 +55,10 @@ func GetTorrentById(id string) (model.Torrents, error) { func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) ([]model.Torrents, int) { var torrents []model.Torrents var count int - conditions := "torrent_hash IS NOT NULL" // filter out broken entries + conditions := "" if strings.HasPrefix(orderBy, "filesize") { // torrents w/ NULL filesize fuck up the sorting on postgres - // TODO: fix this properly - conditions += " AND filesize IS NOT NULL" + conditions = "filesize IS NOT NULL" } var params []interface{}