U:调整 “资料库” 删除时数据传递方式
This commit is contained in:
1 parent
94ed0d5acb
commit
b1ee8d916a
4 files changed
+82
-46
No files matched your search
@@ -1,13 +1,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"rear/internal/container"
|
||||
"rear/internal/model"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
@@ -18,7 +19,7 @@ func NewLibraryHandler(container *container.Container) *Handler {
|
||||
return &Handler{container: container}
|
||||
}
|
||||
|
||||
// GetLibrary 获取存储哭
|
||||
// GetLibrary 获取存储
|
||||
func (h *Handler) GetLibrary(c *gin.Context) {
|
||||
libraries, err := h.container.LibraryRepo.GetAllLibrary()
|
||||
if err != nil {
|
||||
@@ -38,39 +39,39 @@ func (h *Handler) GetLibrary(c *gin.Context) {
|
||||
Data: libraries,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) AddLibrary(c *gin.Context) {
|
||||
// 定义请求体结构
|
||||
type AddLibraryRequest struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
var req AddLibraryRequest
|
||||
|
||||
// 读取原始JSON数据(用于调试)
|
||||
rawData, _ := c.GetRawData()
|
||||
// 重新设置请求体,因为GetRawData()会消耗掉body
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(rawData))
|
||||
|
||||
// 绑定JSON到结构体
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "Invalid request body",
|
||||
Message: fmt.Sprintf("Invalid request body: %v", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 现在可以使用 req.Path
|
||||
path := req.Path
|
||||
// 验证路径
|
||||
path := strings.TrimSpace(req.Path)
|
||||
if path == "" {
|
||||
c.JSON(http.StatusBadRequest, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "Path cannot be empty",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
library := &model.LibraryTable{
|
||||
ImgPath: path,
|
||||
IsEnable: true,
|
||||
}
|
||||
err := h.container.LibraryRepo.AddLibrary(library)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
|
||||
if err := h.container.LibraryRepo.AddLibrary(library); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Response{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
@@ -83,22 +84,26 @@ func (h *Handler) AddLibrary(c *gin.Context) {
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateLibrary(c *gin.Context) {
|
||||
var path string
|
||||
if err := c.ShouldBindJSON(&path); err != nil {
|
||||
type UpdateLibraryRequest struct {
|
||||
Path string `json:"path"`
|
||||
IsEnable bool `json:"is_enable"`
|
||||
}
|
||||
|
||||
var req UpdateLibraryRequest
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil || strings.TrimSpace(req.Path) == "" {
|
||||
c.JSON(http.StatusBadRequest, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "Invalid request body",
|
||||
Message: "Invalid request body or missing path",
|
||||
})
|
||||
return
|
||||
}
|
||||
library := &model.LibraryTable{
|
||||
ImgPath: path,
|
||||
IsEnable: true,
|
||||
}
|
||||
err := h.container.LibraryRepo.AddLibrary(library)
|
||||
|
||||
err := h.container.LibraryRepo.UpdateLibrary(req.Path, req.IsEnable)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
c.JSON(http.StatusInternalServerError, model.Response{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
@@ -107,26 +112,22 @@ func (h *Handler) UpdateLibrary(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: nil,
|
||||
Message: "Library updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteLibrary(c *gin.Context) {
|
||||
var path string
|
||||
if err := c.ShouldBindJSON(&path); err != nil {
|
||||
path := c.Query("path")
|
||||
if strings.TrimSpace(path) == "" {
|
||||
c.JSON(http.StatusBadRequest, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "Invalid request body",
|
||||
Message: "Missing path query parameter",
|
||||
})
|
||||
return
|
||||
}
|
||||
library := &model.LibraryTable{
|
||||
ImgPath: path,
|
||||
IsEnable: true,
|
||||
}
|
||||
err := h.container.LibraryRepo.AddLibrary(library)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
|
||||
if err := h.container.LibraryRepo.DeleteLibrary(path); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Response{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
@@ -135,7 +136,6 @@ func (h *Handler) DeleteLibrary(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: nil,
|
||||
Message: "Library deleted successfully",
|
||||
})
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"rear/internal/db"
|
||||
"rear/internal/model"
|
||||
"rear/pkg/logger"
|
||||
)
|
||||
|
||||
type LibraryRepository struct{}
|
||||
@@ -32,15 +34,28 @@ func (s *LibraryRepository) AddLibrary(lib *model.LibraryTable) error {
|
||||
}
|
||||
|
||||
func (s *LibraryRepository) DeleteLibrary(imgPath string) error {
|
||||
result := fmt.Sprintf("DeleteLibrary called with oldImgPath: %s", imgPath)
|
||||
logger.Warn(result)
|
||||
return ExecuteWrite(func() error {
|
||||
return db.GetDB().Where("img_path = ?", imgPath).Delete(&model.LibraryTable{}).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (s *LibraryRepository) UpdateLibrary(oldImgPath string, newImgPath string) error {
|
||||
func (s *LibraryRepository) UpdateLibrary(oldImgPath string, isEnable bool) error {
|
||||
result := fmt.Sprintf("UpdateLibrary called with oldImgPath: %s, isEnable: %t", oldImgPath, isEnable)
|
||||
logger.Warn(result)
|
||||
/*
|
||||
SQL 更新语句中只更新了 updated_at 字段,而没有更新 is_enable 字段。这是 GORM 的一个常见陷阱。
|
||||
问题原因:
|
||||
GORM 的 Updates 方法默认会忽略零值字段。如果 isEnable 参数是 false(布尔类型的零值),GORM 不会将其包含在更新语句中。
|
||||
解决方案有以下几种:
|
||||
方案1:使用 Select 明确指定字段(推荐)
|
||||
*/
|
||||
|
||||
return ExecuteWrite(func() error {
|
||||
return db.GetDB().Where("img_path = ?", oldImgPath).
|
||||
Updates(&model.LibraryTable{ImgPath: newImgPath}).Error
|
||||
Select("is_enable"). // 明确指定要更新的字段
|
||||
Updates(&model.LibraryTable{IsEnable: isEnable}).Error
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"rear/internal/container"
|
||||
"rear/internal/handler"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SetupRoutes 设置路由
|
||||
@@ -35,8 +36,8 @@ func SetupRoutes(r *gin.Engine, contain *container.Container) {
|
||||
{
|
||||
library.GET("", libraryHandler.GetLibrary)
|
||||
library.POST("", libraryHandler.AddLibrary)
|
||||
library.PUT("/:path", libraryHandler.UpdateLibrary)
|
||||
library.DELETE("/:path", libraryHandler.DeleteLibrary)
|
||||
library.PUT("", libraryHandler.UpdateLibrary)
|
||||
library.DELETE("", libraryHandler.DeleteLibrary)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user