storage.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "net/http"
  19. "github.com/88250/gulu"
  20. "github.com/gin-gonic/gin"
  21. "github.com/siyuan-note/siyuan/kernel/model"
  22. "github.com/siyuan-note/siyuan/kernel/util"
  23. )
  24. func removeCriterion(c *gin.Context) {
  25. ret := gulu.Ret.NewResult()
  26. defer c.JSON(http.StatusOK, ret)
  27. arg, ok := util.JsonArg(c, ret)
  28. if !ok {
  29. return
  30. }
  31. name := arg["name"].(string)
  32. err := model.RemoveCriterion(name)
  33. if nil != err {
  34. ret.Code = -1
  35. ret.Msg = err.Error()
  36. return
  37. }
  38. }
  39. func setCriterion(c *gin.Context) {
  40. ret := gulu.Ret.NewResult()
  41. defer c.JSON(http.StatusOK, ret)
  42. arg, ok := util.JsonArg(c, ret)
  43. if !ok {
  44. return
  45. }
  46. param, err := gulu.JSON.MarshalJSON(arg["criterion"])
  47. if nil != err {
  48. ret.Code = -1
  49. ret.Msg = err.Error()
  50. return
  51. }
  52. criterion := &model.Criterion{}
  53. if err = gulu.JSON.UnmarshalJSON(param, criterion); nil != err {
  54. ret.Code = -1
  55. ret.Msg = err.Error()
  56. return
  57. }
  58. err = model.SetCriterion(criterion)
  59. if nil != err {
  60. ret.Code = -1
  61. ret.Msg = err.Error()
  62. return
  63. }
  64. }
  65. func getCriteria(c *gin.Context) {
  66. ret := gulu.Ret.NewResult()
  67. defer c.JSON(http.StatusOK, ret)
  68. data, err := model.GetCriteria()
  69. if nil != err {
  70. ret.Code = -1
  71. ret.Msg = err.Error()
  72. return
  73. }
  74. ret.Data = data
  75. }
  76. func removeLocalStorageVal(c *gin.Context) {
  77. ret := gulu.Ret.NewResult()
  78. defer c.JSON(http.StatusOK, ret)
  79. arg, ok := util.JsonArg(c, ret)
  80. if !ok {
  81. return
  82. }
  83. key := arg["key"].(string)
  84. err := model.RemoveLocalStorageVal(key)
  85. if nil != err {
  86. ret.Code = -1
  87. ret.Msg = err.Error()
  88. return
  89. }
  90. }
  91. func setLocalStorageVal(c *gin.Context) {
  92. ret := gulu.Ret.NewResult()
  93. defer c.JSON(http.StatusOK, ret)
  94. arg, ok := util.JsonArg(c, ret)
  95. if !ok {
  96. return
  97. }
  98. key := arg["key"].(string)
  99. val := arg["val"].(interface{})
  100. err := model.SetLocalStorageVal(key, val)
  101. if nil != err {
  102. ret.Code = -1
  103. ret.Msg = err.Error()
  104. return
  105. }
  106. }
  107. func getLocalStorage(c *gin.Context) {
  108. ret := gulu.Ret.NewResult()
  109. defer c.JSON(http.StatusOK, ret)
  110. data, err := model.GetLocalStorage()
  111. if nil != err {
  112. ret.Code = -1
  113. ret.Msg = err.Error()
  114. return
  115. }
  116. ret.Data = data
  117. }
  118. func setLocalStorage(c *gin.Context) {
  119. ret := gulu.Ret.NewResult()
  120. defer c.JSON(http.StatusOK, ret)
  121. arg, ok := util.JsonArg(c, ret)
  122. if !ok {
  123. return
  124. }
  125. val := arg["val"].(interface{})
  126. err := model.SetLocalStorage(val)
  127. if nil != err {
  128. ret.Code = -1
  129. ret.Msg = err.Error()
  130. return
  131. }
  132. }