139 lines
3.5 KiB
Go
139 lines
3.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"kra/internal/server/httpx"
|
|
"kra/internal/service"
|
|
"kra/internal/service/dto"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Announcement struct{ service *service.AnnouncementService }
|
|
|
|
func NewAnnouncement(service *service.AnnouncementService) *Announcement {
|
|
return &Announcement{service: service}
|
|
}
|
|
|
|
func announcementInput(req dto.AnnouncementRequest) service.AnnouncementInput {
|
|
return service.AnnouncementInput{ID: req.ID, Title: req.Title, Content: req.Content, UserID: req.UserID, Attachments: req.Attachments}
|
|
}
|
|
|
|
func (h *Announcement) Create(c *gin.Context) {
|
|
var req dto.AnnouncementRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.service.Create(c.Request.Context(), announcementInput(req)); err != nil {
|
|
httpx.Fail(c, "创建失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建成功")
|
|
}
|
|
|
|
func (h *Announcement) Delete(c *gin.Context) {
|
|
id, present, err := queryUintValue(c, "ID")
|
|
if err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if !present {
|
|
// Preserve the legacy endpoint behavior for an omitted ID.
|
|
id = 0
|
|
}
|
|
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
|
httpx.Fail(c, "删除失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
|
|
}
|
|
|
|
func (h *Announcement) DeleteByIDs(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 {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
ids = append(ids, uint(id))
|
|
}
|
|
if err := h.service.DeleteByIDs(c.Request.Context(), ids); err != nil {
|
|
httpx.Fail(c, "批量删除失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "批量删除成功")
|
|
}
|
|
|
|
func (h *Announcement) Update(c *gin.Context) {
|
|
var req dto.AnnouncementRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.service.Update(c.Request.Context(), announcementInput(req)); err != nil {
|
|
httpx.Fail(c, "更新失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
|
|
}
|
|
|
|
func (h *Announcement) Find(c *gin.Context) {
|
|
id, _, err := queryUintValue(c, "ID")
|
|
if err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
item, err := h.service.Find(c.Request.Context(), id)
|
|
if err != nil {
|
|
httpx.Fail(c, "查询失败")
|
|
return
|
|
}
|
|
httpx.OKWithData(c, item)
|
|
}
|
|
|
|
func (h *Announcement) List(c *gin.Context) {
|
|
var req dto.AnnouncementSearchRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
items, total, err := h.service.List(c.Request.Context(), req.Page, req.PageSize, req.StartCreatedAt, req.EndCreatedAt)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
|
}
|
|
|
|
func (h *Announcement) DataSource(c *gin.Context) {
|
|
users, err := h.service.UserOptions(c.Request.Context())
|
|
if err != nil {
|
|
httpx.Fail(c, "查询失败")
|
|
return
|
|
}
|
|
httpx.OKWithData(c, gin.H{"userID": users})
|
|
}
|
|
|
|
func (h *Announcement) Public(c *gin.Context) {
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{"info": "不需要鉴权的公告接口信息"}, "获取成功")
|
|
}
|
|
|
|
func parseTime(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
|
|
}
|