feat(channel): integrate channel functionality into post management
- 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:
54
internal/repository/channel_repo.go
Normal file
54
internal/repository/channel_repo.go
Normal 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
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ func (r *PostRepository) Delete(id string) error {
|
||||
|
||||
// List 分页获取帖子列表
|
||||
// includePending=true 时,仅在指定 userID 下额外返回 pending(用于作者查看自己待审核帖子)
|
||||
func (r *PostRepository) List(page, pageSize int, userID string, includePending bool) ([]*model.Post, int64, error) {
|
||||
func (r *PostRepository) List(page, pageSize int, userID string, includePending bool, channelID *string) ([]*model.Post, int64, error) {
|
||||
var posts []*model.Post
|
||||
var total int64
|
||||
|
||||
@@ -183,6 +183,9 @@ func (r *PostRepository) List(page, pageSize int, userID string, includePending
|
||||
} else {
|
||||
query = query.Where("status = ?", model.PostStatusPublished)
|
||||
}
|
||||
if channelID != nil && *channelID != "" {
|
||||
query = query.Where("channel_id = ?", *channelID)
|
||||
}
|
||||
|
||||
query.Count(&total)
|
||||
|
||||
@@ -849,7 +852,7 @@ func (r *PostRepository) UpdateFeatureStatus(postID string, isFeatured bool) err
|
||||
|
||||
// GetPostsByCursor 游标分页获取帖子列表
|
||||
// includePending=true 时,仅在指定 userID 下额外返回 pending(用于作者查看自己待审核帖子)
|
||||
func (r *PostRepository) GetPostsByCursor(ctx context.Context, userID string, includePending bool, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Post], error) {
|
||||
func (r *PostRepository) GetPostsByCursor(ctx context.Context, userID string, includePending bool, channelID *string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Post], error) {
|
||||
db := r.getDB(ctx).WithContext(ctx)
|
||||
|
||||
// 构建基础查询
|
||||
@@ -866,6 +869,9 @@ func (r *PostRepository) GetPostsByCursor(ctx context.Context, userID string, in
|
||||
} else {
|
||||
query = query.Where("status = ?", model.PostStatusPublished)
|
||||
}
|
||||
if channelID != nil && *channelID != "" {
|
||||
query = query.Where("channel_id = ?", *channelID)
|
||||
}
|
||||
|
||||
// 使用游标构建器
|
||||
builder := cursor.NewBuilder(query, cursor.SortByCreatedAtDesc).
|
||||
|
||||
Reference in New Issue
Block a user