Initial backend repository commit.

Set up project files and add .gitignore to exclude local build/runtime artifacts.

Made-with: Cursor
This commit is contained in:
2026-03-09 21:28:58 +08:00
commit 4d8f2ec997
102 changed files with 25022 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
package handler
import (
"strconv"
"github.com/gin-gonic/gin"
"carrot_bbs/internal/pkg/response"
"carrot_bbs/internal/service"
)
// NotificationHandler 通知处理器
type NotificationHandler struct {
notificationService *service.NotificationService
}
// NewNotificationHandler 创建通知处理器
func NewNotificationHandler(notificationService *service.NotificationService) *NotificationHandler {
return &NotificationHandler{
notificationService: notificationService,
}
}
// GetNotifications 获取通知列表
func (h *NotificationHandler) GetNotifications(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
unreadOnly := c.Query("unread_only") == "true"
notifications, total, err := h.notificationService.GetByUserID(c.Request.Context(), userID, page, pageSize, unreadOnly)
if err != nil {
response.InternalServerError(c, "failed to get notifications")
return
}
response.Paginated(c, notifications, total, page, pageSize)
}
// MarkAsRead 标记为已读
func (h *NotificationHandler) MarkAsRead(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
id := c.Param("id")
err := h.notificationService.MarkAsReadWithUserID(c.Request.Context(), id, userID)
if err != nil {
response.InternalServerError(c, "failed to mark as read")
return
}
response.SuccessWithMessage(c, "marked as read", nil)
}
// MarkAllAsRead 标记所有为已读
func (h *NotificationHandler) MarkAllAsRead(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
err := h.notificationService.MarkAllAsRead(c.Request.Context(), userID)
if err != nil {
response.InternalServerError(c, "failed to mark all as read")
return
}
response.SuccessWithMessage(c, "all marked as read", nil)
}
// GetUnreadCount 获取未读数量
func (h *NotificationHandler) GetUnreadCount(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
count, err := h.notificationService.GetUnreadCount(c.Request.Context(), userID)
if err != nil {
response.InternalServerError(c, "failed to get unread count")
return
}
response.Success(c, gin.H{"count": count})
}
// DeleteNotification 删除通知
func (h *NotificationHandler) DeleteNotification(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
id := c.Param("id")
err := h.notificationService.DeleteNotification(c.Request.Context(), id, userID)
if err != nil {
response.InternalServerError(c, "failed to delete notification")
return
}
response.Success(c, gin.H{"success": true})
}
// ClearAllNotifications 清空所有通知
func (h *NotificationHandler) ClearAllNotifications(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
err := h.notificationService.ClearAllNotifications(c.Request.Context(), userID)
if err != nil {
response.InternalServerError(c, "failed to clear notifications")
return
}
response.Success(c, gin.H{"success": true})
}