130 lines
3.5 KiB
Go
130 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, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
if err := h.service.Delete(c.Request.Context(), uint(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, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := h.service.Find(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "查询失败")
|
|
return
|
|
}
|
|
httpx.OKWithData(c, item)
|
|
}
|
|
|
|
func (h *Announcement) List(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.Query("page"))
|
|
pageSize, _ := strconv.Atoi(c.Query("pageSize"))
|
|
start, startErr := parseTime(c.Query("startCreatedAt"))
|
|
end, endErr := parseTime(c.Query("endCreatedAt"))
|
|
if startErr != nil || endErr != nil || (start == nil) != (end == nil) {
|
|
httpx.Fail(c, "创建日期范围不合法")
|
|
return
|
|
}
|
|
items, total, err := h.service.List(c.Request.Context(), page, pageSize, start, end)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: page, PageSize: 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
|
|
}
|