Hej powiem mi ktoś co robię źle i jak to mam zrobić bo już nie wiem całkiem coś pokręciłem. Mam metode do aktualizacji pytania i odpowiedzi quizu.
Pytanie sie aktualizuje a odpowiedzi i nie wiem czemu.
public void updateQuestions(Long questionId, Questions questions, Principal principal) {
String username = principal.getName();
User user = userRepository.findByEmail(username).orElseThrow(() -> new EntityNotFoundException("Not found user"));
Questions questionToUpdate = questionsRepository.findById(questionId).orElseThrow(() -> new EntityNotFoundException("Question not found"));
Quiz quiz = questionToUpdate.getQuiz();
if (quiz == null || !quiz.getUserId().equals(user.getId())) {
throw new UnsupportedOperationException("You are not authorized to update this question");
}
questionToUpdate.setContents(questions.getContents());
for (Answers answer : questionToUpdate.getAnswers()) {
answer.setQuestion(questionToUpdate);
for (Answers updatedAnswer : questions.getAnswers()) {
if (answer.getId().equals(updatedAnswer.getId())) {
answer.setContents(updatedAnswer.getContents());
}
}
}
questionsRepository.save(questionToUpdate);
}
@Entity
@Data
public class Questions {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String contents;
@ManyToOne
@JoinColumn(name = "quiz_id")
private Quiz quiz;
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Answers> answers = new ArrayList<>();
}