probo/store/memory/memory.go
2023-06-28 17:21:59 +02:00

181 lines
4.1 KiB
Go

package memory
import (
"fmt"
"sync"
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/hasher"
"git.andreafazzi.eu/andrea/probo/models"
"github.com/google/uuid"
)
type MemoryProboCollectorStore struct {
quizzes map[string]*models.Quiz
questionsHashes map[string]*models.Question
answersHashes map[string]*models.Answer
quizzesHashes map[string]*models.Quiz
hasher hasher.Hasher
// A mutex is used to synchronize read/write access to the map
lock sync.RWMutex
}
func NewMemoryProboCollectorStore(hasher hasher.Hasher) *MemoryProboCollectorStore {
s := new(MemoryProboCollectorStore)
s.hasher = hasher
s.questionsHashes = make(map[string]*models.Question)
s.answersHashes = make(map[string]*models.Answer)
s.quizzesHashes = make(map[string]*models.Quiz)
s.quizzes = make(map[string]*models.Quiz)
return s
}
func (s *MemoryProboCollectorStore) getQuizFromHash(hash string) *models.Quiz {
s.lock.RLock()
defer s.lock.RUnlock()
quiz, ok := s.quizzesHashes[hash]
if ok {
return quiz
}
return nil
}
func (s *MemoryProboCollectorStore) getQuizFromID(id string) *models.Quiz {
s.lock.RLock()
defer s.lock.RUnlock()
quiz, ok := s.quizzes[id]
if ok {
return quiz
}
return nil
}
func (s *MemoryProboCollectorStore) getQuestionFromHash(hash string) *models.Question {
s.lock.RLock()
defer s.lock.RUnlock()
question, ok := s.questionsHashes[hash]
if ok {
return question
}
return nil
}
func (s *MemoryProboCollectorStore) getAnswerFromHash(hash string) *models.Answer {
s.lock.RLock()
defer s.lock.RUnlock()
answer, ok := s.answersHashes[hash]
if ok {
return answer
}
return nil
}
func (s *MemoryProboCollectorStore) createQuizFromHash(id string, hash string, quiz *models.Quiz) *models.Quiz {
s.lock.Lock()
defer s.lock.Unlock()
quiz.ID = id
quiz.Hash = hash
s.quizzesHashes[hash] = quiz
s.quizzes[id] = quiz
return quiz
}
func (s *MemoryProboCollectorStore) createQuestionFromHash(hash string, question *models.Question) *models.Question {
s.lock.Lock()
defer s.lock.Unlock()
s.questionsHashes[hash] = question
return question
}
func (s *MemoryProboCollectorStore) createAnswerFromHash(hash string, answer *models.Answer) *models.Answer {
s.lock.Lock()
defer s.lock.Unlock()
s.answersHashes[hash] = answer
return answer
}
func (s *MemoryProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
result := make([]*models.Quiz, 0)
for id := range s.quizzes {
result = append(result, s.getQuizFromID(id))
}
return result, nil
}
func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) {
hashes := s.hasher.QuizHashes(r.Quiz)
quizHash := hashes[len(hashes)-1]
quiz := s.getQuizFromHash(quizHash)
if quiz != nil { // Quiz is already present in the store
return quiz, nil
}
if id != "" {
quiz = s.getQuizFromID(id)
if quiz == nil { // Quiz is already present in the store
return nil, fmt.Errorf("Quiz ID %v doesn't exist in the store!", id)
}
} else {
id = uuid.New().String()
quiz = new(models.Quiz)
}
questionHash := hashes[0]
q := s.getQuestionFromHash(questionHash)
if q == nil { // if the question is not in the store then we should add it
q = s.createQuestionFromHash(questionHash, &models.Question{
ID: uuid.New().String(),
Text: r.Quiz.Question.Text,
})
}
// Populate Question field
quiz.Question = q
for i, answer := range r.Quiz.Answers {
answerHash := hashes[i+1]
a := s.getAnswerFromHash(answerHash)
if a == nil { // if the answer is not in the store add it
a = s.createAnswerFromHash(answerHash, &models.Answer{
ID: uuid.New().String(),
Text: answer.Text,
})
}
if answer.Correct {
quiz.Correct = a
}
quiz.Answers = append(quiz.Answers, a)
}
return s.createQuizFromHash(id, quizHash, quiz), nil
}
func (s *MemoryProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) {
return s.createOrUpdateQuiz(r, "")
}
func (s *MemoryProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) {
return s.createOrUpdateQuiz(r, id)
}