Use UUID for models
This commit is contained in:
parent
4f45fec610
commit
00f4e08627
7 changed files with 42 additions and 34 deletions
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.17
|
||||||
require github.com/sirupsen/logrus v1.8.1
|
require github.com/sirupsen/logrus v1.8.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
github.com/kr/pretty v0.2.1 // indirect
|
github.com/kr/pretty v0.2.1 // indirect
|
||||||
github.com/kr/text v0.1.0 // indirect
|
github.com/kr/text v0.1.0 // indirect
|
||||||
github.com/remogatto/prettytest v0.0.0-20200211072524-6d385e11dcb8 // indirect
|
github.com/remogatto/prettytest v0.0.0-20200211072524-6d385e11dcb8 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,4 +1,6 @@
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
{
|
{
|
||||||
"logseq": {
|
"logseq": {
|
||||||
"id": "logseq-helloworld-plugin",
|
"id": "logseq-probo-plugin",
|
||||||
"title": "logseq-helloworld-plugin",
|
"title": "logseq-probo-plugin",
|
||||||
"icon": "./icon.svg"
|
"icon": "./icon.svg"
|
||||||
},
|
},
|
||||||
"name": "logseq-helloworld-plugin",
|
"name": "logseq-probo-plugin",
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.html",
|
"main": "dist/index.html",
|
||||||
|
|
|
@ -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(
|
await logseq.Editor.insertAtEditingCursor(
|
||||||
`{{renderer :quizhub_${uniqueIdentifier()}}}`
|
`{{renderer :quizhub_${uniqueIdentifier()}}}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const currBlock = await logseq.Editor.getCurrentBlock();
|
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,
|
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) => {
|
const answers = child.children.map((answer: BlockEntity, i: number) => {
|
||||||
return { text: answer.content, correct: (i == 0) ? true : false }
|
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 res = await fetch(endpoint, { method: 'POST', body: JSON.stringify(quiz) })
|
||||||
const data = await res.json();
|
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(`
|
logseq.provideStyle(`
|
||||||
.renderBtn {
|
.renderBtn {
|
||||||
border: 1px solid black;
|
border: 1px solid white;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
padding: 3px;
|
padding: 5px;
|
||||||
font-size: 80%;
|
font-size: 80%;
|
||||||
background-color: white;
|
|
||||||
color: black;
|
|
||||||
}
|
|
||||||
.renderBtn:hover {
|
|
||||||
background-color: black;
|
background-color: black;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
.renderBtn:hover {
|
||||||
|
background-color: white;
|
||||||
|
color: black;
|
||||||
|
}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
logseq.provideUI({
|
logseq.provideUI({
|
||||||
|
|
|
@ -2,7 +2,6 @@ package models
|
||||||
|
|
||||||
type Quiz struct {
|
type Quiz struct {
|
||||||
ID string
|
ID string
|
||||||
|
|
||||||
Question *Question
|
Question *Question
|
||||||
Answers []*Answer
|
Answers []*Answer
|
||||||
Correct *Answer
|
Correct *Answer
|
||||||
|
|
|
@ -77,10 +77,13 @@ func (t *integrationTestSuite) TestCatchDuplicateQuiz() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
quiz1, err := t.createQuiz(server, payload)
|
quiz1, err := t.createQuiz(server, payload)
|
||||||
|
|
||||||
t.True(err == nil)
|
t.True(err == nil)
|
||||||
|
|
||||||
quiz2, err := t.createQuiz(server, payload)
|
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) {
|
func (t *integrationTestSuite) createQuiz(server *QuizHubCollectorServer, payload string) (*client.CreateQuizResponse, error) {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"git.andreafazzi.eu/andrea/probo/client"
|
"git.andreafazzi.eu/andrea/probo/client"
|
||||||
"git.andreafazzi.eu/andrea/probo/hasher"
|
"git.andreafazzi.eu/andrea/probo/hasher"
|
||||||
"git.andreafazzi.eu/andrea/probo/models"
|
"git.andreafazzi.eu/andrea/probo/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MemoryQuizHubCollectorStore struct {
|
type MemoryQuizHubCollectorStore struct {
|
||||||
|
@ -69,12 +70,12 @@ func (s *MemoryQuizHubCollectorStore) readAnswer(id string) *models.Answer {
|
||||||
return nil
|
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()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
quiz.ID = id
|
quiz.ID = id
|
||||||
s.quizzes[id] = quiz
|
s.quizzes[hash] = quiz
|
||||||
|
|
||||||
return quiz
|
return quiz
|
||||||
}
|
}
|
||||||
|
@ -108,20 +109,21 @@ func (s *MemoryQuizHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
|
||||||
func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) (*models.Quiz, error) {
|
func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) (*models.Quiz, error) {
|
||||||
hashes := s.hasher.QuizHashes(r)
|
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
|
if quiz != nil { // Quiz is already present in the store
|
||||||
return quiz, nil
|
return quiz, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
quiz = new(models.Quiz)
|
quiz = new(models.Quiz)
|
||||||
|
|
||||||
questionID := hashes[0]
|
questionHash := hashes[0]
|
||||||
q := s.readQuestion(questionID)
|
q := s.readQuestion(questionHash)
|
||||||
if q == nil { // if the question is not in the store add it
|
if q == nil { // if the question is not in the store then we should add it
|
||||||
q = s.createQuestion(questionID, &models.Question{
|
q = s.createQuestion(questionHash, &models.Question{
|
||||||
ID: questionID,
|
ID: uuid.New().String(),
|
||||||
Text: r.Question.Text,
|
Text: r.Question.Text,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -129,11 +131,11 @@ func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) (*
|
||||||
// Populate Question field
|
// Populate Question field
|
||||||
quiz.Question = q
|
quiz.Question = q
|
||||||
for i, answer := range r.Answers {
|
for i, answer := range r.Answers {
|
||||||
answerID := hashes[i+1]
|
answerHash := hashes[i+1]
|
||||||
a := s.readAnswer(answerID)
|
a := s.readAnswer(answerHash)
|
||||||
if a == nil { // if the answer is not in the store add it
|
if a == nil { // if the answer is not in the store add it
|
||||||
a = s.createAnswer(answerID, &models.Answer{
|
a = s.createAnswer(answerHash, &models.Answer{
|
||||||
ID: answerID,
|
ID: uuid.New().String(),
|
||||||
Text: answer.Text,
|
Text: answer.Text,
|
||||||
})
|
})
|
||||||
if answer.Correct {
|
if answer.Correct {
|
||||||
|
@ -143,5 +145,5 @@ func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) (*
|
||||||
quiz.Answers = append(quiz.Answers, a)
|
quiz.Answers = append(quiz.Answers, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.createQuiz(quizID, quiz), nil
|
return s.createQuiz(quizID, quizHash, quiz), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue