riff.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "time"
  20. "github.com/88250/gulu"
  21. "github.com/gin-gonic/gin"
  22. "github.com/siyuan-note/riff"
  23. "github.com/siyuan-note/siyuan/kernel/model"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. func getRiffCards(c *gin.Context) {
  27. ret := gulu.Ret.NewResult()
  28. defer c.JSON(http.StatusOK, ret)
  29. arg, ok := util.JsonArg(c, ret)
  30. if !ok {
  31. return
  32. }
  33. deckID := arg["deckID"].(string)
  34. page := int(arg["page"].(float64))
  35. blockIDs, total, pageCount := model.GetFlashcards(deckID, page)
  36. ret.Data = map[string]interface{}{
  37. "blocks": blockIDs,
  38. "total": total,
  39. "pageCount": pageCount,
  40. }
  41. }
  42. func reviewRiffCard(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. blockID := arg["blockID"].(string)
  51. rating := int(arg["rating"].(float64))
  52. err := model.ReviewFlashcard(deckID, blockID, riff.Rating(rating))
  53. if nil != err {
  54. ret.Code = -1
  55. ret.Msg = err.Error()
  56. return
  57. }
  58. }
  59. func getTreeRiffDueCards(c *gin.Context) {
  60. ret := gulu.Ret.NewResult()
  61. defer c.JSON(http.StatusOK, ret)
  62. arg, ok := util.JsonArg(c, ret)
  63. if !ok {
  64. return
  65. }
  66. rootID := arg["rootID"].(string)
  67. cards, err := model.GetTreeDueFlashcards(rootID)
  68. if nil != err {
  69. ret.Code = -1
  70. ret.Msg = err.Error()
  71. return
  72. }
  73. ret.Data = cards
  74. }
  75. func getRiffDueCards(c *gin.Context) {
  76. ret := gulu.Ret.NewResult()
  77. defer c.JSON(http.StatusOK, ret)
  78. arg, ok := util.JsonArg(c, ret)
  79. if !ok {
  80. return
  81. }
  82. deckID := arg["deckID"].(string)
  83. cards, err := model.GetDueFlashcards(deckID)
  84. if nil != err {
  85. ret.Code = -1
  86. ret.Msg = err.Error()
  87. return
  88. }
  89. ret.Data = cards
  90. }
  91. func removeRiffCards(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. deckID := arg["deckID"].(string)
  99. blockIDsArg := arg["blockIDs"].([]interface{})
  100. var blockIDs []string
  101. for _, blockID := range blockIDsArg {
  102. blockIDs = append(blockIDs, blockID.(string))
  103. }
  104. err := model.RemoveFlashcards(deckID, blockIDs)
  105. if nil != err {
  106. ret.Code = -1
  107. ret.Msg = err.Error()
  108. return
  109. }
  110. deck := model.Decks[deckID]
  111. ret.Data = deckData(deck)
  112. }
  113. func addRiffCards(c *gin.Context) {
  114. ret := gulu.Ret.NewResult()
  115. defer c.JSON(http.StatusOK, ret)
  116. arg, ok := util.JsonArg(c, ret)
  117. if !ok {
  118. return
  119. }
  120. deckID := arg["deckID"].(string)
  121. blockIDsArg := arg["blockIDs"].([]interface{})
  122. var blockIDs []string
  123. for _, blockID := range blockIDsArg {
  124. blockIDs = append(blockIDs, blockID.(string))
  125. }
  126. err := model.AddFlashcards(deckID, blockIDs)
  127. if nil != err {
  128. ret.Code = -1
  129. ret.Msg = err.Error()
  130. return
  131. }
  132. deck := model.Decks[deckID]
  133. ret.Data = deckData(deck)
  134. }
  135. func renameRiffDeck(c *gin.Context) {
  136. ret := gulu.Ret.NewResult()
  137. defer c.JSON(http.StatusOK, ret)
  138. arg, ok := util.JsonArg(c, ret)
  139. if !ok {
  140. return
  141. }
  142. deckID := arg["deckID"].(string)
  143. name := arg["name"].(string)
  144. err := model.RenameDeck(deckID, name)
  145. if nil != err {
  146. ret.Code = -1
  147. ret.Msg = err.Error()
  148. return
  149. }
  150. }
  151. func removeRiffDeck(c *gin.Context) {
  152. ret := gulu.Ret.NewResult()
  153. defer c.JSON(http.StatusOK, ret)
  154. arg, ok := util.JsonArg(c, ret)
  155. if !ok {
  156. return
  157. }
  158. deckID := arg["deckID"].(string)
  159. err := model.RemoveDeck(deckID)
  160. if nil != err {
  161. ret.Code = -1
  162. ret.Msg = err.Error()
  163. return
  164. }
  165. }
  166. func createRiffDeck(c *gin.Context) {
  167. ret := gulu.Ret.NewResult()
  168. defer c.JSON(http.StatusOK, ret)
  169. arg, ok := util.JsonArg(c, ret)
  170. if !ok {
  171. return
  172. }
  173. name := arg["name"].(string)
  174. deck, err := model.CreateDeck(name)
  175. if nil != err {
  176. ret.Code = -1
  177. ret.Msg = err.Error()
  178. return
  179. }
  180. ret.Data = deckData(deck)
  181. }
  182. func getRiffDecks(c *gin.Context) {
  183. ret := gulu.Ret.NewResult()
  184. defer c.JSON(http.StatusOK, ret)
  185. decks := model.GetDecks()
  186. var data []interface{}
  187. for _, deck := range decks {
  188. data = append(data, deckData(deck))
  189. }
  190. if 1 > len(data) {
  191. data = []interface{}{}
  192. }
  193. ret.Data = data
  194. }
  195. func deckData(deck *riff.Deck) map[string]interface{} {
  196. return map[string]interface{}{
  197. "id": deck.ID,
  198. "name": deck.Name,
  199. "size": len(deck.BlockCard),
  200. "created": time.UnixMilli(deck.Created).Format("2006-01-02 15:04:05"),
  201. "updated": time.UnixMilli(deck.Updated).Format("2006-01-02 15:04:05"),
  202. }
  203. }