Complete 4-layer notification module as reference template: domain → infrastructure → application → interfaces. 10 unit tests covering all service methods with mock repository.
125 lines
3.1 KiB
Go
125 lines
3.1 KiB
Go
package interfaces
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"mengstack/internal/kernel/response"
|
|
"mengstack/internal/modules/notification/application"
|
|
"mengstack/internal/modules/notification/domain"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
svc *application.Service
|
|
}
|
|
|
|
func NewHandler(svc *application.Service) *Handler {
|
|
return &Handler{svc: svc}
|
|
}
|
|
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
var req domain.CreateNotificationRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid request: "+err.Error()))
|
|
return
|
|
}
|
|
tenantID := c.GetString("tenant_id")
|
|
n, err := h.svc.Create(c.Request.Context(), tenantID, &req)
|
|
if err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, n)
|
|
}
|
|
|
|
func (h *Handler) Get(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid id"))
|
|
return
|
|
}
|
|
tenantID := c.GetString("tenant_id")
|
|
n, err := h.svc.Get(c.Request.Context(), tenantID, uint(id))
|
|
if err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
if n == nil {
|
|
response.Fail(c, response.NewBadRequest("notification not found"))
|
|
return
|
|
}
|
|
response.Success(c, n)
|
|
}
|
|
|
|
func (h *Handler) ListByUser(c *gin.Context) {
|
|
userID, err := strconv.ParseUint(c.Param("userId"), 10, 64)
|
|
if err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid user_id"))
|
|
return
|
|
}
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
tenantID := c.GetString("tenant_id")
|
|
notifications, total, err := h.svc.ListByUser(c.Request.Context(), tenantID, uint(userID), page, pageSize)
|
|
if err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{
|
|
"items": notifications,
|
|
"total": total,
|
|
"page": page,
|
|
})
|
|
}
|
|
|
|
func (h *Handler) MarkAsRead(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid id"))
|
|
return
|
|
}
|
|
tenantID := c.GetString("tenant_id")
|
|
if err := h.svc.MarkAsRead(c.Request.Context(), tenantID, uint(id)); err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *Handler) MarkAllAsRead(c *gin.Context) {
|
|
userID := c.GetUint("user_id")
|
|
tenantID := c.GetString("tenant_id")
|
|
if err := h.svc.MarkAllAsRead(c.Request.Context(), tenantID, userID); err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *Handler) CountUnread(c *gin.Context) {
|
|
userID := c.GetUint("user_id")
|
|
tenantID := c.GetString("tenant_id")
|
|
count, err := h.svc.CountUnread(c.Request.Context(), tenantID, userID)
|
|
if err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"count": count})
|
|
}
|
|
|
|
func (h *Handler) Delete(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid id"))
|
|
return
|
|
}
|
|
tenantID := c.GetString("tenant_id")
|
|
if err := h.svc.Delete(c.Request.Context(), tenantID, uint(id)); err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "deleted"})
|
|
}
|