U:调整 “资料库” 删除时数据传递方式
This commit is contained in:
1 parent
94ed0d5acb
commit
b1ee8d916a
4 files changed
+82
-46
No files matched your search
+21
-1
@@ -11,6 +11,8 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY=
|
||||
github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok=
|
||||
github.com/gin-contrib/cors v1.7.5 h1:cXC9SmofOrRg0w9PigwGlHG3ztswH6bqq4vJVXnvYMk=
|
||||
@@ -21,6 +23,10 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
|
||||
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
|
||||
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
||||
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
|
||||
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
@@ -36,8 +42,11 @@ github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PU
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/inflection v1.0.0+incompatible/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
@@ -65,6 +74,9 @@ github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
@@ -116,4 +128,12 @@ gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
|
||||
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
|
||||
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
|
||||
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
|
||||
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
|
||||
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
|
||||
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
|
||||
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
|
||||
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
|
||||
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
|
||||
modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM=
|
||||
modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
@@ -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