add
This commit is contained in:
@@ -1,26 +1,115 @@
|
||||
package logic
|
||||
|
||||
// 这里是创建数据逻辑:
|
||||
// 1、接收 name,code 两个参数。
|
||||
// 2、确认提交的 name,code 两个参数不能为空,如果有空,则返回提示。
|
||||
// 3、第二步通过后,在 country 表中创建一个 id。
|
||||
// 4、通过 3 中的 id,分别保存到 name 和 code 的表中。
|
||||
import (
|
||||
"net/http"
|
||||
"country/db"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// CreateRequest 注册请求参数结构
|
||||
type CreateRequest struct {
|
||||
|
||||
Name string `json:"name" binding:"required"`
|
||||
Code string `json:"code" binding:"required"`
|
||||
}
|
||||
|
||||
// CreateResponse 注册响应结构
|
||||
type CreateResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
|
||||
} `json:"data"`
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Data CreateData `json:"data"`
|
||||
}
|
||||
|
||||
// CreateHandler 逻辑
|
||||
func CreateHandler(c *gin.Context) {
|
||||
// CreateData 响应数据结构
|
||||
type CreateData struct {
|
||||
CountryID string `json:"country_id"`
|
||||
}
|
||||
|
||||
// CreateHandler 处理国家创建逻辑
|
||||
func CreateHandler(c *gin.Context) {
|
||||
var req CreateRequest
|
||||
// 绑定并验证请求参数
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
zap.L().Warn("请求参数验证失败", zap.Error(err))
|
||||
c.JSON(http.StatusBadRequest, CreateResponse{
|
||||
Success: false,
|
||||
Message: "name和code为必填参数,不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 在country表中创建记录并获取ID
|
||||
var countryID string
|
||||
err := db.DB.QueryRow("INSERT INTO \"country\" DEFAULT VALUES RETURNING id").Scan(&countryID)
|
||||
if err != nil {
|
||||
zap.L().Error("插入country表失败", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||
Success: false,
|
||||
Message: "创建国家记录失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
tx, err := db.DB.Begin()
|
||||
if err != nil {
|
||||
zap.L().Error("开启事务失败", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||
Success: false,
|
||||
Message: "系统错误,无法开启事务",
|
||||
})
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
// 发生panic时回滚事务
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("事务处理发生panic", zap.Any("recover", r))
|
||||
}
|
||||
}()
|
||||
|
||||
// 插入name表
|
||||
_, err = tx.Exec("INSERT INTO name (country_id, name) VALUES ($1, $2)", countryID, req.Name)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("插入name表失败", zap.Error(err), zap.String("country_id", countryID))
|
||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||
Success: false,
|
||||
Message: "保存名称信息失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 插入code表
|
||||
_, err = tx.Exec("INSERT INTO code (country_id, code) VALUES ($1, $2)", countryID, req.Code)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("插入code表失败", zap.Error(err), zap.String("country_id", countryID))
|
||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||
Success: false,
|
||||
Message: "保存代码信息失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("提交事务失败", zap.Error(err), zap.String("country_id", countryID))
|
||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||
Success: false,
|
||||
Message: "提交数据失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
c.JSON(http.StatusOK, CreateResponse{
|
||||
Success: true,
|
||||
Message: "创建成功",
|
||||
Data: CreateData{
|
||||
CountryID: countryID,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user