Implement DeleteQuiz

This commit is contained in:
andrea 2023-09-01 11:48:09 +02:00
parent dd4636a89d
commit 9780956432
6 changed files with 108 additions and 0 deletions

View file

@ -47,3 +47,7 @@ type CreateAnswerRequest struct {
type CreateUpdateQuizRequest struct { type CreateUpdateQuizRequest struct {
*Quiz *Quiz
} }
type DeleteQuizRequest struct {
ID string
}

View file

@ -133,6 +133,30 @@ func (s *FileProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest,
return s.memoryStore.ReadQuizByHash(quiz.Hash) return s.memoryStore.ReadQuizByHash(quiz.Hash)
} }
func (s *FileProboCollectorStore) DeleteQuiz(r *client.DeleteQuizRequest) (*models.Quiz, error) {
quiz, err := s.memoryStore.DeleteQuiz(r.ID)
if err != nil {
return nil, err
}
path, err := s.GetPath(quiz)
if err != nil {
return nil, err
}
err = os.Remove(path)
if err != nil {
return nil, err
}
err = s.Reindex()
if err != nil {
return nil, err
}
return quiz, nil
}
func (s *FileProboCollectorStore) GetPath(quiz *models.Quiz) (string, error) { func (s *FileProboCollectorStore) GetPath(quiz *models.Quiz) (string, error) {
s.lock.RLock() s.lock.RLock()
defer s.lock.RUnlock() defer s.lock.RUnlock()

View file

@ -150,6 +150,39 @@ func (t *testSuite) TestCreateQuiz() {
} }
} }
func (t *testSuite) TestDeleteQuiz() {
dirname := "./testdata/quizzes"
store, err := NewFileProboCollectorStore(dirname)
t.True(err == nil, fmt.Sprintf("A file store should be initialized without problems but an error occurred: %v", err))
if !t.Failed() {
quiz, err := createQuizOnDisk(store, &client.CreateUpdateQuizRequest{
Quiz: &client.Quiz{
Question: &client.Question{Text: "This quiz should be deleted."},
Answers: []*client.Answer{
{Text: "Answer 1", Correct: true},
{Text: "Answer 2", Correct: false},
{Text: "Answer 3", Correct: false},
{Text: "Answer 4", Correct: false},
},
},
})
t.Nil(err, "The quiz to be deleted should be created without issue")
path, err := store.GetPath(quiz)
t.True(path != "", "Quiz path should be obtained without errors")
if !t.Failed() {
deletedQuiz, err := store.DeleteQuiz(&client.DeleteQuizRequest{ID: quiz.ID})
t.Nil(err, fmt.Sprintf("Quiz should be deleted without errors: %v", err))
t.True(reflect.DeepEqual(quiz, deletedQuiz), "Quiz should be updateEd.")
}
}
}
func (t *testSuite) TestUpdateQuiz() { func (t *testSuite) TestUpdateQuiz() {
dirname := "./testdata/quizzes" dirname := "./testdata/quizzes"
store, err := NewFileProboCollectorStore(dirname) store, err := NewFileProboCollectorStore(dirname)

View file

@ -116,6 +116,22 @@ func (s *MemoryProboCollectorStore) createAnswerFromHash(hash string, answer *mo
return answer return answer
} }
func (s *MemoryProboCollectorStore) deleteQuiz(id string) (*models.Quiz, error) {
s.lock.Lock()
defer s.lock.Unlock()
quiz := s.quizzes[id]
if quiz == nil {
return nil, fmt.Errorf("Trying to delete a quiz that doesn't exist in memory (ID: %v)", id)
}
s.quizzes[id] = nil
s.quizzesHashes[quiz.Hash] = nil
return quiz, nil
}
func (s *MemoryProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) { func (s *MemoryProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
result := make([]*models.Quiz, 0) result := make([]*models.Quiz, 0)
for id := range s.quizzes { for id := range s.quizzes {
@ -198,3 +214,7 @@ func (s *MemoryProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest
func (s *MemoryProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, bool, error) { func (s *MemoryProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, bool, error) {
return s.createOrUpdateQuiz(r, id) return s.createOrUpdateQuiz(r, id)
} }
func (s *MemoryProboCollectorStore) DeleteQuiz(id string) (*models.Quiz, error) {
return s.deleteQuiz(id)
}

View file

@ -87,3 +87,29 @@ func (t *testSuite) TestUpdateQuiz() {
t.Equal("Updated Answer 2", updatedQuiz.Answers[1].Text) t.Equal("Updated Answer 2", updatedQuiz.Answers[1].Text)
} }
} }
func (t *testSuite) TestDeleteQuiz() {
store := NewMemoryProboCollectorStore(
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
)
quiz, _ := store.CreateQuiz(
&client.CreateUpdateQuizRequest{
Quiz: &client.Quiz{
Question: &client.Question{Text: "This test should be removed."},
Answers: []*client.Answer{
{Text: "Answer 1", Correct: true},
{Text: "Answer 2", Correct: false},
{Text: "Answer 3", Correct: false},
{Text: "Answer 4", Correct: false},
},
},
})
deletedQuiz, err := store.DeleteQuiz(quiz.ID)
t.Equal(quiz.ID, deletedQuiz.ID, "Returned deleted quiz ID should be equal to the request")
t.Nil(err, fmt.Sprintf("The update returned an error: %v", err))
_, err = store.ReadQuizByHash(quiz.Hash)
t.True(err != nil, "Reading a non existent quiz should return an error")
}

View file

@ -12,4 +12,5 @@ type ProboCollectorStore interface {
CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error)
UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error)
DeleteQuiz(r *client.DeleteQuizRequest) (*models.Quiz, error)
} }