fix: revert to get only the questions from the current user

pull/1/head 1.0.0
Nicolas Doby 2022-11-14 11:52:42 +01:00
parent bac009af8f
commit bb19bca946
9 changed files with 40 additions and 56 deletions

View File

@ -1,5 +1,6 @@
package fr.itsonus.bousoleplussbackend.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@ -44,6 +45,7 @@ public class Axe {
@OneToMany(mappedBy = "axe")
@ToString.Exclude
@JsonIgnore
private Set<Question> questions;
@Override

View File

@ -32,7 +32,7 @@ public class Question {
@ManyToOne
@JoinColumn(name = "axe_id")
@JsonIgnore
@ToString.Exclude
private Axe axe;
@JsonIgnore
@ -42,7 +42,6 @@ public class Question {
@Size(max = 200)
private String label;
@NotBlank
@Size(max = 500)
private String description;

View File

@ -1,24 +0,0 @@
package fr.itsonus.bousoleplussbackend.projections;
import fr.itsonus.bousoleplussbackend.models.Axe;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.core.config.Projection;
import java.util.List;
@Projection(name = "axeWithQuestion", types = {Axe.class})
public interface AxeWithQuestion {
Integer getIdentifier();
String getShortTitle();
String getTitle();
String getDescription();
String getColor();
@Value("#{target.getQuestions()}")
List<QuestionProj> getQuestions();
}

View File

@ -1,16 +1,11 @@
package fr.itsonus.bousoleplussbackend.repositories;
import fr.itsonus.bousoleplussbackend.models.Axe;
import fr.itsonus.bousoleplussbackend.projections.AxeWithQuestion;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
@RepositoryRestResource(excerptProjection = AxeWithQuestion.class)
public interface AxeRepository extends CrudRepository<Axe, Long> {
@RepositoryRestResource
public interface AxeRepository extends CrudRepository<Axe, Long>, JpaSpecificationExecutor<Axe> {
@RestResource(path = "me", rel = "me")
@Query("SELECT DISTINCT a FROM Axe a INNER JOIN a.questions as q WHERE q.userId = ?#{principal.id}")
Iterable<Axe> findAllWithQuestionsOfCurrentUser();
}

View File

@ -6,7 +6,6 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;
@RepositoryRestResource
public interface QuestionRepository extends CrudRepository<Question, Long> {
@ -14,4 +13,5 @@ public interface QuestionRepository extends CrudRepository<Question, Long> {
@RestResource(path="byAxeId", rel="byAxeId")
@Query("SELECT q FROM Question q JOIN q.axe a WHERE q.axe.id = :id AND user_id = ?#{principal.id}")
Iterable<Question> findAllByAxeId(@Param("id") final Long id);
}

View File

@ -44,7 +44,7 @@
import {AxiosResponse} from "axios";
import {Component, Vue} from "nuxt-property-decorator";
import {RepositoryFactory} from "~/repositories/RepositoryFactory";
import {AxeWithQuestions} from "~/repositories/models/axe.model";
import {Axe} 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";
@ -54,9 +54,10 @@ import {quizStore} from "~/utils/store-accessor";
export default class Login extends Vue {
readonly axeRepository = RepositoryFactory.get("axe");
readonly questionRepository = RepositoryFactory.get("question");
private axes: AxeWithQuestions[] = [];
private currentAxe?: AxeWithQuestions | undefined;
private axes: Axe[] = [];
private currentAxe?: Axe | undefined;
private currentAxeIdentifier = 1;
private questions: Map<number, Question[]> = new Map<number, []>();
private loading = true;
@ -67,17 +68,28 @@ export default class Login extends Vue {
this.loading = true;
this.axeRepository
.findAll()
.then((response: AxiosResponse<RestResponse<AxeWithQuestions>>) => {
.then((response: AxiosResponse<RestResponse<Axe>>) => {
this.axes = response.data._embedded.axes;
const promises: any[] = [];
this.axes.forEach(axe => {
this.questions.set(axe.identifier, axe.questions);
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.initializeState();
quizStore.initialize(this.questions);
})
.finally(() => {
this.loading = false;
});
}

View File

@ -1,11 +1,11 @@
import AxeRepository from "./axeRepository";
import QuestionRepository from "~/repositories/questionRepository";
import QuizRepository from "~/repositories/quizRepository";
import QuestionRepository from "~/repositories/questionRepository";
const repositories = {
axe: AxeRepository,
question: QuestionRepository,
quiz: QuizRepository,
question: QuestionRepository,
// other repositories ...
};

View File

@ -1,9 +1,9 @@
import {RestResponse} from "~/repositories/models/rest-response.model";
import {$axios} from "~/utils/api";
import {AxeWithQuestions} from "~/repositories/models/axe.model";
import {Axe} from "~/repositories/models/axe.model";
export default {
findAll() {
return $axios.get<RestResponse<AxeWithQuestions>>("/axes/search/me");
return $axios.get<RestResponse<Axe>>("/axes");
}
}

View File

@ -11,11 +11,11 @@ export default {
}
})
.then((response) => {
response.data._embedded.questions.forEach(question => {
question.id = Number(question._links.self.href.split("/").reverse()[0]);
return question;
response.data._embedded.questions.forEach(question => {
question.id = Number(question._links.self.href.split("/").reverse()[0]);
return question;
});
return response;
});
return response;
});
}
}