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
video.go 4.28 KB
Copy Edit Raw Blame History
dabolau authored 2020-10-19 14:44 . 添加图片资源
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"
)
// VideoHandler 视频信息
// https://gorm.io/zh_CN/docs/query.html
func VideoHandler(c *gin.Context) {
var videos []model.Video
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(&videos).Where("name LIKE ?", fmt.Sprintf("%%%v%%", text)).Order("ID DESC")
// 数据总量
obj.Count(&totalSize)
// 分页查询
obj.Offset((page - 1) * pageSize).Limit(pageSize).Find(&videos)
// 查询成功
if len(videos) > 0 {
c.JSON(http.StatusOK, gin.H{
"Datas": videos,
"StatusCode": http.StatusOK,
"Message": "查询成功",
})
return
}
// 查询失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "查询失败",
})
}
// VideoDetailHandler 视频详情
// https://gorm.io/zh_CN/docs/query.html
func VideoDetailHandler(c *gin.Context) {
var video model.Video
var id = ""
// 获取参数
id = c.Query("id")
// 参数错误
if id == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.First(&video, id)
// 查询成功
if video.ID > 0 {
c.JSON(http.StatusOK, gin.H{
"Data": video,
"StatusCode": http.StatusOK,
"Message": "查询成功",
})
return
}
// 查询失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "查询失败",
})
}
// VideoAddHandler 视频添加
// https://gorm.io/zh_CN/docs/create.html
func VideoAddHandler(c *gin.Context) {
var requestVideo model.Video
// 绑定前端数据
c.Bind(&requestVideo)
// 参数错误
if requestVideo.Name == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 添加数据
database.DB.Create(&requestVideo)
// 添加成功
if requestVideo.ID > 0 {
c.JSON(http.StatusOK, gin.H{
"ID": requestVideo.ID,
"StatusCode": http.StatusOK,
"Message": "添加成功",
})
return
}
// 添加失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "添加失败",
})
}
// VideoChangeHandler 视频编辑
// https://gorm.io/zh_CN/docs/update.html
func VideoChangeHandler(c *gin.Context) {
var video model.Video
var requestVideo model.Video
var id = ""
// 获取参数
id = c.Query("id")
// 绑定前端数据
c.Bind(&requestVideo)
// 参数错误
if id == "" || requestVideo.Name == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.First(&video, id)
// 编辑成功
if video.ID > 0 {
video.Name = requestVideo.Name
video.Type = requestVideo.Type
video.Area = requestVideo.Area
video.Year = requestVideo.Year
video.Actor = requestVideo.Actor
video.Director = requestVideo.Director
video.Status = requestVideo.Status
video.Description = requestVideo.Description
video.URL = requestVideo.URL
// 保存数据
database.DB.Save(&video)
c.JSON(http.StatusOK, gin.H{
"ID": video.ID,
"StatusCode": http.StatusOK,
"Message": "编辑成功",
})
return
}
// 编辑失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "编辑失败",
})
}
// VideoDeleteHandler 视频删除
// https://gorm.io/zh_CN/docs/delete.html
func VideoDeleteHandler(c *gin.Context) {
var video model.Video
// 获取参数
id := c.Query("id")
// 参数错误
if id == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 数据查询
database.DB.First(&video, id)
// 删除成功
if video.ID > 0 {
database.DB.Delete(&video, id)
c.JSON(http.StatusOK, gin.H{
"ID": video.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