mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Enhance comment functionality with image support and validation.
This commit is contained in:
@@ -13,21 +13,29 @@ type Comment struct {
|
||||
UserID uint `gorm:"not null"`
|
||||
User User `gorm:"foreignKey:UserID"`
|
||||
Resource Resource `gorm:"foreignKey:ResourceID"`
|
||||
Images []Image `gorm:"many2many:comment_images;"`
|
||||
}
|
||||
|
||||
type CommentView struct {
|
||||
ID uint `json:"id"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
User UserView `json:"user"`
|
||||
ID uint `json:"id"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
User UserView `json:"user"`
|
||||
Images []ImageView `json:"images"`
|
||||
}
|
||||
|
||||
func (c *Comment) ToView() *CommentView {
|
||||
imageViews := make([]ImageView, 0, len(c.Images))
|
||||
for _, img := range c.Images {
|
||||
imageViews = append(imageViews, img.ToView())
|
||||
}
|
||||
|
||||
return &CommentView{
|
||||
ID: c.ID,
|
||||
Content: c.Content,
|
||||
CreatedAt: c.CreatedAt,
|
||||
User: c.User.ToView(),
|
||||
Images: imageViews,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,14 +45,21 @@ type CommentWithResourceView struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Resource ResourceView `json:"resource"`
|
||||
User UserView `json:"user"`
|
||||
Images []ImageView `json:"images"`
|
||||
}
|
||||
|
||||
func (c *Comment) ToViewWithResource() *CommentWithResourceView {
|
||||
imageViews := make([]ImageView, 0, len(c.Images))
|
||||
for _, img := range c.Images {
|
||||
imageViews = append(imageViews, img.ToView())
|
||||
}
|
||||
|
||||
return &CommentWithResourceView{
|
||||
ID: c.ID,
|
||||
Content: c.Content,
|
||||
CreatedAt: c.CreatedAt,
|
||||
Resource: c.Resource.ToView(),
|
||||
User: c.User.ToView(),
|
||||
Images: imageViews,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user