✨ Editable
This commit is contained in:
@ -83,6 +83,16 @@ func (v *PostTypeContext) GetViaAlias(alias string) (models.Feed, error) {
|
||||
return item, err
|
||||
}
|
||||
|
||||
var attachments []models.Attachment
|
||||
if err := database.C.
|
||||
Model(&models.Attachment{}).
|
||||
Where(v.ColumnName+"_id = ?", item.ID).
|
||||
Scan(&attachments).Error; err != nil {
|
||||
return item, err
|
||||
} else {
|
||||
item.Attachments = attachments
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
@ -96,6 +106,16 @@ func (v *PostTypeContext) Get(id uint, noComments ...bool) (models.Feed, error)
|
||||
return item, err
|
||||
}
|
||||
|
||||
var attachments []models.Attachment
|
||||
if err := database.C.
|
||||
Model(&models.Attachment{}).
|
||||
Where(v.ColumnName+"_id = ?", id).
|
||||
Scan(&attachments).Error; err != nil {
|
||||
return item, err
|
||||
} else {
|
||||
item.Attachments = attachments
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
@ -184,6 +204,41 @@ func (v *PostTypeContext) List(take int, offset int, noReact ...bool) ([]*models
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var attachments []struct {
|
||||
models.Attachment
|
||||
|
||||
PostID uint `json:"post_id"`
|
||||
}
|
||||
|
||||
itemMap := lo.SliceToMap(items, func(item *models.Feed) (uint, *models.Feed) {
|
||||
return item.ID, item
|
||||
})
|
||||
|
||||
idx := lo.Map(items, func(item *models.Feed, index int) uint {
|
||||
return item.ID
|
||||
})
|
||||
|
||||
if err := database.C.
|
||||
Model(&models.Attachment{}).
|
||||
Select(v.ColumnName+"_id as post_id, *").
|
||||
Where(v.ColumnName+"_id IN (?)", idx).
|
||||
Scan(&attachments).Error; err != nil {
|
||||
return items, err
|
||||
}
|
||||
|
||||
list := map[uint][]models.Attachment{}
|
||||
for _, info := range attachments {
|
||||
list[info.PostID] = append(list[info.PostID], info.Attachment)
|
||||
}
|
||||
|
||||
for k, v := range list {
|
||||
if post, ok := itemMap[k]; ok {
|
||||
post.Attachments = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user