riff.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/riff"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func reviewRiffCard(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. arg, ok := util.JsonArg(c, ret)
  29. if !ok {
  30. return
  31. }
  32. deckID := arg["deckID"].(string)
  33. blockID := arg["blockID"].(string)
  34. rating := int(arg["rating"].(float64))
  35. err := model.ReviewFlashcard(deckID, blockID, riff.Rating(rating))
  36. if nil != err {
  37. ret.Code = -1
  38. ret.Msg = err.Error()
  39. return
  40. }
  41. }
  42. func getRiffDueCards(c *gin.Context) {
  43. ret := gulu.Ret.NewResult()
  44. defer c.JSON(http.StatusOK, ret)
  45. arg, ok := util.JsonArg(c, ret)
  46. if !ok {
  47. return
  48. }
  49. deckID := arg["deckID"].(string)
  50. cards, err := model.GetDueFlashcards(deckID)
  51. if nil != err {
  52. ret.Code = -1
  53. ret.Msg = err.Error()
  54. return
  55. }
  56. ret.Data = cards
  57. }
  58. func removeRiffCards(c *gin.Context) {
  59. ret := gulu.Ret.NewResult()
  60. defer c.JSON(http.StatusOK, ret)
  61. arg, ok := util.JsonArg(c, ret)
  62. if !ok {
  63. return
  64. }
  65. deckID := arg["deckID"].(string)
  66. blockIDsArg := arg["blockIDs"].([]interface{})
  67. var blockIDs []string
  68. for _, blockID := range blockIDsArg {
  69. blockIDs = append(blockIDs, blockID.(string))
  70. }
  71. err := model.RemoveFlashcards(deckID, blockIDs)
  72. if nil != err {
  73. ret.Code = -1
  74. ret.Msg = err.Error()
  75. return
  76. }
  77. }
  78. func addRiffCards(c *gin.Context) {
  79. ret := gulu.Ret.NewResult()
  80. defer c.JSON(http.StatusOK, ret)
  81. arg, ok := util.JsonArg(c, ret)
  82. if !ok {
  83. return
  84. }
  85. deckID := arg["deckID"].(string)
  86. blockIDsArg := arg["blockIDs"].([]interface{})
  87. var blockIDs []string
  88. for _, blockID := range blockIDsArg {
  89. blockIDs = append(blockIDs, blockID.(string))
  90. }
  91. err := model.AddFlashcards(deckID, blockIDs)
  92. if nil != err {
  93. ret.Code = -1
  94. ret.Msg = err.Error()
  95. return
  96. }
  97. }
  98. func createRiffDeck(c *gin.Context) {
  99. ret := gulu.Ret.NewResult()
  100. defer c.JSON(http.StatusOK, ret)
  101. arg, ok := util.JsonArg(c, ret)
  102. if !ok {
  103. return
  104. }
  105. name := arg["name"].(string)
  106. deck, err := model.CreateDeck(name)
  107. if nil != err {
  108. ret.Code = -1
  109. ret.Msg = err.Error()
  110. return
  111. }
  112. ret.Data = deck
  113. }
  114. func getRiffDecks(c *gin.Context) {
  115. ret := gulu.Ret.NewResult()
  116. defer c.JSON(http.StatusOK, ret)
  117. decks := model.GetDecks()
  118. ret.Data = decks
  119. }