feat: 公告系统 — Phase 2 完成
- Model/Repo/Service/Controller 全栈实现 - CRUD + ListActive(生效时间范围过滤) - 管理后台独立页面 /admin/announcements(列表+弹窗CRUD) - 前台首页 Hero 下方公告栏(info/success/warning/error 四色) - Moderator+ 权限可管理公告
This commit is contained in:
102
internal/controller/admin/announcement_controller.go
Normal file
102
internal/controller/admin/announcement_controller.go
Normal file
@ -0,0 +1,102 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"metazone.cc/mce/internal/common"
|
||||
"metazone.cc/mce/internal/model"
|
||||
"metazone.cc/mce/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AnnouncementController 公告管理控制器
|
||||
type AnnouncementController struct {
|
||||
svc announcementUseCase
|
||||
}
|
||||
|
||||
type announcementUseCase interface {
|
||||
Create(a *model.Announcement) error
|
||||
Update(a *model.Announcement) error
|
||||
Delete(id uint) error
|
||||
List(offset, limit int) ([]model.Announcement, int64, error)
|
||||
ListActive() ([]model.Announcement, error)
|
||||
}
|
||||
|
||||
// NewAnnouncementController 构造函数
|
||||
func NewAnnouncementController(svc announcementUseCase) *AnnouncementController {
|
||||
return &AnnouncementController{svc: svc}
|
||||
}
|
||||
|
||||
// Page 公告管理页面
|
||||
func (ac *AnnouncementController) Page(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "admin/announcements/index.html", common.BuildAdminPageData(c, gin.H{
|
||||
"Title": "公告管理",
|
||||
"ExtraCSS": "/admin/static/css/announcements.css",
|
||||
"ExtraJS": "/admin/static/js/announcements.js",
|
||||
"Types": service.AnnouncementTypes,
|
||||
}))
|
||||
}
|
||||
|
||||
// List 公告列表 API
|
||||
func (ac *AnnouncementController) List(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
list, total, err := ac.svc.List(offset, pageSize)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
}
|
||||
common.Ok(c, gin.H{"list": list, "total": total})
|
||||
}
|
||||
|
||||
// Create 创建公告 API
|
||||
func (ac *AnnouncementController) Create(c *gin.Context) {
|
||||
var req model.Announcement
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.Error(c, http.StatusBadRequest, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := ac.svc.Create(&req); err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "创建失败")
|
||||
return
|
||||
}
|
||||
common.OkMessage(c, "公告已创建")
|
||||
}
|
||||
|
||||
// Update 更新公告 API
|
||||
func (ac *AnnouncementController) Update(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusBadRequest, "无效的 ID")
|
||||
return
|
||||
}
|
||||
var req model.Announcement
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.Error(c, http.StatusBadRequest, "参数错误")
|
||||
return
|
||||
}
|
||||
req.ID = uint(id)
|
||||
if err := ac.svc.Update(&req); err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "更新失败")
|
||||
return
|
||||
}
|
||||
common.OkMessage(c, "公告已更新")
|
||||
}
|
||||
|
||||
// Delete 删除公告 API
|
||||
func (ac *AnnouncementController) Delete(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusBadRequest, "无效的 ID")
|
||||
return
|
||||
}
|
||||
if err := ac.svc.Delete(uint(id)); err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "删除失败")
|
||||
return
|
||||
}
|
||||
common.OkMessage(c, "公告已删除")
|
||||
}
|
||||
Reference in New Issue
Block a user