channel.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package http
  2. import (
  3. "go-common/app/admin/main/tv/model"
  4. "go-common/library/ecode"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/jinzhu/gorm"
  8. )
  9. func chlInfo(c *bm.Context) {
  10. var (
  11. req = c.Request.Form
  12. vid = parseInt(req.Get("id"))
  13. err error
  14. )
  15. exist := model.ChannelFmt{}
  16. if err = tvSrv.DB.Where("id=?", vid).Where("deleted!=?", _isDeleted).First(&exist).Error; err != nil {
  17. c.JSON(nil, err)
  18. return
  19. }
  20. exist.MtimeFormat = tvSrv.TimeFormat(exist.Mtime)
  21. exist.Mtime = 0
  22. c.JSON(exist, nil)
  23. }
  24. func chlList(c *bm.Context) {
  25. param := new(model.ReqChannel)
  26. if err := c.Bind(param); err != nil {
  27. return
  28. }
  29. c.JSON(tvSrv.ChlSplash(c, param))
  30. }
  31. func chlEdit(c *bm.Context) {
  32. var (
  33. req = c.Request.PostForm
  34. vid = parseInt(req.Get("id"))
  35. allowed bool
  36. err error
  37. )
  38. exist := model.Channel{}
  39. if err = tvSrv.DB.Where("id=?", vid).Where("deleted!=?", _isDeleted).First(&exist).Error; err != nil {
  40. c.JSON(nil, err)
  41. return
  42. }
  43. alert, simple := validateChl(c)
  44. if alert != "" {
  45. renderErrMsg(c, ecode.RequestErr.Code(), alert)
  46. return
  47. }
  48. if allowed, _ = nameExist(simple.Title, int(vid)); !allowed {
  49. renderErrMsg(c, ecode.RequestErr.Code(), "Title exists")
  50. return
  51. }
  52. if err = tvSrv.DB.Model(&model.Channel{}).Where("id=?", vid).Update(simple).Error; err != nil {
  53. log.Error("tvSrv.saveChannel error(%v)", err)
  54. c.JSON(nil, err)
  55. return
  56. }
  57. c.JSON(nil, nil)
  58. }
  59. func chlAdd(c *bm.Context) {
  60. var (
  61. err error
  62. allowed bool
  63. )
  64. alert, simple := validateChl(c)
  65. if alert != "" {
  66. renderErrMsg(c, ecode.RequestErr.Code(), alert)
  67. return
  68. }
  69. if allowed, _ = nameExist(simple.Title, 0); !allowed {
  70. renderErrMsg(c, ecode.RequestErr.Code(), _errTitleExist)
  71. return
  72. }
  73. if err = tvSrv.DB.Create(simple).Error; err != nil {
  74. log.Error("tvSrv.addChannel error(%v)", err)
  75. c.JSON(nil, err)
  76. return
  77. }
  78. c.JSON(nil, nil)
  79. }
  80. func chlDel(c *bm.Context) {
  81. var (
  82. req = c.Request.PostForm
  83. vid = parseInt(req.Get("id"))
  84. err error
  85. )
  86. exist := model.Channel{}
  87. if err = tvSrv.DB.Where("id=?", vid).Where("deleted!=?", _isDeleted).First(&exist).Error; err != nil {
  88. c.JSON(nil, err)
  89. return
  90. }
  91. if err = tvSrv.DB.Model(&model.Channel{}).Where("id=?", vid).Update(map[string]int{"deleted": _isDeleted}).Error; err != nil {
  92. log.Error("tvSrv.chlDel error(%v)", err)
  93. c.JSON(nil, err)
  94. return
  95. }
  96. c.JSON(nil, nil)
  97. }
  98. // avoid two same names in DB - conflict
  99. func nameExist(name string, myID int) (allowed bool, err error) {
  100. var (
  101. exist = model.Channel{}
  102. db = tvSrv.DB.Where("title = ?", name).Where("deleted!=?", _isDeleted)
  103. )
  104. if myID != 0 {
  105. db = db.Where("id != ?", myID)
  106. }
  107. if err = db.First(&exist).Error; err != nil {
  108. if err != gorm.ErrRecordNotFound {
  109. log.Error("tvSrv.nameExist error(%v)", err)
  110. return
  111. }
  112. return true, nil
  113. }
  114. return false, nil
  115. }
  116. // validate Channel params
  117. func validateChl(c *bm.Context) (alert string, simple *model.Channel) {
  118. var (
  119. req = c.Request.PostForm
  120. title = req.Get("title")
  121. desc = req.Get("desc")
  122. splash = req.Get("splash")
  123. )
  124. if title == "" {
  125. alert = "Channel Title can't be empty"
  126. return
  127. }
  128. if splash == "" {
  129. alert = "Splash can't be empty"
  130. return
  131. }
  132. return "", &model.Channel{
  133. Title: title,
  134. Desc: desc,
  135. Splash: splash,
  136. }
  137. }