- Updated ChannelService to include caching for channel lists, improving performance and reducing database load. - Introduced cache invalidation methods to ensure channel list consistency after modifications. - Modified ChannelRepository to remove unused ListByIDs method, streamlining the repository interface. - Updated wire generation to inject cache into ChannelService for enhanced functionality. - Added new cache key constants for channel-related data management.
55 lines
1.2 KiB
Go
55 lines
1.2 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
|
|
}
|
|
|
|
func (r *ChannelRepository) Delete(id string) error {
|
|
return r.db.Delete(&model.Channel{}, "id = ?", id).Error
|
|
}
|
|
|