riff.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 renameRiffDeck(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. deckID := arg["deckID"].(string)
  106. name := arg["name"].(string)
  107. err := model.RenameDeck(deckID, name)
  108. if nil != err {
  109. ret.Code = -1
  110. ret.Msg = err.Error()
  111. return
  112. }
  113. }
  114. func createRiffDeck(c *gin.Context) {
  115. ret := gulu.Ret.NewResult()
  116. defer c.JSON(http.StatusOK, ret)
  117. arg, ok := util.JsonArg(c, ret)
  118. if !ok {
  119. return
  120. }
  121. name := arg["name"].(string)
  122. deck, err := model.CreateDeck(name)
  123. if nil != err {
  124. ret.Code = -1
  125. ret.Msg = err.Error()
  126. return
  127. }
  128. ret.Data = map[string]interface{}{
  129. "id": deck.ID,
  130. "name": deck.Name,
  131. }
  132. }
  133. func getRiffDecks(c *gin.Context) {
  134. ret := gulu.Ret.NewResult()
  135. defer c.JSON(http.StatusOK, ret)
  136. decks := model.GetDecks()
  137. var data []interface{}
  138. for _, deck := range decks {
  139. data = append(data, map[string]interface{}{
  140. "id": deck.ID,
  141. "name": deck.Name,
  142. })
  143. }
  144. if 1 > len(data) {
  145. data = []interface{}{}
  146. }
  147. ret.Data = data
  148. }