116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package v1
|
|
|
|
import (
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/dto"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/middleware"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/service"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/app"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/errcode"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// NotificationHandler 通知相关接口。
|
|
type NotificationHandler struct {
|
|
svc *service.NotificationService
|
|
}
|
|
|
|
// NewNotificationHandler 创建通知 handler。
|
|
func NewNotificationHandler() *NotificationHandler {
|
|
return &NotificationHandler{svc: service.NewNotificationService()}
|
|
}
|
|
|
|
// List 分页查询当前用户通知。
|
|
func (h *NotificationHandler) List(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.NotificationListRequest
|
|
if err := c.ShouldBindQuery(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.List(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// Unread 获取当前用户未读数。
|
|
func (h *NotificationHandler) Unread(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
out, e := h.svc.Unread(c.Request.Context(), userID)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// Get 获取通知详情(uuid 来自路径参数)。
|
|
func (h *NotificationHandler) Get(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
request := dto.NotificationGetByUUIDRequest{NotificationUUID: c.Param("uuid")}
|
|
if request.NotificationUUID == "" {
|
|
response.ToErrorResponse(errcode.InvalidParams)
|
|
return
|
|
}
|
|
out, e := h.svc.GetByUUID(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// ReadAll 将当前用户全部通知标记为已读(前端进入通知页时调用)。
|
|
func (h *NotificationHandler) ReadAll(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
out, e := h.svc.ReadAll(c.Request.Context(), userID)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// Read 标记通知已读(uuid 来自路径参数)。
|
|
func (h *NotificationHandler) Read(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
request := dto.NotificationReadRequest{NotificationUUID: c.Param("uuid")}
|
|
if request.NotificationUUID == "" {
|
|
response.ToErrorResponse(errcode.InvalidParams)
|
|
return
|
|
}
|
|
out, e := h.svc.Read(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|