- Refactored repository structures to use interfaces instead of concrete types, enhancing flexibility and testability. - Updated various repository methods to accept interfaces, allowing for better dependency management. - Modified wire generation to accommodate new repository interfaces, ensuring proper service injection throughout the application. - Enhanced handler methods to utilize DTOs for filtering and data handling, improving code clarity and maintainability.
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrot_bbs/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ChannelRepository 频道配置仓储接口
|
|
type ChannelRepository interface {
|
|
ListActive() ([]*model.Channel, error)
|
|
ListAll() ([]*model.Channel, error)
|
|
Create(channel *model.Channel) error
|
|
Update(channel *model.Channel) error
|
|
GetByID(id string) (*model.Channel, error)
|
|
Delete(id string) error
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|