mysql_hook_url.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "go-common/app/admin/ep/tapd/model"
  4. "go-common/library/ecode"
  5. )
  6. const _wildcards = "%"
  7. // AddHookURL Add Hook URL.
  8. func (d *Dao) AddHookURL(hookURL *model.HookUrl) error {
  9. return d.db.Create(hookURL).Error
  10. }
  11. // UpdateHookURL Update Hook URL.
  12. func (d *Dao) UpdateHookURL(hookURL *model.HookUrl) error {
  13. return d.db.Model(&model.HookUrl{}).Where("id=?", hookURL.ID).Update(hookURL).Error
  14. }
  15. // QueryHookURLByID Query Hook URL By ID.
  16. func (d *Dao) QueryHookURLByID(id int64) (hookURL *model.HookUrl, err error) {
  17. hookURL = &model.HookUrl{}
  18. err = d.db.Model(&model.HookUrl{}).Where("id = ?", id).First(hookURL).Error
  19. if err == ecode.NothingFound {
  20. err = nil
  21. }
  22. return
  23. }
  24. // AddHookURLandEvent Add Hook URL and Event.
  25. func (d *Dao) AddHookURLandEvent(hookURL *model.HookUrl, urlEvents []*model.UrlEvent) (err error) {
  26. tx := d.db.Begin()
  27. if err = tx.Error; err != nil {
  28. return
  29. }
  30. if err = tx.Create(hookURL).Error; err != nil {
  31. tx.Rollback()
  32. return
  33. }
  34. for _, urlEvent := range urlEvents {
  35. urlEvent.UrlID = hookURL.ID
  36. if err = tx.Create(urlEvent).Error; err != nil {
  37. tx.Rollback()
  38. return
  39. }
  40. }
  41. if err = tx.Commit().Error; err != nil {
  42. tx.Rollback()
  43. }
  44. return
  45. }
  46. // UpdateHookURLandEvent Update Hook URL and Event.
  47. func (d *Dao) UpdateHookURLandEvent(hookURL *model.HookUrl, urlEvents []*model.UrlEvent) (err error) {
  48. tx := d.db.Begin()
  49. if err = tx.Error; err != nil {
  50. return
  51. }
  52. if err = tx.Model(model.HookUrl{}).Where("id=?", hookURL.ID).
  53. Updates(map[string]interface{}{"url": hookURL.URL, "workspace_id": hookURL.WorkspaceID, "status": hookURL.Status, "update_by": hookURL.UpdateBy}).
  54. Error; err != nil {
  55. tx.Rollback()
  56. return
  57. }
  58. for _, urlEvent := range urlEvents {
  59. if urlEvent.ID != 0 {
  60. //update
  61. if err = tx.Model(model.UrlEvent{}).Where("id=?", urlEvent.ID).Update(urlEvent).Error; err != nil {
  62. tx.Rollback()
  63. return
  64. }
  65. } else {
  66. //add
  67. if err = tx.Create(urlEvent).Error; err != nil {
  68. tx.Rollback()
  69. return
  70. }
  71. }
  72. }
  73. if err = tx.Commit().Error; err != nil {
  74. tx.Rollback()
  75. }
  76. return
  77. }
  78. //FindHookURLs Find Hook URLs.
  79. func (d *Dao) FindHookURLs(req *model.QueryHookURLReq) (total int64, hookURLs []*model.HookUrl, err error) {
  80. gDB := d.db.Model(&model.HookUrl{})
  81. if req.ID > 0 {
  82. gDB = gDB.Where("id=?", req.ID)
  83. }
  84. if req.Status > 0 {
  85. gDB = gDB.Where("status=?", req.Status)
  86. }
  87. if req.UpdateBy != "" {
  88. gDB = gDB.Where("update_by=?", req.UpdateBy)
  89. }
  90. if req.URL != "" {
  91. gDB = gDB.Where("url like ?", req.URL+_wildcards)
  92. }
  93. if err = gDB.Count(&total).Error; err != nil {
  94. return
  95. }
  96. err = gDB.Order("ctime desc").Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Find(&hookURLs).Error
  97. return
  98. }