Files
urldb/handlers/pan_handler.go

130 lines
2.7 KiB
Go
Raw Permalink Normal View History

2025-07-10 13:58:28 +08:00
package handlers
import (
"net/http"
"strconv"
2025-07-18 09:42:07 +08:00
"github.com/ctwj/urldb/db/converter"
"github.com/ctwj/urldb/db/dto"
"github.com/ctwj/urldb/db/entity"
2025-07-10 13:58:28 +08:00
"github.com/gin-gonic/gin"
)
// GetPans 获取平台列表
func GetPans(c *gin.Context) {
pans, err := repoManager.PanRepository.FindAll()
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
2025-07-10 13:58:28 +08:00
return
}
responses := converter.ToPanResponseList(pans)
2025-07-11 17:45:16 +08:00
ListResponse(c, responses, int64(len(responses)))
2025-07-10 13:58:28 +08:00
}
// CreatePan 创建平台
func CreatePan(c *gin.Context) {
var req dto.CreatePanRequest
if err := c.ShouldBindJSON(&req); err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, err.Error(), http.StatusBadRequest)
2025-07-10 13:58:28 +08:00
return
}
pan := &entity.Pan{
2025-07-11 10:01:48 +08:00
Name: req.Name,
Key: req.Key,
Icon: req.Icon,
Remark: req.Remark,
2025-07-10 13:58:28 +08:00
}
err := repoManager.PanRepository.Create(pan)
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
2025-07-10 13:58:28 +08:00
return
}
2025-07-11 17:45:16 +08:00
SuccessResponse(c, gin.H{
2025-07-10 13:58:28 +08:00
"id": pan.ID,
"message": "平台创建成功",
})
}
// UpdatePan 更新平台
func UpdatePan(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, "无效的ID", http.StatusBadRequest)
2025-07-10 13:58:28 +08:00
return
}
var req dto.UpdatePanRequest
if err := c.ShouldBindJSON(&req); err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, err.Error(), http.StatusBadRequest)
2025-07-10 13:58:28 +08:00
return
}
pan, err := repoManager.PanRepository.FindByID(uint(id))
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, "平台不存在", http.StatusNotFound)
2025-07-10 13:58:28 +08:00
return
}
if req.Name != "" {
pan.Name = req.Name
}
pan.Key = req.Key
2025-07-11 10:01:48 +08:00
if req.Icon != "" {
pan.Icon = req.Icon
2025-07-10 13:58:28 +08:00
}
if req.Remark != "" {
pan.Remark = req.Remark
}
err = repoManager.PanRepository.Update(pan)
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
2025-07-10 13:58:28 +08:00
return
}
2025-07-11 17:45:16 +08:00
SuccessResponse(c, gin.H{"message": "平台更新成功"})
2025-07-10 13:58:28 +08:00
}
// DeletePan 删除平台
func DeletePan(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, "无效的ID", http.StatusBadRequest)
2025-07-10 13:58:28 +08:00
return
}
err = repoManager.PanRepository.Delete(uint(id))
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
2025-07-10 13:58:28 +08:00
return
}
2025-07-11 17:45:16 +08:00
SuccessResponse(c, gin.H{"message": "平台删除成功"})
2025-07-10 13:58:28 +08:00
}
// GetPan 根据ID获取平台
func GetPan(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, "无效的ID", http.StatusBadRequest)
2025-07-10 13:58:28 +08:00
return
}
pan, err := repoManager.PanRepository.FindByID(uint(id))
if err != nil {
2025-07-11 17:45:16 +08:00
ErrorResponse(c, "平台不存在", http.StatusNotFound)
2025-07-10 13:58:28 +08:00
return
}
response := converter.ToPanResponse(pan)
2025-07-11 17:45:16 +08:00
SuccessResponse(c, response)
2025-07-10 13:58:28 +08:00
}