From 9780956432bb8088d7b0fd6ad513b64bd6508dee Mon Sep 17 00:00:00 2001 From: andrea Date: Fri, 1 Sep 2023 11:48:09 +0200 Subject: [PATCH] Implement DeleteQuiz --- client/client.go | 4 ++++ store/file/file.go | 24 ++++++++++++++++++++++++ store/file/file_test.go | 33 +++++++++++++++++++++++++++++++++ store/memory/memory.go | 20 ++++++++++++++++++++ store/memory/memory_test.go | 26 ++++++++++++++++++++++++++ store/store.go | 1 + 6 files changed, 108 insertions(+) diff --git a/client/client.go b/client/client.go index 5d38d73..f7b5dfd 100644 --- a/client/client.go +++ b/client/client.go @@ -47,3 +47,7 @@ type CreateAnswerRequest struct { type CreateUpdateQuizRequest struct { *Quiz } + +type DeleteQuizRequest struct { + ID string +} diff --git a/store/file/file.go b/store/file/file.go index ea3bb5c..4cfcea9 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -133,6 +133,30 @@ func (s *FileProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, 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) { s.lock.RLock() defer s.lock.RUnlock() diff --git a/store/file/file_test.go b/store/file/file_test.go index 36ddd06..e68ba7a 100644 --- a/store/file/file_test.go +++ b/store/file/file_test.go @@ -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() { dirname := "./testdata/quizzes" store, err := NewFileProboCollectorStore(dirname) diff --git a/store/memory/memory.go b/store/memory/memory.go index 9b52127..81f79dc 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -116,6 +116,22 @@ func (s *MemoryProboCollectorStore) createAnswerFromHash(hash string, answer *mo 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) { result := make([]*models.Quiz, 0) 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) { return s.createOrUpdateQuiz(r, id) } + +func (s *MemoryProboCollectorStore) DeleteQuiz(id string) (*models.Quiz, error) { + return s.deleteQuiz(id) +} diff --git a/store/memory/memory_test.go b/store/memory/memory_test.go index d386fa5..197c4e6 100644 --- a/store/memory/memory_test.go +++ b/store/memory/memory_test.go @@ -87,3 +87,29 @@ func (t *testSuite) TestUpdateQuiz() { 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") +} diff --git a/store/store.go b/store/store.go index 38a31be..a8b7985 100644 --- a/store/store.go +++ b/store/store.go @@ -12,4 +12,5 @@ type ProboCollectorStore interface { CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) + DeleteQuiz(r *client.DeleteQuizRequest) (*models.Quiz, error) }