You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.0 KiB
141 lines
3.0 KiB
package mainPage
|
|
|
|
import (
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"practice_Go/internal/model/dto"
|
|
"practice_Go/internal/service"
|
|
"time"
|
|
)
|
|
|
|
// 定义一个MainPage结构体
|
|
type MainPage struct{}
|
|
|
|
// 创建一个新的MainPage实例 初始化
|
|
func NewMainPage() *MainPage {
|
|
return &MainPage{}
|
|
}
|
|
|
|
// 获取文章,视频
|
|
func (c *MainPage) GetShows(req *ghttp.Request) {
|
|
shows, err := service.GetShows().GetShows(req.Context())
|
|
if err == nil {
|
|
req.Response.WriteJson(dto.Result{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: shows,
|
|
})
|
|
} else {
|
|
req.Response.WriteJson(dto.Result{
|
|
Code: 500,
|
|
Message: "error",
|
|
Data: err,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 获取视频
|
|
func (c *MainPage) GetVideos(req *ghttp.Request) {
|
|
shows, err := service.GetShows().GetVideos(req.Context())
|
|
if err == nil {
|
|
req.Response.WriteJson(dto.Result{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: shows,
|
|
})
|
|
} else {
|
|
req.Response.WriteJson(dto.Result{
|
|
Code: 500,
|
|
Message: "error",
|
|
Data: err,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 新增数据
|
|
func (c *MainPage) AddShow(req *ghttp.Request) {
|
|
|
|
type Show struct {
|
|
Id int `json:"id"`
|
|
Name string `json:"name"`
|
|
Cover string `json:"cover"`
|
|
UserId int `json:"userId"`
|
|
ReleaseTime time.Time `json:"releaseTime"`
|
|
VideoDuration time.Duration `json:"videoDuration"`
|
|
ViewCount int `json:"viewCount"`
|
|
Comments int `json:"comments"`
|
|
Likes int `json:"likes"`
|
|
FlagType int `json:"flagType"`
|
|
ChannelId int `json:"channelId"`
|
|
}
|
|
|
|
////通过map
|
|
//data := g.Map{
|
|
// "name": "视频1",
|
|
// "userId": 1,
|
|
// "likes": 6,
|
|
// "comments": 3,
|
|
// "viewCount": 10,
|
|
//}
|
|
//
|
|
////通过结构体对象
|
|
//data := Show{
|
|
// Name: "视频1",
|
|
// UserId: 1,
|
|
// Likes: 6,
|
|
// Comments: 3,
|
|
// ReleaseTime: gtime.New("2024/10/23"),
|
|
//}
|
|
//
|
|
md := g.Model("show")
|
|
//
|
|
|
|
//批量插入多条数据
|
|
////通过map
|
|
//data := g.List{
|
|
// g.Map{
|
|
// "name": "视频1",
|
|
// "userId": 1,
|
|
// "likes": 6,
|
|
// "comments": 3,
|
|
// "viewCount": 23,
|
|
// },
|
|
// g.Map{
|
|
// "name": "视频2",
|
|
// "userId": 2,
|
|
// "likes": 6,
|
|
// "comments": 3,
|
|
// "viewCount": 20,
|
|
// },
|
|
// g.Map{
|
|
// "name": "文章1",
|
|
// "userId": 3,
|
|
// "likes": 6,
|
|
// "comments": 3,
|
|
// "viewCount": 15,
|
|
// },
|
|
//}
|
|
|
|
//通过结构体对象
|
|
data := g.Array{
|
|
//里边是结构体集合
|
|
}
|
|
|
|
////写法一 插入
|
|
////result, err := md.Data(data).Insert()
|
|
////写法二 插入 主键冲突,报错
|
|
////result, err := md.Insert(data) //insert into ...
|
|
////写法三 主键冲突,就新数据 替换 旧数据
|
|
////result, err := md.Replace(data) //replace into ...
|
|
////写法四 主键冲突,就根据主键 更新 数据
|
|
//result, err := md.Save(data) //insert into into ... on duplicate key update ...
|
|
|
|
//插入并获取id
|
|
result, err := md.InsertAndGetId(data)
|
|
|
|
if err == nil {
|
|
req.Response.WriteJson(result)
|
|
} else {
|
|
req.Response.WriteJson(err)
|
|
}
|
|
}
|