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
version.go 4.21 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"
)
// VersionHandler 版本信息
// https://gorm.io/zh_CN/docs/query.html
func VersionHandler(c *gin.Context) {
var versions []model.Version
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(&versions).Where("name LIKE ?", fmt.Sprintf("%%%v%%", text)).Order("ID DESC")
// 数据总量
obj.Count(&totalSize)
// 分页查询
obj.Offset((page - 1) * pageSize).Limit(pageSize).Find(&versions)
// 查询成功
if len(versions) > 0 {
c.JSON(http.StatusOK, gin.H{
"Datas": versions,
"StatusCode": http.StatusOK,
"Message": "查询成功",
})
return
}
// 查询失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "查询失败",
})
}
// VersionDetailHandler 版本详情
// https://gorm.io/zh_CN/docs/query.html
func VersionDetailHandler(c *gin.Context) {
var version model.Version
var id = ""
// 获取参数
id = c.Query("id")
// 参数错误
if id == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.First(&version, id)
// 查询成功
if version.ID > 0 {
c.JSON(http.StatusOK, gin.H{
"Data": version,
"StatusCode": http.StatusOK,
"Message": "查询成功",
})
return
}
// 查询失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "查询失败",
})
}
// VersionAddHandler 版本添加
// https://gorm.io/zh_CN/docs/create.html
func VersionAddHandler(c *gin.Context) {
var requestVersion model.Version
// 绑定前端数据
c.Bind(&requestVersion)
// 参数错误
if requestVersion.Name == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 添加数据
database.DB.Create(&requestVersion)
// 添加成功
if requestVersion.ID > 0 {
c.JSON(http.StatusOK, gin.H{
"ID": requestVersion.ID,
"StatusCode": http.StatusOK,
"Message": "添加成功",
})
return
}
// 添加失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "添加失败",
})
}
// VersionChangeHandler 版本编辑
// https://gorm.io/zh_CN/docs/update.html
func VersionChangeHandler(c *gin.Context) {
var version model.Version
var requestVersion model.Version
var id = ""
// 获取参数
id = c.Query("id")
// 绑定前端数据
c.Bind(&requestVersion)
// 参数错误
if id == "" || requestVersion.Name == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.First(&version, id)
// 编辑成功
if version.ID > 0 {
version.Name = requestVersion.Name
version.Version = requestVersion.Version
version.URL = requestVersion.URL
version.Description = requestVersion.Description
// 保存数据
database.DB.Save(&version)
c.JSON(http.StatusOK, gin.H{
"ID": version.ID,
"StatusCode": http.StatusOK,
"Message": "编辑成功",
})
return
}
// 编辑失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "编辑失败",
})
}
// VersionDeleteHandler 版本删除
// https://gorm.io/zh_CN/docs/delete.html
func VersionDeleteHandler(c *gin.Context) {
var version model.Version
// 获取参数
id := c.Query("id")
// 参数错误
if id == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 数据查询
database.DB.First(&version, id)
// 删除成功
if version.ID > 0 {
database.DB.Delete(&version, id)
c.JSON(http.StatusOK, gin.H{
"ID": version.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