- 新增附件模型与上传服务,拆分头像上传/管理逻辑 - 重构用户资料接口,移除直接修改头像字段,改用专用上传接口 - 添加站点公开设置API与管理员外观配置页 - 全局替换hover背景为accent-soft,优化交互一致性 - 完善头像组件,支持图片加载失败回退与src更新重置 - 新增用户状态事件总线,解决同页头像更新不及时问题 - 调整依赖包顺序与CI依赖版本 - 重构板块列表接口,新增帖子计数 - 迁移账号设置到用户主页tab,合并重复代码
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package service
|
||
|
||
import (
|
||
"github.com/freefire/jiang13-bbs/model"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// BoardService 板块服务
|
||
type BoardService struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
func NewBoardService(db *gorm.DB) *BoardService {
|
||
return &BoardService{db: db}
|
||
}
|
||
|
||
// BoardWithCount 板块及其已发布帖子计数(Board 字段被 encoding/json 展平)
|
||
type BoardWithCount struct {
|
||
model.Board
|
||
PostCount int64 `json:"post_count"`
|
||
}
|
||
|
||
// List 获取所有板块(按 sort_order 排序),附带各板块已发布帖子数(板块目录页展示用)
|
||
func (s *BoardService) List() ([]BoardWithCount, error) {
|
||
var boards []model.Board
|
||
if err := s.db.Order("sort_order ASC, id ASC").Find(&boards).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
type countRow struct {
|
||
BoardID uint
|
||
Cnt int64
|
||
}
|
||
var counts []countRow
|
||
if err := s.db.Model(&model.Post{}).
|
||
Select("board_id, COUNT(*) AS cnt").
|
||
Where("status = ?", model.ContentStatusPublished).
|
||
Group("board_id").Scan(&counts).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
countMap := make(map[uint]int64, len(counts))
|
||
for _, c := range counts {
|
||
countMap[c.BoardID] = c.Cnt
|
||
}
|
||
items := make([]BoardWithCount, 0, len(boards))
|
||
for _, b := range boards {
|
||
items = append(items, BoardWithCount{Board: b, PostCount: countMap[b.ID]})
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
// Get 根据 ID 获取板块
|
||
func (s *BoardService) Get(id uint) (*model.Board, error) {
|
||
var board model.Board
|
||
if err := s.db.First(&board, id).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return &board, nil
|
||
}
|