Files
backend/internal/repository/channel_repo.go
lafay 7c14cf5bab feat(post): enhance post handling with channel integration
- Updated PostHandler to include channel information in post responses.
- Introduced PostChannelBrief DTO to represent channel details associated with posts.
- Modified post conversion functions to support channel data, ensuring proper mapping of channel IDs to channel names.
- Enhanced ChannelRepository and ChannelService to facilitate batch retrieval of channels by IDs.
- Updated wire generation to inject channel service into post handler for improved functionality.
2026-03-25 00:58:41 +08:00

68 lines
1.6 KiB
Go

package repository
import (
"carrot_bbs/internal/model"
"gorm.io/gorm"
)
// ChannelRepository 频道配置仓储
type ChannelRepository struct {
db *gorm.DB
}
func NewChannelRepository(db *gorm.DB) *ChannelRepository {
return &ChannelRepository{db: db}
}
func (r *ChannelRepository) ListActive() ([]*model.Channel, error) {
var channels []*model.Channel
err := r.db.
Where("is_active = ?", true).
Order("sort_order ASC, created_at ASC").
Find(&channels).Error
return channels, err
}
func (r *ChannelRepository) ListAll() ([]*model.Channel, error) {
var channels []*model.Channel
err := r.db.
Order("sort_order ASC, created_at ASC").
Find(&channels).Error
return channels, err
}
func (r *ChannelRepository) Create(channel *model.Channel) error {
return r.db.Create(channel).Error
}
func (r *ChannelRepository) Update(channel *model.Channel) error {
return r.db.Save(channel).Error
}
func (r *ChannelRepository) GetByID(id string) (*model.Channel, error) {
var channel model.Channel
if err := r.db.First(&channel, "id = ?", id).Error; err != nil {
return nil, err
}
return &channel, nil
}
// ListByIDs 按 ID 列表查询频道(用于帖子列表批量填充 channel 名称)
func (r *ChannelRepository) ListByIDs(ids []string) ([]*model.Channel, error) {
if len(ids) == 0 {
return nil, nil
}
var list []*model.Channel
err := r.db.Where("id IN ?", ids).Find(&list).Error
if err != nil {
return nil, err
}
return list, nil
}
func (r *ChannelRepository) Delete(id string) error {
return r.db.Delete(&model.Channel{}, "id = ?", id).Error
}