riff.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 renderRiffCard(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. blockID := arg["blockID"].(string)
  33. content, err := model.RenderFlashcard(blockID)
  34. if nil != err {
  35. ret.Code = -1
  36. ret.Msg = err.Error()
  37. return
  38. }
  39. ret.Data = map[string]interface{}{
  40. "content": content,
  41. }
  42. }
  43. func reviewRiffCard(c *gin.Context) {
  44. ret := gulu.Ret.NewResult()
  45. defer c.JSON(http.StatusOK, ret)
  46. arg, ok := util.JsonArg(c, ret)
  47. if !ok {
  48. return
  49. }
  50. deckID := arg["deckID"].(string)
  51. blockID := arg["blockID"].(string)
  52. rating := int(arg["rating"].(float64))
  53. err := model.ReviewFlashcard(deckID, blockID, riff.Rating(rating))
  54. if nil != err {
  55. ret.Code = -1
  56. ret.Msg = err.Error()
  57. return
  58. }
  59. }
  60. func getRiffDueCards(c *gin.Context) {
  61. ret := gulu.Ret.NewResult()
  62. defer c.JSON(http.StatusOK, ret)
  63. arg, ok := util.JsonArg(c, ret)
  64. if !ok {
  65. return
  66. }
  67. deckID := arg["deckID"].(string)
  68. cards, err := model.GetDueFlashcards(deckID)
  69. if nil != err {
  70. ret.Code = -1
  71. ret.Msg = err.Error()
  72. return
  73. }
  74. ret.Data = cards
  75. }
  76. func removeRiffCards(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. deckID := arg["deckID"].(string)
  84. blockIDsArg := arg["blockIDs"].([]interface{})
  85. var blockIDs []string
  86. for _, blockID := range blockIDsArg {
  87. blockIDs = append(blockIDs, blockID.(string))
  88. }
  89. err := model.RemoveFlashcards(deckID, blockIDs)
  90. if nil != err {
  91. ret.Code = -1
  92. ret.Msg = err.Error()
  93. return
  94. }
  95. }
  96. func addRiffCards(c *gin.Context) {
  97. ret := gulu.Ret.NewResult()
  98. defer c.JSON(http.StatusOK, ret)
  99. arg, ok := util.JsonArg(c, ret)
  100. if !ok {
  101. return
  102. }
  103. deckID := arg["deckID"].(string)
  104. blockIDsArg := arg["blockIDs"].([]interface{})
  105. var blockIDs []string
  106. for _, blockID := range blockIDsArg {
  107. blockIDs = append(blockIDs, blockID.(string))
  108. }
  109. err := model.AddFlashcards(deckID, blockIDs)
  110. if nil != err {
  111. ret.Code = -1
  112. ret.Msg = err.Error()
  113. return
  114. }
  115. }
  116. func renameRiffDeck(c *gin.Context) {
  117. ret := gulu.Ret.NewResult()
  118. defer c.JSON(http.StatusOK, ret)
  119. arg, ok := util.JsonArg(c, ret)
  120. if !ok {
  121. return
  122. }
  123. deckID := arg["deckID"].(string)
  124. name := arg["name"].(string)
  125. err := model.RenameDeck(deckID, name)
  126. if nil != err {
  127. ret.Code = -1
  128. ret.Msg = err.Error()
  129. return
  130. }
  131. }
  132. func createRiffDeck(c *gin.Context) {
  133. ret := gulu.Ret.NewResult()
  134. defer c.JSON(http.StatusOK, ret)
  135. arg, ok := util.JsonArg(c, ret)
  136. if !ok {
  137. return
  138. }
  139. name := arg["name"].(string)
  140. deck, err := model.CreateDeck(name)
  141. if nil != err {
  142. ret.Code = -1
  143. ret.Msg = err.Error()
  144. return
  145. }
  146. ret.Data = map[string]interface{}{
  147. "id": deck.ID,
  148. "name": deck.Name,
  149. }
  150. }
  151. func getRiffDecks(c *gin.Context) {
  152. ret := gulu.Ret.NewResult()
  153. defer c.JSON(http.StatusOK, ret)
  154. decks := model.GetDecks()
  155. var data []interface{}
  156. for _, deck := range decks {
  157. data = append(data, map[string]interface{}{
  158. "id": deck.ID,
  159. "name": deck.Name,
  160. })
  161. }
  162. if 1 > len(data) {
  163. data = []interface{}{}
  164. }
  165. ret.Data = data
  166. }