Skip to content
Snippets Groups Projects
Commit e37b3ba7 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Change question update to patchUpdate

parent c6a529bc
No related branches found
No related tags found
3 merge requests!31M2,!30M2 exercise,!29M2 exercise
...@@ -119,7 +119,7 @@ public class QuestionController { ...@@ -119,7 +119,7 @@ public class QuestionController {
@PutMapping("/{id}") @PutMapping("/{id}")
public ResponseEntity<QuestionDto> updateQuestion(@NotNull @PathVariable long id, @Valid @RequestBody QuestionUpdateDto dto) { public ResponseEntity<QuestionDto> updateQuestion(@NotNull @PathVariable long id, @Valid @RequestBody QuestionUpdateDto dto) {
try { try {
return ResponseEntity.ok(questionFacade.update(id, dto)); return ResponseEntity.ok(questionFacade.patchUpdate(id, dto));
} catch (EntityNotFoundException e) { } catch (EntityNotFoundException e) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
......
...@@ -101,13 +101,9 @@ public class QuestionFacade { ...@@ -101,13 +101,9 @@ public class QuestionFacade {
* @param dto dto of updated question with correct id * @param dto dto of updated question with correct id
* @return dto of updated question * @return dto of updated question
*/ */
public QuestionDto update(long id, QuestionUpdateDto dto) { public QuestionDto patchUpdate(long id, QuestionUpdateDto dto) {
Question question = questionMapper.fromUpdateDto(dto); var a = questionService.patchUpdateWithoutAnswers(id, questionMapper.fromUpdateDto(dto));
question.setId(id); return questionMapper.toDto(a);
List<Answer> questionAnswers = answerService.findAllByQuestionId(id);
question.setAnswers(new HashSet<>(questionAnswers));
Question updatedQuestion = questionService.update(id, question);
return questionMapper.toDto(updatedQuestion);
} }
/** /**
......
...@@ -4,11 +4,11 @@ import jakarta.persistence.EntityNotFoundException; ...@@ -4,11 +4,11 @@ import jakarta.persistence.EntityNotFoundException;
import lombok.Getter; import lombok.Getter;
import org.fuseri.moduleexercise.common.DomainService; import org.fuseri.moduleexercise.common.DomainService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/** /**
* Represent a service for managing Question entities * Represent a service for managing Question entities
*/ */
...@@ -43,4 +43,25 @@ public class QuestionService extends DomainService<Question> { ...@@ -43,4 +43,25 @@ public class QuestionService extends DomainService<Question> {
return repository.findById(id) return repository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Question '" + id + "' not found.")); .orElseThrow(() -> new EntityNotFoundException("Question '" + id + "' not found."));
} }
/**
* Patch update a question. Don't update question answers.
*
* @param id the question ID
* @param updatedQuestion the question to update
* @return the updated question
*/
@Transactional
public Question patchUpdateWithoutAnswers(Long id, Question updatedQuestion) {
Optional<Question> optionalQuestion = repository.findById(id);
if (optionalQuestion.isPresent()) {
Question question = optionalQuestion.get();
question.setText(updatedQuestion.getText());
question.setExercise(updatedQuestion.getExercise());
return repository.save(question);
} else {
throw new EntityNotFoundException("Question with id: " + id + " was not found.");
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment