Commit 3221e79d authored by shajiaiming's avatar shajiaiming

直播间api

parent 78f35215
......@@ -17,6 +17,9 @@ type LiveInfo struct {
ApplyId uint32 `gorm:"not null;default:0" json:"apply_id"`
Status uint8 `gorm:"not null;default:0" json:"status"`
Sort uint8 `gorm:"not null;default:0" json:"sort"`
PlatformId int64 `gorm:"not null;default:0" json:"platform_id"`
Category *LiveCategory `gorm:"foreignkey:CategoryId" json:"category"`
}
func (w LiveInfo) TableName() string {
......@@ -65,7 +68,7 @@ func GetLiveInfoTotal(maps interface{}) (int, error) {
func GetLiveInfo(pageNum, pageSize int, maps interface{}) ([]*LiveInfo, error) {
var live []*LiveInfo
err := db.Where(maps).Order("sort desc").Order("create_time desc").Offset(pageNum).Limit(pageSize).Find(&live).Error
err := db.Where(maps).Preload("Category").Order("sort desc").Order("create_time desc").Offset(pageNum).Limit(pageSize).Find(&live).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
......@@ -83,6 +86,7 @@ func AddLiveInfo(data map[string]interface{}) (error) {
Mode: data["mode"].(uint8),
ApplyId: data["apply_id"].(uint32),
Sort: data["sort"].(uint8),
PlatformId: data["platform_id"].(int64),
}
if err := db.Create(&live).Error; err != nil {
......
......@@ -3,48 +3,50 @@ package backend
import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"bwallet/service/live_info_service"
"bwallet/validate_service"
"github.com/Unknwon/com"
"github.com/gin-gonic/gin"
"strings"
)
func GetLiveInfo(c *gin.Context) {
//token := c.Request.Header.Get("Token")
//user, _ := util.ParseToken(token)
//group := user.UserInfo.Group
//
//var platform_id int
//platform_id = user.UserInfo.PlatformId
//if ("administrator" == group) {
// if arg := c.Query("platform_id"); arg != "" {
// platform_id = com.StrTo(c.Query("platform_id")).MustInt()
// }
//}
//
//infoService := live_info_service.LiveInfo{
// PlatformId: int64(platform_id),
// PageNum: util.GetPage(c),
// PageSize: util.GetLimit(c),
//}
//
//total, err := infoService.Count()
//if err != nil {
// handler.SendResponse(c, errno.ErrCountLiveInfo, nil)
// return
//}
//
//info, err := infoService.GetAll()
//if err != nil {
// handler.SendResponse(c, errno.InternalServerError, nil)
// return
//}
//
//data := make(map[string]interface{})
//data["items"] = info
//data["total"] = total
//
//handler.SendResponse(c, nil, data)
token := c.Request.Header.Get("Token")
user, _ := util.ParseToken(token)
group := user.UserInfo.Group
var platform_id int
platform_id = user.UserInfo.PlatformId
if ("administrator" == group) {
if arg := c.Query("platform_id"); arg != "" {
platform_id = com.StrTo(c.Query("platform_id")).MustInt()
}
}
infoService := live_info_service.LiveInfo{
PlatformId: int64(platform_id),
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
total, err := infoService.Count()
if err != nil {
handler.SendResponse(c, errno.ErrCountLiveInfo, nil)
return
}
info, err := infoService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
data := make(map[string]interface{})
data["items"] = info
data["total"] = total
handler.SendResponse(c, nil, data)
}
func AddLiveInfo(c *gin.Context) {
......@@ -75,6 +77,30 @@ func AddLiveInfo(c *gin.Context) {
handler.SendResponse(c, nil, nil)
}
func VerifyLiveInfo(c *gin.Context) {
info := validate_service.VerifyLiveInfo{}
c.ShouldBindJSON(&info)
if ok, errors := validate_service.ValidateInputs(info); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
}
}
infoService := live_info_service.LiveInfo{
Id: info.Id,
Status: info.Status,
}
if err := infoService.Verify(); err != nil {
handler.SendResponse(c, errno.ErrUpdateLiveInfo, nil)
return
}
handler.SendResponse(c, nil, nil)
}
func EditLiveInfo(c *gin.Context) {
info := validate_service.EditLiveInfo{}
c.ShouldBindJSON(&info)
......
......@@ -111,6 +111,8 @@ func InitRouter() *gin.Engine {
api.DELETE("/live-category", backend.DeleteLiveCategory)
api.POST("/live", backend.AddLiveInfo)
api.GET("/live-info", backend.GetLiveInfo)
api.PUT("/live-info", backend.VerifyLiveInfo)
}
return r
......
......@@ -15,29 +15,30 @@ type LiveInfo struct {
ApplyId uint32
Status uint8
Sort uint8
PlatformId int64
PageNum int
PageSize int
}
func (l *LiveInfo) GetOneLiveInfo() (*models.LiveInfo, error) {
category, err := models.GetOneLiveInfo(l.Id)
live, err := models.GetOneLiveInfo(l.Id)
if err != nil {
return nil, err
}
return category, nil
return live, nil
}
func (l *LiveInfo) GetAll() ([]*models.LiveInfo, error) {
var article []*models.LiveInfo
var live []*models.LiveInfo
article, err := models.GetLiveInfo(l.PageNum, l.PageSize, l.getMaps())
live, err := models.GetLiveInfo(l.PageNum, l.PageSize, l.getMaps())
if err != nil {
return nil, err
}
return article, nil
return live, nil
}
func (l *LiveInfo) Add() error {
......@@ -50,6 +51,7 @@ func (l *LiveInfo) Add() error {
"mode": l.Mode,
"apply_id": l.ApplyId,
"sort": l.Sort,
"platform_id": l.PlatformId,
}
if err := models.AddLiveInfo(live); err != nil {
......@@ -59,6 +61,12 @@ func (l *LiveInfo) Add() error {
return nil
}
func (l *LiveInfo) Verify() error {
return models.EditLiveInfo(l.Id, map[string]interface{}{
"status": l.Status,
})
}
func (l *LiveInfo) Edit() error {
return models.EditLiveInfo(l.Id, map[string]interface{}{
"title": l.Title,
......@@ -70,6 +78,7 @@ func (l *LiveInfo) Edit() error {
"apply_id": l.ApplyId,
"status": l.Status,
"sort": l.Sort,
"platform_id": l.PlatformId,
})
}
......@@ -100,5 +109,9 @@ func (l *LiveInfo) getMaps() (map[string]interface{}) {
maps["category_id"] = l.CategoryId
}
if l.PlatformId != 0 {
maps["platform_id"] = l.PlatformId
}
return maps
}
......@@ -9,9 +9,15 @@ type LiveInfo struct {
Mode uint8 `json:"mode" validate:"required"`
ApplyId uint32 `json:"apply_id" validate:"required"`
Sort uint8 `json:"sort" validate:"required"`
PlatformId int64 `json:"platform_id" validate:"required"`
}
type EditLiveInfo struct {
LiveInfo
Id int `json:"id" validate:"required"`
}
type VerifyLiveInfo struct {
Id int `json:"id" validate:"required"`
Status uint8 `json:"status" validate:"min=1,max=2"`
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment