Use UUID for models

This commit is contained in:
Andrea Fazzi 2022-06-27 15:11:37 +02:00
parent 4f45fec610
commit 00f4e08627
7 changed files with 42 additions and 34 deletions

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -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",

View file

@ -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({

View file

@ -1,8 +1,7 @@
package models
type Quiz struct {
ID string
ID string
Question *Question
Answers []*Answer
Correct *Answer

View file

@ -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) {

View file

@ -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
}