Skip to content

Commit e4b76fe

Browse files
fix type data encoding
1 parent e577bb5 commit e4b76fe

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

internal/models/post.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ type PostSummary struct {
126126
Title string `json:"title" gorm:"column:title"`
127127
Content string `json:"content" gorm:"column:content"`
128128
Type string `json:"type" gorm:"column:type"`
129-
TypeData PostTypeData `json:"type_data,omitempty" gorm:"type:jsonb"`
129+
TypeData interface{} `json:"type_data,omitempty"`
130130
IsPinned bool `json:"is_pinned" gorm:"column:is_pinned"`
131131
LikesCount int `json:"likes_count" gorm:"column:likes_count"`
132132
CommentsCount int `json:"comments_count" gorm:"column:comments_count"`

internal/repository/post.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package repository
22

33
import (
4+
"encoding/json"
45
"strconv"
56
"time"
67

@@ -130,7 +131,6 @@ func (r *postRepository) ListPostSummaries(clubID uint, userID *uint, limit, off
130131
Title: rrow.Title,
131132
Content: rrow.Content,
132133
Type: rrow.Type,
133-
TypeData: models.PostTypeData(rrow.TypeData),
134134
IsPinned: rrow.IsPinned,
135135
LikesCount: rrow.LikesCount,
136136
CommentsCount: rrow.CommentsCount,
@@ -141,6 +141,17 @@ func (r *postRepository) ListPostSummaries(clubID uint, userID *uint, limit, off
141141
UpdatedAt: rrow.UpdatedAt,
142142
}
143143

144+
if rrow.TypeData != "" && rrow.TypeData != "null" {
145+
var typeDataInterface interface{}
146+
if err := json.Unmarshal([]byte(rrow.TypeData), &typeDataInterface); err == nil {
147+
ps.TypeData = typeDataInterface
148+
} else {
149+
ps.TypeData = nil
150+
}
151+
} else {
152+
ps.TypeData = nil
153+
}
154+
144155
if userID != nil {
145156
hasLiked, err := r.HasUserLiked(*userID, rrow.ID)
146157
if err == nil {

0 commit comments

Comments
 (0)