Commit 57dfa249 authored by shajiaiming's avatar shajiaiming

直播间名称更新相关接口

parent a7e14536
......@@ -104,6 +104,14 @@ func EditLiveInfo(id int, data interface{}) error {
return nil
}
func TitleUpdate(id int, data interface{}) error {
if err := db.Model(&LiveInfo{}).Where("id = ?", id).Updates(data).Error; err != nil {
return err
}
return nil
}
func DeleteLiveInfo(id int) error {
if err := db.Where("id = ?", id).Delete(LiveInfo{}).Error; err != nil {
return err
......
package models
import (
"bwallet/pkg/setting"
"github.com/jinzhu/gorm"
)
type LiveTitleRecord struct {
Model
LiveId int `json:"live_id"`
CurrentTitle string `json:"current_title"`
NewTitle string `json:"new_title"`
Status uint8 `json:"status"`
PlatformId int64 `gorm:"not null;default:0" json:"platform_id"`
LiveInfo *LiveInfo `gorm:"foreignkey:LiveId" json:"live"`
}
func (w LiveTitleRecord) TableName() string {
return setting.DatabaseSetting.Name_Sources + ".wallet_live_title_record"
}
func GetOneLiveTitleRecord(id int) (*LiveTitleRecord, error) {
var live LiveTitleRecord
err := db.Where("id = ?", id).First(&live).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
err = db.Model(&live).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return &live, nil
}
func ExistLiveTitleRecordById(id int) (bool, error) {
var live LiveTitleRecord
err := db.Select("id").Where("id = ?", id).First(&live).Error
if err != nil && err != gorm.ErrRecordNotFound {
return false, err
}
if live.ID > 0 {
return true, nil
}
return false, nil
}
func GetLiveTitleRecordTotal(maps interface{}) (int, error) {
var count int
if err := db.Model(&LiveTitleRecord{}).Where(maps).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
func GetLiveTitleRecord(pageNum, pageSize int, maps interface{}) ([]*LiveTitleRecord, error) {
var live []*LiveTitleRecord
err := db.Where(maps).Preload("LiveInfo").Order("create_time desc").Offset(pageNum).Limit(pageSize).Find(&live).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return live, nil
}
func AddLiveTitleRecord(data map[string]interface{}) (error) {
live := LiveTitleRecord{
LiveId: data["live_id"].(int),
CurrentTitle: data["current_title"].(string),
NewTitle: data["new_title"].(string),
Status: data["sort"].(uint8),
PlatformId: data["platform_id"].(int64),
}
if err := db.Create(&live).Error; err != nil {
return err
}
return nil
}
func EditLiveTitleRecord(id int, data interface{}) error {
if err := db.Model(&LiveTitleRecord{}).Where("id = ?", id).Updates(data).Error; err != nil {
return err
}
return nil
}
\ No newline at end of file
......@@ -163,4 +163,12 @@ var (
ErrUpdateLiveInfo = &Errno{Code: 20102, Message: "The live info update error."}
ErrDeleteLiveInfo = &Errno{Code: 20102, Message: "The live info delete error."}
ErrExistLiveInfo = &Errno{Code: 20103, Message: "The live info already exists."}
// live title record errors
ErrLiveTitleRecordNotFound = &Errno{Code: 20101, Message: "The live title record was not found."}
ErrCountLiveTitleRecord = &Errno{Code: 20102, Message: "The live title record statistic error."}
ErrAddLiveTitleRecord = &Errno{Code: 20101, Message: "The live title record add error."}
ErrUpdateLiveTitleRecord = &Errno{Code: 20102, Message: "The live title record update error."}
ErrDeleteLiveTitleRecord = &Errno{Code: 20102, Message: "The live title record delete error."}
ErrExistLiveTitleRecord = &Errno{Code: 20103, Message: "The live title record already exists."}
)
......@@ -24,7 +24,10 @@ func GetLiveInfo(c *gin.Context) {
}
}
status := com.StrTo(c.DefaultQuery("status", "0")).MustUint8()
infoService := live_info_service.LiveInfo{
Status: status,
PlatformId: int64(platform_id),
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
......@@ -100,75 +103,3 @@ func VerifyLiveInfo(c *gin.Context) {
handler.SendResponse(c, nil, nil)
}
func EditLiveInfo(c *gin.Context) {
info := validate_service.EditLiveInfo{}
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,
Title: info.Title,
Cover: info.Cover,
CategoryId: info.CategoryId,
Duration: info.Duration,
Attendance: info.Attendance,
Mode: info.Mode,
ApplyId: info.ApplyId,
Sort: info.Sort,
}
if err := infoService.Edit(); err != nil {
handler.SendResponse(c, errno.ErrUpdateLiveInfo, nil)
return
}
handler.SendResponse(c, nil, nil)
}
func DeleteLiveInfo(c *gin.Context) {
//id := com.StrTo(c.DefaultQuery("id", "0")).MustInt()
//valid := validation.Validation{}
//valid.Min(id, 1, "id").Message("Id必须大于0")
//if valid.HasErrors() {
// handler.SendResponse(c, valid.Errors[0], nil)
// return
//}
//
//infoService := live_info_service.LiveInfo{Id: id}
//exists, err := infoService.ExistById()
//if err != nil {
// handler.SendResponse(c, errno.ErrLiveInfoNotFound, nil)
// return
//}
//
//if !exists {
// handler.SendResponse(c, errno.ErrLiveInfoNotFound, nil)
// return
//}
//
//token := c.Request.Header.Get("Token")
//user, _ := util.ParseToken(token)
//group := user.UserInfo.Group
//if ("administrator" != group) {
// info, _ := infoService.GetOneLiveInfo()
// if info.PlatformId != int64(user.UserInfo.PlatformId) {
// handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
// return
// }
//}
//err = infoService.Delete()
//if err != nil {
// handler.SendResponse(c, errno.ErrDeleteLiveInfo, nil)
// return
//}
//
//handler.SendResponse(c, nil, nil)
}
package backend
import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"bwallet/service/live_info_service"
"bwallet/service/live_title_record_service"
"bwallet/validate_service"
"github.com/Unknwon/com"
"github.com/gin-gonic/gin"
"strings"
)
func GetLiveTitleRecord(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()
}
}
status := com.StrTo(c.DefaultQuery("status", "0")).MustUint8()
recordService := live_title_record_service.LiveTitleRecord{
Status: status,
PlatformId: int64(platform_id),
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
total, err := recordService.Count()
if err != nil {
handler.SendResponse(c, errno.ErrCountLiveTitleRecord, nil)
return
}
record, err := recordService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
data := make(map[string]interface{})
data["items"] = record
data["total"] = total
handler.SendResponse(c, nil, data)
}
func VerifyLiveTitleRecord(c *gin.Context) {
record := validate_service.VerifyLiveTitleRecord{}
c.ShouldBindJSON(&record)
if ok, errors := validate_service.ValidateInputs(record); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
}
}
recordService := live_title_record_service.LiveTitleRecord{
Id: record.Id,
Status: record.Status,
}
if err := recordService.Verify(); err != nil {
handler.SendResponse(c, errno.ErrUpdateLiveTitleRecord, nil)
return
}
if 1 == record.Status {
recordService := live_title_record_service.LiveTitleRecord{
Id: record.Id,
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
_, err := recordService.Count()
if err != nil {
handler.SendResponse(c, errno.ErrCountLiveTitleRecord, nil)
return
}
record, err := recordService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
infoService := live_info_service.LiveInfo{
Id: record[0].LiveId,
Title: record[0].NewTitle,
}
if err := infoService.TitleUpdate(); err != nil {
handler.SendResponse(c, errno.ErrUpdateLiveInfo, nil)
return
}
}
handler.SendResponse(c, nil, nil)
}
......@@ -118,6 +118,8 @@ func InitRouter() *gin.Engine {
api.POST("/live", backend.AddLiveInfo)
api.GET("/live-info", backend.GetLiveInfo)
api.PUT("/live-info", backend.VerifyLiveInfo)
api.GET("/live-title-record", backend.GetLiveTitleRecord)
api.PUT("/live-title-record", backend.VerifyLiveTitleRecord)
}
return r
......
......@@ -82,6 +82,12 @@ func (l *LiveInfo) Edit() error {
})
}
func (l *LiveInfo) TitleUpdate() error {
return models.TitleUpdate(l.Id, map[string]interface{}{
"title": l.Title,
})
}
func (l *LiveInfo) ExistById() (bool, error) {
return models.ExistLiveInfoById(l.Id)
}
......@@ -109,6 +115,10 @@ func (l *LiveInfo) getMaps() (map[string]interface{}) {
maps["category_id"] = l.CategoryId
}
if l.Status != 0 {
maps["status"] = l.Status
}
if l.PlatformId != 0 {
maps["platform_id"] = l.PlatformId
}
......
package live_title_record_service
import (
"bwallet/models"
)
type LiveTitleRecord struct {
Id int
LiveId int
CurrentTitle string
NewTitle string
Status uint8
PlatformId int64
PageNum int
PageSize int
}
func (l *LiveTitleRecord) GetAll() ([]*models.LiveTitleRecord, error) {
var live []*models.LiveTitleRecord
live, err := models.GetLiveTitleRecord(l.PageNum, l.PageSize, l.getMaps())
if err != nil {
return nil, err
}
return live, nil
}
func (l *LiveTitleRecord) Add() error {
live := map[string]interface{}{
"live_id": l.LiveId,
"current_title": l.CurrentTitle,
"new_title": l.NewTitle,
"platform_id": l.PlatformId,
}
if err := models.AddLiveTitleRecord(live); err != nil {
return err
}
return nil
}
func (l *LiveTitleRecord) Verify() error {
return models.EditLiveTitleRecord(l.Id, map[string]interface{}{
"status": l.Status,
})
}
func (l *LiveTitleRecord) Edit() error {
return models.EditLiveTitleRecord(l.Id, map[string]interface{}{
"new_title": l.NewTitle,
})
}
func (l *LiveTitleRecord) ExistById() (bool, error) {
return models.ExistLiveTitleRecordById(l.Id)
}
func (l *LiveTitleRecord) Count() (int, error) {
return models.GetLiveTitleRecordTotal(l.getMaps())
}
func (l *LiveTitleRecord) getMaps() (map[string]interface{}) {
maps := make(map[string]interface{})
if l.Id != 0 {
maps["id"] = l.Id
}
if l.Status != 0 {
maps["status"] = l.Status
}
if l.PlatformId != 0 {
maps["platform_id"] = l.PlatformId
}
return maps
}
package validate_service
type LiveTitleRecord struct {
LiveId int `json:"live_id" validate:"required"`
CurrentTitle string `json:"current_title" validate:"required"`
NewTitle string `json:"new_title" validate:"required"`
PlatformId int64 `json:"platform_id" validate:"required"`
}
type VerifyLiveTitleRecord 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