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:
123
internal/handler/channel_handler.go
Normal file
123
internal/handler/channel_handler.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/pkg/response"
|
||||
"carrot_bbs/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ChannelHandler struct {
|
||||
channelService service.ChannelService
|
||||
}
|
||||
|
||||
func NewChannelHandler(channelService service.ChannelService) *ChannelHandler {
|
||||
return &ChannelHandler{channelService: channelService}
|
||||
}
|
||||
|
||||
type channelResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Description string `json:"description,omitempty"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
IsActive bool `json:"is_active"`
|
||||
}
|
||||
|
||||
func toChannelResponse(c *model.Channel) *channelResponse {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return &channelResponse{
|
||||
ID: c.ID,
|
||||
Name: c.Name,
|
||||
Slug: c.Slug,
|
||||
Description: c.Description,
|
||||
SortOrder: c.SortOrder,
|
||||
IsActive: c.IsActive,
|
||||
}
|
||||
}
|
||||
|
||||
// ListPublic 公开频道列表(仅启用)
|
||||
func (h *ChannelHandler) ListPublic(c *gin.Context) {
|
||||
items, err := h.channelService.ListActive()
|
||||
if err != nil {
|
||||
response.InternalServerError(c, "failed to get channels")
|
||||
return
|
||||
}
|
||||
result := make([]*channelResponse, 0, len(items))
|
||||
for _, item := range items {
|
||||
result = append(result, toChannelResponse(item))
|
||||
}
|
||||
response.Success(c, gin.H{"list": result})
|
||||
}
|
||||
|
||||
// AdminList 管理端频道列表(全部)
|
||||
func (h *ChannelHandler) AdminList(c *gin.Context) {
|
||||
items, err := h.channelService.ListAll()
|
||||
if err != nil {
|
||||
response.InternalServerError(c, "failed to get channels")
|
||||
return
|
||||
}
|
||||
result := make([]*channelResponse, 0, len(items))
|
||||
for _, item := range items {
|
||||
result = append(result, toChannelResponse(item))
|
||||
}
|
||||
response.Success(c, gin.H{"list": result})
|
||||
}
|
||||
|
||||
type adminChannelUpsertRequest struct {
|
||||
Name string `json:"name" binding:"required,max=100"`
|
||||
Slug string `json:"slug" binding:"required,max=100"`
|
||||
Description string `json:"description" binding:"max=255"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
IsActive *bool `json:"is_active"`
|
||||
}
|
||||
|
||||
func (h *ChannelHandler) AdminCreate(c *gin.Context) {
|
||||
var req adminChannelUpsertRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
isActive := true
|
||||
if req.IsActive != nil {
|
||||
isActive = *req.IsActive
|
||||
}
|
||||
item, err := h.channelService.Create(req.Name, req.Slug, req.Description, req.SortOrder, isActive)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, "failed to create channel")
|
||||
return
|
||||
}
|
||||
response.Success(c, toChannelResponse(item))
|
||||
}
|
||||
|
||||
func (h *ChannelHandler) AdminUpdate(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var req adminChannelUpsertRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
isActive := true
|
||||
if req.IsActive != nil {
|
||||
isActive = *req.IsActive
|
||||
}
|
||||
item, err := h.channelService.Update(id, req.Name, req.Slug, req.Description, req.SortOrder, isActive)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, "failed to update channel")
|
||||
return
|
||||
}
|
||||
response.Success(c, toChannelResponse(item))
|
||||
}
|
||||
|
||||
func (h *ChannelHandler) AdminDelete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.channelService.Delete(id); err != nil {
|
||||
response.InternalServerError(c, "failed to delete channel")
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"success": true})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user