quiz.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package store
  2. import (
  3. "fmt"
  4. "strings"
  5. "git.andreafazzi.eu/andrea/probo/pkg/models"
  6. )
  7. type ErrQuizAlreadyPresent struct {
  8. hash string
  9. }
  10. func (e *ErrQuizAlreadyPresent) Error() string {
  11. return fmt.Sprintf("Quiz with hash %v is already present in the store.", e.hash)
  12. }
  13. type QuizStore struct {
  14. // Memory store for quizzes. It satisfies FilterStorer
  15. // interface.
  16. *FilterStore[*models.Quiz]
  17. questions *Store[*models.Question]
  18. answers *Store[*models.Answer]
  19. }
  20. func NewQuizStore() *QuizStore {
  21. store := new(QuizStore)
  22. store.questions = NewStore[*models.Question]()
  23. store.answers = NewStore[*models.Answer]()
  24. store.FilterStore = NewFilterStore[*models.Quiz]()
  25. return store
  26. }
  27. func (s *QuizStore) Create(quiz *models.Quiz) (*models.Quiz, error) {
  28. if hash := quiz.GetHash(); hash != "" {
  29. q, ok := s.hashes[hash]
  30. if ok {
  31. return q, &ErrQuizAlreadyPresent{hash}
  32. }
  33. }
  34. question, err := s.questions.Create(quiz.Question)
  35. if err != nil {
  36. return nil, err
  37. }
  38. answers := make([]*models.Answer, 0)
  39. for _, a := range quiz.Answers {
  40. storedAnswer, err := s.answers.Create(a)
  41. if err != nil {
  42. return nil, err
  43. }
  44. answers = append(answers, storedAnswer)
  45. }
  46. tags := make([]*models.Tag, 0)
  47. q, err := s.Store.Create(&models.Quiz{
  48. Meta: quiz.Meta,
  49. Question: parseTags[*models.Question](&tags, question)[0],
  50. Answers: parseTags[*models.Answer](&tags, answers...),
  51. Correct: answers[quiz.CorrectPos],
  52. CorrectPos: quiz.CorrectPos,
  53. Tags: tags,
  54. })
  55. if err != nil {
  56. return nil, err
  57. }
  58. return q, nil
  59. }
  60. func (s *QuizStore) Update(quiz *models.Quiz, id string) (*models.Quiz, error) {
  61. _, err := s.Read(id)
  62. if err != nil {
  63. return quiz, err
  64. }
  65. question, err := s.questions.Create(quiz.Question)
  66. if err != nil {
  67. return nil, err
  68. }
  69. answers := make([]*models.Answer, 0)
  70. for _, a := range quiz.Answers {
  71. storedAnswer, err := s.answers.Create(a)
  72. if err != nil {
  73. return nil, err
  74. }
  75. answers = append(answers, storedAnswer)
  76. }
  77. tags := make([]*models.Tag, 0)
  78. q, err := s.Store.Update(&models.Quiz{
  79. Question: parseTags[*models.Question](&tags, question)[0],
  80. Answers: parseTags[*models.Answer](&tags, answers...),
  81. Correct: answers[quiz.CorrectPos],
  82. CorrectPos: quiz.CorrectPos,
  83. Tags: tags,
  84. }, id)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return q, nil
  89. }
  90. func (s *QuizStore) FilterInCollection(collection *models.Collection, filter map[string]string) []*models.Quiz {
  91. quizzes := s.ReadAll()
  92. if filter == nil {
  93. return quizzes
  94. }
  95. tagsValue := filter["tags"]
  96. if tagsValue == "" || len(tagsValue) == 0 {
  97. return quizzes
  98. }
  99. fTags := strings.Split(tagsValue, ",")
  100. filteredQuizzes := s.Filter(quizzes, func(q *models.Quiz) bool {
  101. count := 0
  102. for _, qTag := range q.Tags {
  103. if s.isTagInFilter(qTag, fTags) {
  104. count++
  105. }
  106. }
  107. if count == len(fTags) {
  108. return true
  109. }
  110. return false
  111. })
  112. collection.Quizzes = filteredQuizzes
  113. return collection.Quizzes
  114. }
  115. func (s *QuizStore) isTagInFilter(tag *models.Tag, fTags []string) bool {
  116. for _, t := range fTags {
  117. if tag.Name == strings.TrimSpace(t) {
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. func parseTags[T fmt.Stringer](tags *[]*models.Tag, entities ...T) []T {
  124. for _, entity := range entities {
  125. // Trim the following chars
  126. trimChars := "*:.,/\\@()[]{}<>"
  127. // Split the text into words
  128. words := strings.Fields(entity.String())
  129. for _, word := range words {
  130. // If the word starts with '#', it is considered as a tag
  131. if strings.HasPrefix(word, "#") {
  132. // Check if the tag already exists in the tags slice
  133. exists := false
  134. for _, tag := range *tags {
  135. if tag.Name == word {
  136. exists = true
  137. break
  138. }
  139. }
  140. // If the tag does not exist in the tags slice, add it
  141. if !exists {
  142. *tags = append(*tags, &models.Tag{Name: strings.TrimRight(word, trimChars)})
  143. }
  144. }
  145. }
  146. }
  147. return entities
  148. }