Commit 2e664c09 authored by shajiaiming's avatar shajiaiming

test

parent e194c81b
package models
import (
"bwallet/pkg/setting"
"github.com/jinzhu/gorm"
)
type LiveInfo struct {
Model
Title string `gorm:"not null;default:''" json:"title"`
Cover string `gorm:"not null;default:''" json:"cover"`
CategoryId uint8 `gorm:"not null;default:0" json:"category_id"`
Duration uint32 `gorm:"not null;default:0" json:"duration"`
Attendance uint32 `gorm:"not null;default:0" json:"attendance"`
Mode uint8 `gorm:"not null;default:1" json:"mode"`
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"`
}
func (w LiveInfo) TableName() string {
return setting.DatabaseSetting.Name_Sources + ".wallet_live_info"
}
func GetOneLiveInfo(id int) (*LiveInfo, error) {
var live LiveInfo
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 ExistLiveInfoById(id int) (bool, error) {
var live LiveInfo
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 GetLiveInfoTotal(maps interface{}) (int, error) {
var count int
if err := db.Model(&LiveInfo{}).Where(maps).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
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
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return live, nil
}
func AddLiveInfo(data map[string]interface{}) (error) {
live := LiveInfo{
Title: data["title"].(string),
Cover: data["cover"].(string),
CategoryId: data["category_id"].(uint8),
Duration: data["duration"].(uint32),
Attendance: data["attendance"].(uint32),
Mode: data["mode"].(uint8),
ApplyId: data["apply_id"].(uint32),
Status: data["status"].(uint8),
Sort: data["sort"].(uint8),
}
if err := db.Create(&live).Error; err != nil {
return err
}
return nil
}
func EditLiveInfo(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
}
return nil
}
......@@ -155,4 +155,12 @@ var (
ErrUpdateLiveCategory = &Errno{Code: 20102, Message: "The live category update error."}
ErrDeleteLiveCategory = &Errno{Code: 20102, Message: "The live category delete error."}
ErrExistLiveCategory = &Errno{Code: 20103, Message: "The live category already exists."}
// live info errors
ErrLiveInfoNotFound = &Errno{Code: 20101, Message: "The live info was not found."}
ErrCountLiveInfo = &Errno{Code: 20102, Message: "The live info statistic error."}
ErrAddLiveInfo = &Errno{Code: 20101, Message: "The live info add error."}
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."}
)
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/astaxie/beego/validation"
"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)
}
func AddLiveInfo(c *gin.Context) {
info := validate_service.LiveInfo{}
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{
Title: info.Title,
Cover: info.Cover,
CategoryId: info.CategoryId,
Duration: info.Duration,
Attendance: info.Attendance,
Mode: info.Mode,
ApplyId: info.ApplyId,
Sort: info.Sort,
}
infoService.Add()
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 live_info_service
import (
"bwallet/models"
)
type LiveInfo struct {
Id int
Title string
Cover string
CategoryId uint8
Duration uint32
Attendance uint32
Mode uint8
ApplyId uint32
Status uint8
Sort uint8
PageNum int
PageSize int
}
func (l *LiveInfo) GetOneLiveInfo() (*models.LiveInfo, error) {
category, err := models.GetOneLiveInfo(l.Id)
if err != nil {
return nil, err
}
return category, nil
}
func (l *LiveInfo) GetAll() ([]*models.LiveInfo, error) {
var article []*models.LiveInfo
article, err := models.GetLiveInfo(l.PageNum, l.PageSize, l.getMaps())
if err != nil {
return nil, err
}
return article, nil
}
func (l *LiveInfo) Add() error {
category := map[string]interface{}{
"title": l.Title,
"cover": l.Cover,
"category_id": l.CategoryId,
"duration": l.Duration,
"attendance": l.Attendance,
"mode": l.Mode,
"apply_id": l.ApplyId,
"sort": l.Sort,
}
if err := models.AddLiveInfo(category); err != nil {
return err
}
return nil
}
func (l *LiveInfo) Edit() error {
return models.EditLiveInfo(l.Id, map[string]interface{}{
"title": l.Title,
"cover": l.Cover,
"category_id": l.CategoryId,
"duration": l.Duration,
"attendance": l.Attendance,
"mode": l.Mode,
"apply_id": l.ApplyId,
"status": l.Status,
"sort": l.Sort,
})
}
func (l *LiveInfo) ExistById() (bool, error) {
return models.ExistLiveInfoById(l.Id)
}
func (l *LiveInfo) Count() (int, error) {
return models.GetLiveInfoTotal(l.getMaps())
}
func (l *LiveInfo) Delete() error {
return models.DeleteLiveInfo(l.Id)
}
func (l *LiveInfo) getMaps() (map[string]interface{}) {
maps := make(map[string]interface{})
if l.Id != 0 {
maps["id"] = l.Id
}
if l.Title != "" {
maps["title"] = l.Title
}
if l.CategoryId != 0 {
maps["category_id"] = l.CategoryId
}
return maps
}
package validate_service
type LiveInfo struct {
Title string `json:"title" validate:"required"`
Cover string `json:"cover" validate:"required"`
CategoryId uint8 `json:"category_id" validate:"required"`
Duration uint32 `json:"duration" validate:"required"`
Attendance uint32 `json:"attendance" validate:"required"`
Mode uint8 `json:"mode" validate:"required"`
ApplyId uint32 `json:"apply_id" validate:"required"`
Sort uint8 `json:"sort" validate:"required"`
}
type EditLiveInfo struct {
LiveInfo
Id int `json:"id" validate:"required"`
}
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