apps.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. "go-common/app/admin/main/push/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. func appList(c *bm.Context) {
  11. var (
  12. err error
  13. items []*model.App
  14. )
  15. if err = pushSrv.DB.Find(&items).Error; err != nil {
  16. log.Error("appList error(%v)", err)
  17. c.JSON(nil, err)
  18. return
  19. }
  20. c.JSON(items, nil)
  21. }
  22. func addApp(c *bm.Context) {
  23. app := new(model.App)
  24. if err := c.Bind(app); err != nil {
  25. return
  26. }
  27. if !pushSrv.DB.Where("name=?", app.Name).First(&model.App{}).RecordNotFound() {
  28. log.Warn("addApp(%+v) repeat", app)
  29. c.JSON(nil, ecode.PushRecordRepeatErr)
  30. return
  31. }
  32. if err := pushSrv.DB.Create(app).Error; err != nil {
  33. log.Error("addApp(%s) error(%v)", app.Name, err)
  34. c.JSON(nil, err)
  35. return
  36. }
  37. c.JSON(nil, nil)
  38. }
  39. func appInfo(c *bm.Context) {
  40. var (
  41. req = c.Request.Form
  42. info = &model.App{}
  43. )
  44. id, _ := strconv.ParseInt(req.Get("id"), 10, 64)
  45. if id == 0 {
  46. c.JSON(nil, ecode.RequestErr)
  47. return
  48. }
  49. if err := pushSrv.DB.First(info, id).Error; err != nil {
  50. log.Error("appInfo(%d) error(%v)", id, err)
  51. c.JSON(nil, err)
  52. return
  53. }
  54. c.JSON(info, nil)
  55. }
  56. func saveApp(c *bm.Context) {
  57. app := new(model.App)
  58. if err := c.Bind(app); err != nil {
  59. return
  60. }
  61. if err := pushSrv.DB.Model(&model.App{ID: app.ID}).Updates(map[string]interface{}{"name": app.Name, "push_limit_user": app.PushLimitUser}).Error; err != nil {
  62. log.Error("saveApp(%d,%s) error(%v)", app.ID, app.Name, err)
  63. c.JSON(nil, err)
  64. return
  65. }
  66. c.JSON(nil, nil)
  67. }
  68. func delApp(c *bm.Context) {
  69. req := c.Request.Form
  70. id, _ := strconv.ParseInt(req.Get("id"), 10, 64)
  71. if id == 0 {
  72. c.JSON(nil, ecode.RequestErr)
  73. return
  74. }
  75. if err := pushSrv.DB.Model(&model.App{ID: id}).Update("dtime", time.Now().Unix()).Error; err != nil {
  76. log.Error("delApps(%d) error(%v)", id, err)
  77. c.JSON(nil, err)
  78. return
  79. }
  80. c.JSON(nil, nil)
  81. }