Commit 3221e79d authored by shajiaiming's avatar shajiaiming

直播间api

parent 78f35215
...@@ -17,6 +17,9 @@ type LiveInfo struct { ...@@ -17,6 +17,9 @@ type LiveInfo struct {
ApplyId uint32 `gorm:"not null;default:0" json:"apply_id"` ApplyId uint32 `gorm:"not null;default:0" json:"apply_id"`
Status uint8 `gorm:"not null;default:0" json:"status"` Status uint8 `gorm:"not null;default:0" json:"status"`
Sort uint8 `gorm:"not null;default:0" json:"sort"` 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 { func (w LiveInfo) TableName() string {
...@@ -65,7 +68,7 @@ func GetLiveInfoTotal(maps interface{}) (int, error) { ...@@ -65,7 +68,7 @@ func GetLiveInfoTotal(maps interface{}) (int, error) {
func GetLiveInfo(pageNum, pageSize int, maps interface{}) ([]*LiveInfo, error) { func GetLiveInfo(pageNum, pageSize int, maps interface{}) ([]*LiveInfo, error) {
var live []*LiveInfo 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 { if err != nil && err != gorm.ErrRecordNotFound {
return nil, err return nil, err
} }
...@@ -83,6 +86,7 @@ func AddLiveInfo(data map[string]interface{}) (error) { ...@@ -83,6 +86,7 @@ func AddLiveInfo(data map[string]interface{}) (error) {
Mode: data["mode"].(uint8), Mode: data["mode"].(uint8),
ApplyId: data["apply_id"].(uint32), ApplyId: data["apply_id"].(uint32),
Sort: data["sort"].(uint8), Sort: data["sort"].(uint8),
PlatformId: data["platform_id"].(int64),
} }
if err := db.Create(&live).Error; err != nil { if err := db.Create(&live).Error; err != nil {
......
...@@ -3,48 +3,50 @@ package backend ...@@ -3,48 +3,50 @@ package backend
import ( import (
"bwallet/pkg/errno" "bwallet/pkg/errno"
"bwallet/pkg/handler" "bwallet/pkg/handler"
"bwallet/pkg/util"
"bwallet/service/live_info_service" "bwallet/service/live_info_service"
"bwallet/validate_service" "bwallet/validate_service"
"github.com/Unknwon/com"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strings" "strings"
) )
func GetLiveInfo(c *gin.Context) { func GetLiveInfo(c *gin.Context) {
//token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
//user, _ := util.ParseToken(token) user, _ := util.ParseToken(token)
//group := user.UserInfo.Group group := user.UserInfo.Group
//
//var platform_id int var platform_id int
//platform_id = user.UserInfo.PlatformId platform_id = user.UserInfo.PlatformId
//if ("administrator" == group) { if ("administrator" == group) {
// if arg := c.Query("platform_id"); arg != "" { if arg := c.Query("platform_id"); arg != "" {
// platform_id = com.StrTo(c.Query("platform_id")).MustInt() platform_id = com.StrTo(c.Query("platform_id")).MustInt()
// } }
//} }
//
//infoService := live_info_service.LiveInfo{ infoService := live_info_service.LiveInfo{
// PlatformId: int64(platform_id), PlatformId: int64(platform_id),
// PageNum: util.GetPage(c), PageNum: util.GetPage(c),
// PageSize: util.GetLimit(c), PageSize: util.GetLimit(c),
//} }
//
//total, err := infoService.Count() total, err := infoService.Count()
//if err != nil { if err != nil {
// handler.SendResponse(c, errno.ErrCountLiveInfo, nil) handler.SendResponse(c, errno.ErrCountLiveInfo, nil)
// return return
//} }
//
//info, err := infoService.GetAll() info, err := infoService.GetAll()
//if err != nil { if err != nil {
// handler.SendResponse(c, errno.InternalServerError, nil) handler.SendResponse(c, errno.InternalServerError, nil)
// return return
//} }
//
//data := make(map[string]interface{}) data := make(map[string]interface{})
//data["items"] = info data["items"] = info
//data["total"] = total data["total"] = total
//
//handler.SendResponse(c, nil, data) handler.SendResponse(c, nil, data)
} }
func AddLiveInfo(c *gin.Context) { func AddLiveInfo(c *gin.Context) {
...@@ -75,6 +77,30 @@ func AddLiveInfo(c *gin.Context) { ...@@ -75,6 +77,30 @@ func AddLiveInfo(c *gin.Context) {
handler.SendResponse(c, nil, nil) 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) { func EditLiveInfo(c *gin.Context) {
info := validate_service.EditLiveInfo{} info := validate_service.EditLiveInfo{}
c.ShouldBindJSON(&info) c.ShouldBindJSON(&info)
......
...@@ -111,6 +111,8 @@ func InitRouter() *gin.Engine { ...@@ -111,6 +111,8 @@ func InitRouter() *gin.Engine {
api.DELETE("/live-category", backend.DeleteLiveCategory) api.DELETE("/live-category", backend.DeleteLiveCategory)
api.POST("/live", backend.AddLiveInfo) api.POST("/live", backend.AddLiveInfo)
api.GET("/live-info", backend.GetLiveInfo)
api.PUT("/live-info", backend.VerifyLiveInfo)
} }
return r return r
......
...@@ -15,29 +15,30 @@ type LiveInfo struct { ...@@ -15,29 +15,30 @@ type LiveInfo struct {
ApplyId uint32 ApplyId uint32
Status uint8 Status uint8
Sort uint8 Sort uint8
PlatformId int64
PageNum int PageNum int
PageSize int PageSize int
} }
func (l *LiveInfo) GetOneLiveInfo() (*models.LiveInfo, error) { func (l *LiveInfo) GetOneLiveInfo() (*models.LiveInfo, error) {
category, err := models.GetOneLiveInfo(l.Id) live, err := models.GetOneLiveInfo(l.Id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return category, nil return live, nil
} }
func (l *LiveInfo) GetAll() ([]*models.LiveInfo, error) { 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 { if err != nil {
return nil, err return nil, err
} }
return article, nil return live, nil
} }
func (l *LiveInfo) Add() error { func (l *LiveInfo) Add() error {
...@@ -50,6 +51,7 @@ func (l *LiveInfo) Add() error { ...@@ -50,6 +51,7 @@ func (l *LiveInfo) Add() error {
"mode": l.Mode, "mode": l.Mode,
"apply_id": l.ApplyId, "apply_id": l.ApplyId,
"sort": l.Sort, "sort": l.Sort,
"platform_id": l.PlatformId,
} }
if err := models.AddLiveInfo(live); err != nil { if err := models.AddLiveInfo(live); err != nil {
...@@ -59,6 +61,12 @@ func (l *LiveInfo) Add() error { ...@@ -59,6 +61,12 @@ func (l *LiveInfo) Add() error {
return nil return nil
} }
func (l *LiveInfo) Verify() error {
return models.EditLiveInfo(l.Id, map[string]interface{}{
"status": l.Status,
})
}
func (l *LiveInfo) Edit() error { func (l *LiveInfo) Edit() error {
return models.EditLiveInfo(l.Id, map[string]interface{}{ return models.EditLiveInfo(l.Id, map[string]interface{}{
"title": l.Title, "title": l.Title,
...@@ -70,6 +78,7 @@ func (l *LiveInfo) Edit() error { ...@@ -70,6 +78,7 @@ func (l *LiveInfo) Edit() error {
"apply_id": l.ApplyId, "apply_id": l.ApplyId,
"status": l.Status, "status": l.Status,
"sort": l.Sort, "sort": l.Sort,
"platform_id": l.PlatformId,
}) })
} }
...@@ -100,5 +109,9 @@ func (l *LiveInfo) getMaps() (map[string]interface{}) { ...@@ -100,5 +109,9 @@ func (l *LiveInfo) getMaps() (map[string]interface{}) {
maps["category_id"] = l.CategoryId maps["category_id"] = l.CategoryId
} }
if l.PlatformId != 0 {
maps["platform_id"] = l.PlatformId
}
return maps return maps
} }
...@@ -9,9 +9,15 @@ type LiveInfo struct { ...@@ -9,9 +9,15 @@ type LiveInfo struct {
Mode uint8 `json:"mode" validate:"required"` Mode uint8 `json:"mode" validate:"required"`
ApplyId uint32 `json:"apply_id" validate:"required"` ApplyId uint32 `json:"apply_id" validate:"required"`
Sort uint8 `json:"sort" validate:"required"` Sort uint8 `json:"sort" validate:"required"`
PlatformId int64 `json:"platform_id" validate:"required"`
} }
type EditLiveInfo struct { type EditLiveInfo struct {
LiveInfo LiveInfo
Id int `json:"id" validate:"required"` 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