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:
32
internal/model/channel.go
Normal file
32
internal/model/channel.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Channel 频道配置(用于帖子分区)
|
||||
type Channel struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
Name string `json:"name" gorm:"type:varchar(100);not null"`
|
||||
Slug string `json:"slug" gorm:"type:varchar(100);uniqueIndex;not null"`
|
||||
Description string `json:"description" gorm:"type:varchar(255)"`
|
||||
SortOrder int `json:"sort_order" gorm:"default:0;index"`
|
||||
IsActive bool `json:"is_active" gorm:"default:true;index"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||||
}
|
||||
|
||||
func (c *Channel) BeforeCreate(tx *gorm.DB) error {
|
||||
if c.ID == "" {
|
||||
c.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (Channel) TableName() string {
|
||||
return "channels"
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ func autoMigrate(db *gorm.DB) error {
|
||||
// 帖子相关
|
||||
&Post{},
|
||||
&PostImage{},
|
||||
&Channel{},
|
||||
|
||||
// 评论相关
|
||||
&Comment{},
|
||||
@@ -178,6 +179,11 @@ func autoMigrate(db *gorm.DB) error {
|
||||
return fmt.Errorf("failed to seed permissions: %w", err)
|
||||
}
|
||||
|
||||
// 初始化频道配置种子数据
|
||||
if err := seedChannels(db); err != nil {
|
||||
return fmt.Errorf("failed to seed channels: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -315,6 +321,43 @@ func seedPermissions(db *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// seedChannels 初始化频道种子数据(按 slug 幂等)
|
||||
func seedChannels(db *gorm.DB) error {
|
||||
defaultChannels := []Channel{
|
||||
{Name: "跳蚤市场", Slug: "market", Description: "二手交易与闲置发布", SortOrder: 10, IsActive: true},
|
||||
{Name: "课程交流", Slug: "courses", Description: "课程资料、选课与学习讨论", SortOrder: 20, IsActive: true},
|
||||
{Name: "工作实习", Slug: "jobs", Description: "实习、内推与求职信息", SortOrder: 30, IsActive: true},
|
||||
{Name: "考研考公", Slug: "exam", Description: "备考经验、资料与互助", SortOrder: 40, IsActive: true},
|
||||
{Name: "保研出国", Slug: "graduate-abroad", Description: "保研、留学申请与经验分享", SortOrder: 50, IsActive: true},
|
||||
}
|
||||
|
||||
for _, item := range defaultChannels {
|
||||
var existing Channel
|
||||
err := db.Where("slug = ?", item.Slug).First(&existing).Error
|
||||
if err == nil {
|
||||
updates := map[string]any{
|
||||
"name": item.Name,
|
||||
"description": item.Description,
|
||||
"sort_order": item.SortOrder,
|
||||
"is_active": item.IsActive,
|
||||
}
|
||||
if uerr := db.Model(&existing).Updates(updates).Error; uerr != nil {
|
||||
return uerr
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
if cerr := db.Create(&item).Error; cerr != nil {
|
||||
return cerr
|
||||
}
|
||||
}
|
||||
|
||||
zap.L().Info("Seeded channels successfully", zap.Int("count", len(defaultChannels)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseDB 关闭数据库连接
|
||||
func CloseDB(db *gorm.DB) {
|
||||
if sqlDB, err := db.DB(); err == nil {
|
||||
|
||||
@@ -22,7 +22,7 @@ const (
|
||||
type Post struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);index;index:idx_posts_user_status_created,priority:1;not null"`
|
||||
CommunityID string `json:"community_id" gorm:"type:varchar(36);index"`
|
||||
ChannelID *string `json:"channel_id,omitempty" gorm:"type:varchar(36);index"`
|
||||
Title string `json:"title" gorm:"type:varchar(200);not null"`
|
||||
Content string `json:"content" gorm:"type:text;not null"`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user