A:增加默认资料库写入
This commit is contained in:
1 parent
592b01a89b
commit
76d747bbd9
6 files changed
+36
-13
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
func AutoMigrate() error {
|
||||
return DB.AutoMigrate(
|
||||
&model.User{},
|
||||
&model.LibraryTable{},
|
||||
// 在这里添加其他模型
|
||||
)
|
||||
}
|
||||
@@ -19,14 +19,17 @@ func HealthCheck(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// BasicResponse 健康检查
|
||||
func BasicResponse(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
Message: "OK",
|
||||
Data: map[string]interface{}{
|
||||
"timestamp": time.Now().Unix(),
|
||||
"version": "1.0.0",
|
||||
},
|
||||
Data: "hi",
|
||||
})
|
||||
}
|
||||
func BasicResponseV1(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
Message: "OK",
|
||||
Data: "hi, this is v1",
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"rear/internal/container"
|
||||
@@ -38,14 +40,30 @@ func (h *Handler) GetLibrary(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) AddLibrary(c *gin.Context) {
|
||||
var path string
|
||||
if err := c.ShouldBindJSON(&path); err != nil {
|
||||
// 定义请求体结构
|
||||
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",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 现在可以使用 req.Path
|
||||
path := req.Path
|
||||
|
||||
library := &model.LibraryTable{
|
||||
ImgPath: path,
|
||||
IsEnable: true,
|
||||
|
||||
@@ -3,7 +3,7 @@ package model
|
||||
type LibraryTable struct {
|
||||
BaseModel
|
||||
// 照片存储库路径
|
||||
ImgPath string `gorm:"not null;size:255" json:"imgPath"`
|
||||
ImgPath string `gorm:"not null;size:255" json:"img_path"`
|
||||
// 是否开启
|
||||
IsEnable bool `gorm:"default:false;" json:"isEnable"`
|
||||
IsEnable bool `gorm:"default:false;" json:"is_enable"`
|
||||
}
|
||||
@@ -14,14 +14,14 @@ func NewLibraryRepository() *LibraryRepository {
|
||||
func (s *LibraryRepository) AddLibrary(lib *model.LibraryTable) error {
|
||||
var result model.LibraryTable
|
||||
|
||||
err := db.GetDB().Where("imgPath = ?", lib.ImgPath).FirstOrCreate(&result, lib).Error
|
||||
err := db.GetDB().Where("img_path = ?", lib.ImgPath).FirstOrCreate(&result, lib).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ExecuteWrite(func() error {
|
||||
// 如果找到了已存在的记录,更新 IsEnable
|
||||
if result.ID != 0 && result.ImgPath == lib.ImgPath {
|
||||
return db.GetDB().Model(&result).Update("isEnable", true).Error
|
||||
return db.GetDB().Model(&result).Update("is_enable", true).Error
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -33,13 +33,13 @@ func (s *LibraryRepository) AddLibrary(lib *model.LibraryTable) error {
|
||||
|
||||
func (s *LibraryRepository) DeleteLibrary(imgPath string) error {
|
||||
return ExecuteWrite(func() error {
|
||||
return db.GetDB().Where("imgPath = ?", imgPath).Delete(&model.LibraryTable{}).Error
|
||||
return db.GetDB().Where("img_path = ?", imgPath).Delete(&model.LibraryTable{}).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (s *LibraryRepository) UpdateLibrary(oldImgPath string, newImgPath string) error {
|
||||
return ExecuteWrite(func() error {
|
||||
return db.GetDB().Where("imgPath = ?", oldImgPath).
|
||||
return db.GetDB().Where("img_path = ?", oldImgPath).
|
||||
Updates(&model.LibraryTable{ImgPath: newImgPath}).Error
|
||||
})
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ func SetupRoutes(r *gin.Engine, contain *container.Container) {
|
||||
// API版本组
|
||||
v1 := r.Group("/api/v1")
|
||||
{
|
||||
v1.GET("", handler.BasicResponseV1)
|
||||
// 用户相关路由
|
||||
users := v1.Group("/users")
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user