1 Star 0 Fork 0

dabolau / lakaigo

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
user.go 4.15 KB
Copy Edit Raw Blame History
dabolau authored 2020-10-06 11:35 . 添加分页大小
package controller
import (
"fmt"
"net/http"
"strconv"
"gitee.com/dabolau/lakaigo/database"
"gitee.com/dabolau/lakaigo/model"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// UserHandler 用户信息
// https://gorm.io/zh_CN/docs/query.html
func UserHandler(c *gin.Context) {
var users []model.User
var text = ""
var obj *gorm.DB
var page = 1
var pageSize = 3
var totalSize int64 = 0
// 获取参数
text = c.Query("text")
pageSize, _ = strconv.Atoi(c.Query("size"))
page, _ = strconv.Atoi(c.Query("page"))
// 查询数据
obj = database.DB.Model(&users).Where("username LIKE ?", fmt.Sprintf("%%%v%%", text)).Order("ID DESC")
// 数据总量
obj.Count(&totalSize)
// 分页查询
obj.Offset((page - 1) * pageSize).Limit(pageSize).Find(&users)
// 查询成功
if len(users) > 0 {
c.JSON(http.StatusOK, gin.H{
"Datas": users,
"StatusCode": http.StatusOK,
"Message": "查询成功",
})
return
}
// 查询失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "查询失败",
})
}
// UserDetailHandler 用户详情
// https://gorm.io/zh_CN/docs/query.html
func UserDetailHandler(c *gin.Context) {
var user model.User
var id = ""
// 获取参数
id = c.Query("id")
// 参数错误
if id == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.First(&user, id)
// 查询成功
if user.ID > 0 {
c.JSON(http.StatusOK, gin.H{
"Data": user,
"StatusCode": http.StatusOK,
"Message": "查询成功",
})
return
}
// 查询失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "查询失败",
})
}
// UserAddHandler 用户添加
// https://gorm.io/zh_CN/docs/create.html
func UserAddHandler(c *gin.Context) {
var requestUser model.User
// 绑定前端数据
c.Bind(&requestUser)
// 参数错误
if requestUser.Username == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 添加数据
database.DB.Create(&requestUser)
// 添加成功
if requestUser.ID > 0 {
c.JSON(http.StatusOK, gin.H{
"ID": requestUser.ID,
"StatusCode": http.StatusOK,
"Message": "添加成功",
})
return
}
// 添加失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "添加失败",
})
}
// UserChangeHandler 用户编辑
// https://gorm.io/zh_CN/docs/update.html
func UserChangeHandler(c *gin.Context) {
var user model.User
var requestUser model.User
var id = ""
// 获取参数
id = c.Query("id")
// 绑定前端数据
c.Bind(&requestUser)
// 参数错误
if id == "" || requestUser.Username == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.First(&user, id)
// 编辑成功
if user.ID > 0 {
user.Username = requestUser.Username
user.Email = requestUser.Email
user.Phone = requestUser.Phone
user.Password = requestUser.Password
user.Nickname = requestUser.Nickname
user.Description = requestUser.Description
// 保存数据
database.DB.Save(&user)
c.JSON(http.StatusOK, gin.H{
"ID": user.ID,
"StatusCode": http.StatusOK,
"Message": "编辑成功",
})
return
}
// 编辑失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "编辑失败",
})
}
// UserDeleteHandler 用户删除
// https://gorm.io/zh_CN/docs/delete.html
func UserDeleteHandler(c *gin.Context) {
var user model.User
// 获取参数
id := c.Query("id")
// 参数错误
if id == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 数据查询
database.DB.First(&user, id)
// 删除成功
if user.ID > 0 {
database.DB.Delete(&user, id)
c.JSON(http.StatusOK, gin.H{
"ID": user.ID,
"StatusCode": http.StatusOK,
"Message": "删除成功",
})
return
}
// 删除失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "删除失败",
})
}
Go
1
https://gitee.com/dabolau/lakaigo.git
git@gitee.com:dabolau/lakaigo.git
dabolau
lakaigo
lakaigo
4b3cbd6f3e69

Search

53164aa7 5694891 3bd8fe86 5694891