迁移公告,邮件
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"kra/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type announcementRequest struct {
|
||||
ID uint `json:"ID"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
UserID *uint `json:"userID"`
|
||||
Attachments json.RawMessage `json:"attachments"`
|
||||
}
|
||||
|
||||
func announcementInput(req announcementRequest) service.AnnouncementInput {
|
||||
return service.AnnouncementInput{ID: req.ID, Title: req.Title, Content: req.Content, UserID: req.UserID, Attachments: req.Attachments}
|
||||
}
|
||||
|
||||
func registerAnnouncementRoutes(private, public *gin.RouterGroup, svc *service.AnnouncementService) {
|
||||
privateInfo := private.Group("/info")
|
||||
privateInfo.POST("/createInfo", func(c *gin.Context) {
|
||||
var req announcementRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := svc.Create(c.Request.Context(), announcementInput(req)); err != nil {
|
||||
fail(c, "创建失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, gin.H{}, "创建成功")
|
||||
})
|
||||
privateInfo.DELETE("/deleteInfo", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
if err := svc.Delete(c.Request.Context(), uint(id)); err != nil {
|
||||
fail(c, "删除失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, gin.H{}, "删除成功")
|
||||
})
|
||||
privateInfo.DELETE("/deleteInfoByIds", func(c *gin.Context) {
|
||||
values := c.QueryArray("IDs[]")
|
||||
if len(values) == 0 {
|
||||
values = c.QueryArray("IDs")
|
||||
}
|
||||
ids := make([]uint, 0, len(values))
|
||||
for _, value := range values {
|
||||
id, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
ids = append(ids, uint(id))
|
||||
}
|
||||
if err := svc.DeleteByIDs(c.Request.Context(), ids); err != nil {
|
||||
fail(c, "批量删除失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, gin.H{}, "批量删除成功")
|
||||
})
|
||||
privateInfo.PUT("/updateInfo", func(c *gin.Context) {
|
||||
var req announcementRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := svc.Update(c.Request.Context(), announcementInput(req)); err != nil {
|
||||
fail(c, "更新失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, gin.H{}, "更新成功")
|
||||
})
|
||||
privateInfo.GET("/findInfo", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
item, err := svc.Find(c.Request.Context(), uint(id))
|
||||
if err != nil {
|
||||
fail(c, "查询失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
okWithData(c, item)
|
||||
})
|
||||
privateInfo.GET("/getInfoList", func(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||
start, startErr := parseAnnouncementTime(c.Query("startCreatedAt"))
|
||||
end, endErr := parseAnnouncementTime(c.Query("endCreatedAt"))
|
||||
if startErr != nil || endErr != nil || (start == nil) != (end == nil) {
|
||||
fail(c, "创建日期范围不合法")
|
||||
return
|
||||
}
|
||||
items, total, err := svc.List(c.Request.Context(), page, pageSize, start, end)
|
||||
if err != nil {
|
||||
fail(c, "获取失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: pageSize}, "获取成功")
|
||||
})
|
||||
publicInfo := public.Group("/info")
|
||||
publicInfo.GET("/getInfoDataSource", func(c *gin.Context) {
|
||||
users, err := svc.UserOptions(c.Request.Context())
|
||||
if err != nil {
|
||||
fail(c, "查询失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
okWithData(c, gin.H{"userID": users})
|
||||
})
|
||||
publicInfo.GET("/getInfoPublic", func(c *gin.Context) {
|
||||
writeResult(c, codeSuccess, gin.H{"info": "不需要鉴权的公告接口信息"}, "获取成功")
|
||||
})
|
||||
}
|
||||
|
||||
func parseAnnouncementTime(value string) (*time.Time, error) {
|
||||
if value == "" {
|
||||
return nil, nil
|
||||
}
|
||||
parsed, err := time.Parse(time.RFC3339, value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &parsed, nil
|
||||
}
|
||||
Reference in New Issue
Block a user