diff --git a/go.mod b/go.mod index f3c54e4..9bf7911 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require github.com/sirupsen/logrus v1.8.1 require ( + github.com/google/uuid v1.3.0 // indirect github.com/kr/pretty v0.2.1 // indirect github.com/kr/text v0.1.0 // indirect github.com/remogatto/prettytest v0.0.0-20200211072524-6d385e11dcb8 // indirect diff --git a/go.sum b/go.sum index b0788f1..fe2dcf6 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= diff --git a/misc/logseq/package.json b/misc/logseq/package.json index 8558724..c7c67ed 100644 --- a/misc/logseq/package.json +++ b/misc/logseq/package.json @@ -1,10 +1,10 @@ { "logseq": { - "id": "logseq-helloworld-plugin", - "title": "logseq-helloworld-plugin", + "id": "logseq-probo-plugin", + "title": "logseq-probo-plugin", "icon": "./icon.svg" }, - "name": "logseq-helloworld-plugin", + "name": "logseq-probo-plugin", "version": "1.2.0", "description": "", "main": "dist/index.html", diff --git a/misc/logseq/src/index.ts b/misc/logseq/src/index.ts index b3c57df..ed13866 100644 --- a/misc/logseq/src/index.ts +++ b/misc/logseq/src/index.ts @@ -32,20 +32,20 @@ const main = () => { }) }); - logseq.Editor.registerSlashCommand("Insert a batch of quizzes", async () => { + logseq.Editor.registerSlashCommand("Create a new Probo quiz", async () => { await logseq.Editor.insertAtEditingCursor( `{{renderer :quizhub_${uniqueIdentifier()}}}` ); const currBlock = await logseq.Editor.getCurrentBlock(); - await logseq.Editor.insertBlock(currBlock.uuid, 'Text of the question here...', + await logseq.Editor.insertBlock(currBlock.uuid, 'Write your question here...', { - sibling: false, + sibling: false, before: false, } ); - await logseq.Editor.exitEditingMode(); + // await logseq.Editor.exitEditingMode(); }); @@ -62,12 +62,13 @@ const main = () => { const answers = child.children.map((answer: BlockEntity, i: number) => { return { text: answer.content, correct: (i == 0) ? true : false } }) - return { question: question, answers: answers } + return { question: question, answers: answers, uuid: child.uuid } }); - quizzes.forEach(async quiz => { + quizzes.forEach(async (quiz, i) => { const res = await fetch(endpoint, { method: 'POST', body: JSON.stringify(quiz) }) const data = await res.json(); - console.log(data) + const block = await logseq.Editor.getBlock(quiz.uuid) + await logseq.Editor.upsertBlockProperty(parentBlock.uuid, `probo-quiz-uuid`, data.content.ID) }) } @@ -75,17 +76,17 @@ const main = () => { logseq.provideStyle(` .renderBtn { - border: 1px solid black; + border: 1px solid white; border-radius: 8px; - padding: 3px; + padding: 5px; font-size: 80%; - background-color: white; - color: black; - } - .renderBtn:hover { background-color: black; color: white; } + .renderBtn:hover { + background-color: white; + color: black; + } `); logseq.provideUI({ diff --git a/models/test.go b/models/test.go index f811006..a64a089 100644 --- a/models/test.go +++ b/models/test.go @@ -1,8 +1,7 @@ package models type Quiz struct { - ID string - + ID string Question *Question Answers []*Answer Correct *Answer diff --git a/server_integration_test.go b/server_integration_test.go index df1d00e..e93f551 100644 --- a/server_integration_test.go +++ b/server_integration_test.go @@ -77,10 +77,13 @@ func (t *integrationTestSuite) TestCatchDuplicateQuiz() { } ` quiz1, err := t.createQuiz(server, payload) + t.True(err == nil) + quiz2, err := t.createQuiz(server, payload) - t.True(err == nil, "Quiz are duplicated, but the API should not return an error") - t.True(reflect.DeepEqual(quiz1, quiz2)) + + t.True(err == nil, "Quizzes are duplicated, but the API should not return an error") + t.True(reflect.DeepEqual(quiz1, quiz2), "Quizzes shold be exactly the same") } func (t *integrationTestSuite) createQuiz(server *QuizHubCollectorServer, payload string) (*client.CreateQuizResponse, error) { diff --git a/store/memory/memory.go b/store/memory/memory.go index 166ff5d..b08a2f7 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -6,6 +6,7 @@ import ( "git.andreafazzi.eu/andrea/probo/client" "git.andreafazzi.eu/andrea/probo/hasher" "git.andreafazzi.eu/andrea/probo/models" + "github.com/google/uuid" ) type MemoryQuizHubCollectorStore struct { @@ -69,12 +70,12 @@ func (s *MemoryQuizHubCollectorStore) readAnswer(id string) *models.Answer { return nil } -func (s *MemoryQuizHubCollectorStore) createQuiz(id string, quiz *models.Quiz) *models.Quiz { +func (s *MemoryQuizHubCollectorStore) createQuiz(id string, hash string, quiz *models.Quiz) *models.Quiz { s.lock.Lock() defer s.lock.Unlock() quiz.ID = id - s.quizzes[id] = quiz + s.quizzes[hash] = quiz return quiz } @@ -108,20 +109,21 @@ func (s *MemoryQuizHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) { func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) (*models.Quiz, error) { hashes := s.hasher.QuizHashes(r) - quizID := hashes[len(hashes)-1] + quizID := uuid.New().String() + quizHash := hashes[len(hashes)-1] - quiz := s.readQuiz(quizID) + quiz := s.readQuiz(quizHash) if quiz != nil { // Quiz is already present in the store return quiz, nil } quiz = new(models.Quiz) - questionID := hashes[0] - q := s.readQuestion(questionID) - if q == nil { // if the question is not in the store add it - q = s.createQuestion(questionID, &models.Question{ - ID: questionID, + questionHash := hashes[0] + q := s.readQuestion(questionHash) + if q == nil { // if the question is not in the store then we should add it + q = s.createQuestion(questionHash, &models.Question{ + ID: uuid.New().String(), Text: r.Question.Text, }) } @@ -129,11 +131,11 @@ func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) (* // Populate Question field quiz.Question = q for i, answer := range r.Answers { - answerID := hashes[i+1] - a := s.readAnswer(answerID) + answerHash := hashes[i+1] + a := s.readAnswer(answerHash) if a == nil { // if the answer is not in the store add it - a = s.createAnswer(answerID, &models.Answer{ - ID: answerID, + a = s.createAnswer(answerHash, &models.Answer{ + ID: uuid.New().String(), Text: answer.Text, }) if answer.Correct { @@ -143,5 +145,5 @@ func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) (* quiz.Answers = append(quiz.Answers, a) } - return s.createQuiz(quizID, quiz), nil + return s.createQuiz(quizID, quizHash, quiz), nil }