actions.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package note
  2. import (
  3. "github.com/qor5/admin/presets"
  4. "github.com/qor5/web"
  5. "github.com/qor5/x/i18n"
  6. "gorm.io/gorm"
  7. )
  8. func createNoteAction(db *gorm.DB, mb *presets.ModelBuilder) web.EventFunc {
  9. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  10. ri := ctx.R.FormValue("resource_id")
  11. rt := ctx.R.FormValue("resource_type")
  12. content := ctx.R.FormValue("Content")
  13. userID, creator := GetUserData(ctx)
  14. note := QorNote{
  15. UserID: userID,
  16. Creator: creator,
  17. ResourceID: ri,
  18. ResourceType: rt,
  19. Content: content,
  20. }
  21. if err = db.Save(&note).Error; err != nil {
  22. presets.ShowMessage(&r, err.Error(), "error")
  23. err = nil
  24. return
  25. }
  26. userNote := UserNote{UserID: userID, ResourceType: rt, ResourceID: ri}
  27. db.Where(userNote).FirstOrCreate(&userNote)
  28. var total int64
  29. db.Model(&QorNote{}).Where("resource_type = ? AND resource_id = ?", rt, ri).Count(&total)
  30. db.Model(&userNote).UpdateColumn("Number", total)
  31. msgr := i18n.MustGetModuleMessages(ctx.R, I18nNoteKey, Messages_en_US).(*Messages)
  32. presets.ShowMessage(&r, msgr.SuccessfullyCreated, "")
  33. notesSection := getNotesTab(ctx, db, rt, ri)
  34. r.UpdatePortals = append(r.UpdatePortals, &web.PortalUpdate{
  35. Name: "notes-section",
  36. Body: notesSection,
  37. })
  38. if err = AfterCreateFunc(db); err != nil {
  39. return
  40. }
  41. return
  42. }
  43. }
  44. func updateUserNoteAction(db *gorm.DB, mb *presets.ModelBuilder) web.EventFunc {
  45. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  46. ri := ctx.R.FormValue("resource_id")
  47. rt := ctx.R.FormValue("resource_type")
  48. userID, _ := GetUserData(ctx)
  49. if userID == 0 {
  50. return
  51. }
  52. userNote := UserNote{UserID: userID, ResourceType: rt, ResourceID: ri}
  53. if err = db.Where(userNote).FirstOrCreate(&userNote).Error; err != nil {
  54. return
  55. }
  56. var total int64
  57. db.Model(&QorNote{}).Where("resource_type = ? AND resource_id = ?", rt, ri).Count(&total)
  58. userNote.Number = total
  59. if err = db.Save(&userNote).Error; err != nil {
  60. return
  61. }
  62. if err = AfterCreateFunc(db); err != nil {
  63. return
  64. }
  65. // notify notification center after note read. if notification center is not enabled, this one would just do nothing
  66. r.ReloadPortals = append(r.ReloadPortals, presets.NotificationCenterPortalName)
  67. return
  68. }
  69. }