health-go/handler/UserHandler.go

37 lines
686 B
Go
Raw Normal View History

2024-11-14 01:15:55 +08:00
package handler
2024-10-17 23:52:10 +08:00
import (
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"health-go/config"
2024-11-14 01:15:55 +08:00
"health-go/model"
"health-go/util"
2024-10-17 23:52:10 +08:00
"net/http"
)
func FirstUser(c *gin.Context) {
2024-11-14 01:15:55 +08:00
var user model.User
2024-10-17 23:52:10 +08:00
result := config.DB.Where("user_name = ?", "Biid").First(&user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
util.ReturnJson(c, 404, "用户未找到", nil)
2024-10-17 23:52:10 +08:00
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "内部服务器错误",
"data": nil,
})
}
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "ok",
"data": map[string]interface{}{
"first_user": user,
},
})
}