feat: review model to retrieve all the questions with a single query

- update initial data
- add description field to the axe
This commit is contained in:
2022-11-02 11:16:17 +01:00
parent 075a508bb5
commit 3c4928ae61
20 changed files with 269 additions and 155 deletions

View File

@@ -37,7 +37,7 @@ import {Component, Vue} from "vue-property-decorator";
import {AxiosResponse} from "axios";
import {RepositoryFactory} from "~/repositories/RepositoryFactory";
import {RestResponse} from "~/repositories/models/rest-response.model";
import {Quiz, Score} from "~/repositories/models/quiz.model";
import {Quiz} from "~/repositories/models/quiz.model";
@Component
export default class History extends Vue {
@@ -52,12 +52,6 @@ export default class History extends Vue {
await this.quizRepository.findMine().then((response: AxiosResponse<RestResponse<Quiz>>) => {
this.quizzes = response.data._embedded.quizzes;
});
for (const quiz of this.quizzes) {
await this.quizRepository.findScores(quiz.id)
.then((value: AxiosResponse<Score[]>) => {
quiz.scores = value.data;
});
}
this.currentResult = this.quizzes.length > 0 ? this.quizzes[0] : null;
this.loading = false;
}

View File

@@ -26,6 +26,7 @@ import {Quiz, ResponseWithQuestion, Score} from "~/repositories/models/quiz.mode
import QuizAxeDetails from "~/components/QuizAxeDetails.vue";
import {Axe} from "~/repositories/models/axe.model";
import {RestResponse} from "~/repositories/models/rest-response.model";
@Component({
components: {QuizAxeDetails}
})
@@ -36,7 +37,6 @@ export default class Result extends Vue {
private axes: Axe[] = [];
private quiz: Quiz | null = null;
private scores: Score[] = [];
private responses: ResponseWithQuestion[] = [];
private loading = false;
@@ -50,28 +50,15 @@ export default class Result extends Vue {
this.quizRepository.findById(quizId)
.then((response: AxiosResponse<Quiz>) => {
this.quiz = response.data;
this.responses = response.data._embedded.responses;
return response;
})
.then(() => {
return this.quizRepository.findScores(quizId)
.then((response: AxiosResponse<Score[]>) => {
this.scores = response.data;
return response;
})
})
.then(() => {
return this.quizRepository.findResponses(quizId)
.then((response: AxiosResponse<RestResponse<ResponseWithQuestion>>) => {
this.responses = response.data._embedded.responses;
return response;
})
})
.then(() => {
return this.axeRepository.findAll()
.then((response: AxiosResponse<RestResponse<Axe>>) => {
this.axes = response.data._embedded.axes;
return response;
});
this.axes = response.data._embedded.axes;
return response;
});
})
.finally(() => {
this.loading = false;
@@ -82,8 +69,12 @@ export default class Result extends Vue {
}
}
getScore(axe: Axe) {
return this.scores.find(value => value.axeIdentifier === axe.identifier);
getScore(axe: Axe): Score {
const responses = this.getResponses(axe);
return {
axeIdentifier: axe.identifier,
scoreAvg: responses.reduce((total, response) => total + response.score, 0) / responses.length
};
}
getResponses(axe: Axe) {

View File

@@ -7,6 +7,7 @@
<quiz-part
:key="currentAxe.identifier" :axe-number="currentAxe.identifier" :total-axes="axes.length"
:title="currentAxe.title"
:description="currentAxe.description"
:color="currentAxe.color"
:icon="'balise_' + currentAxe.identifier + '.svg'"
:questions="questions.get(currentAxe.identifier)"
@@ -43,7 +44,7 @@
import {AxiosResponse} from "axios";
import {Component, Vue} from "nuxt-property-decorator";
import {RepositoryFactory} from "~/repositories/RepositoryFactory";
import {Axe} from "~/repositories/models/axe.model";
import {AxeWithQuestions} from "~/repositories/models/axe.model";
import {RestResponse} from "~/repositories/models/rest-response.model";
import {Question} from "~/repositories/models/question.model";
import {Quiz} from "~/repositories/models/quiz.model";
@@ -53,10 +54,9 @@ import {quizStore} from "~/utils/store-accessor";
export default class Login extends Vue {
readonly axeRepository = RepositoryFactory.get("axe");
readonly questionRepository = RepositoryFactory.get("question");
private axes: Axe[] = [];
private currentAxe?: Axe;
private axes: AxeWithQuestions[] = [];
private currentAxe?: AxeWithQuestions | undefined;
private currentAxeIdentifier = 1;
private questions: Map<number, Question[]> = new Map<number, []>();
private loading = true;
@@ -67,28 +67,17 @@ export default class Login extends Vue {
this.loading = true;
this.axeRepository
.findAll()
.then((response: AxiosResponse<RestResponse<Axe>>) => {
.then((response: AxiosResponse<RestResponse<AxeWithQuestions>>) => {
this.axes = response.data._embedded.axes;
const promises: any[] = [];
this.axes.forEach(axe => {
promises.push(
this.questionRepository
.findAllByAxeId(axe.identifier)
.then((response: AxiosResponse<RestResponse<Question>>) => {
return {
axeId: axe.identifier,
questions: response.data._embedded.questions
};
}));
});
Promise.all(promises).then((axeQuestions) => {
axeQuestions.forEach(axeQuestion => {
this.questions.set(axeQuestion.axeId, axeQuestion.questions)
});
quizStore.initialize(this.questions);
this.initializeState();
this.loading = false;
this.questions.set(axe.identifier, axe.questions);
});
this.initializeState();
quizStore.initialize(this.questions);
})
.finally(() => {
this.loading = false;
});
}