feat(channel): integrate channel functionality into post management
All checks were successful
Build Backend / build (push) Successful in 19m14s
Build Backend / build-docker (push) Successful in 1m28s

- Added Channel support in Post model, including ChannelID field in PostRequest and PostResponse DTOs.
- Updated PostService and PostRepository to handle channel-specific logic for creating and listing posts.
- Introduced ChannelRepository and ChannelService, along with corresponding handlers for managing channels.
- Enhanced router to include routes for channel management and public channel listing.
- Seeded default channels in the database during initialization.
This commit is contained in:
lafay
2026-03-24 22:27:53 +08:00
parent 15f0f1605e
commit fc0d6ff19d
19 changed files with 416 additions and 42 deletions

View File

@@ -0,0 +1,54 @@
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
}