add
This commit is contained in:
@@ -3,6 +3,7 @@ package logic
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"country/db"
|
"country/db"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@@ -28,10 +29,19 @@ type CreateData struct {
|
|||||||
|
|
||||||
// CreateHandler 处理国家创建逻辑
|
// CreateHandler 处理国家创建逻辑
|
||||||
func CreateHandler(c *gin.Context) {
|
func CreateHandler(c *gin.Context) {
|
||||||
|
|
||||||
|
tartTime := time.Now()
|
||||||
|
reqID := c.Request.Header.Get("X-RegisterRequest-ID")
|
||||||
|
if reqID == "" {
|
||||||
|
reqID = uuid.New().String()
|
||||||
|
}
|
||||||
|
// 使用zap.Info记录开始处理日志,添加请求ID字段
|
||||||
|
zap.L().Info("⌛️ 收到注册请求,开始处理", zap.String("req_id", reqID))
|
||||||
|
|
||||||
var req CreateRequest
|
var req CreateRequest
|
||||||
// 绑定并验证请求参数
|
// 绑定并验证请求参数
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
zap.L().Warn("请求参数验证失败", zap.Error(err))
|
zap.L().Warn("❗ 请求参数验证失败", zap.Error(err))
|
||||||
c.JSON(http.StatusBadRequest, CreateResponse{
|
c.JSON(http.StatusBadRequest, CreateResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Message: "name和code为必填参数,不能为空",
|
Message: "name和code为必填参数,不能为空",
|
||||||
@@ -43,7 +53,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
var countryID string
|
var countryID string
|
||||||
err := db.DB.QueryRow("INSERT INTO \"country\" DEFAULT VALUES RETURNING id").Scan(&countryID)
|
err := db.DB.QueryRow("INSERT INTO \"country\" DEFAULT VALUES RETURNING id").Scan(&countryID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.L().Error("插入country表失败", zap.Error(err))
|
zap.L().Error("❌ 插入country表失败", zap.Error(err))
|
||||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Message: "创建国家记录失败",
|
Message: "创建国家记录失败",
|
||||||
@@ -54,7 +64,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
// 开启事务
|
// 开启事务
|
||||||
tx, err := db.DB.Begin()
|
tx, err := db.DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.L().Error("开启事务失败", zap.Error(err))
|
zap.L().Error("❌ 开启事务失败", zap.Error(err))
|
||||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Message: "系统错误,无法开启事务",
|
Message: "系统错误,无法开启事务",
|
||||||
@@ -65,7 +75,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
// 发生panic时回滚事务
|
// 发生panic时回滚事务
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
zap.L().Error("事务处理发生panic", zap.Any("recover", r))
|
zap.L().Error("❌事务处理发生panic", zap.Any("recover", r))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -73,7 +83,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
_, err = tx.Exec("INSERT INTO name (country_id, name) VALUES ($1, $2)", countryID, req.Name)
|
_, err = tx.Exec("INSERT INTO name (country_id, name) VALUES ($1, $2)", countryID, req.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
zap.L().Error("插入name表失败", zap.Error(err), zap.String("country_id", countryID))
|
zap.L().Error("❌插入name表失败", zap.Error(err), zap.String("country_id", countryID))
|
||||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Message: "保存名称信息失败",
|
Message: "保存名称信息失败",
|
||||||
@@ -85,7 +95,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
_, err = tx.Exec("INSERT INTO code (country_id, code) VALUES ($1, $2)", countryID, req.Code)
|
_, err = tx.Exec("INSERT INTO code (country_id, code) VALUES ($1, $2)", countryID, req.Code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
zap.L().Error("插入code表失败", zap.Error(err), zap.String("country_id", countryID))
|
zap.L().Error("❌插入code表失败", zap.Error(err), zap.String("country_id", countryID))
|
||||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Message: "保存代码信息失败",
|
Message: "保存代码信息失败",
|
||||||
@@ -96,7 +106,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
// 提交事务
|
// 提交事务
|
||||||
if err := tx.Commit(); err != nil {
|
if err := tx.Commit(); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
zap.L().Error("提交事务失败", zap.Error(err), zap.String("country_id", countryID))
|
zap.L().Error("❌提交事务失败", zap.Error(err), zap.String("country_id", countryID))
|
||||||
c.JSON(http.StatusInternalServerError, CreateResponse{
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Message: "提交数据失败",
|
Message: "提交数据失败",
|
||||||
@@ -104,6 +114,8 @@ func CreateHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
duration := time.Since(startTime)
|
||||||
|
|
||||||
// 返回成功响应
|
// 返回成功响应
|
||||||
c.JSON(http.StatusOK, CreateResponse{
|
c.JSON(http.StatusOK, CreateResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user