Skip to content
Snippets Groups Projects
Commit 098ebde2 authored by Heine Mærde Brakstad's avatar Heine Mærde Brakstad
Browse files

changed save question endpoint to be separate for create and update

parent 966e87b4
No related branches found
No related tags found
1 merge request!50Questions endpoint
Pipeline #270056 passed
......@@ -4,7 +4,9 @@ import edu.ntnu.idatt2105.dto.QuestionDTO;
import edu.ntnu.idatt2105.model.Question;
import edu.ntnu.idatt2105.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
import java.util.stream.Collectors;
......@@ -35,22 +37,31 @@ public class QuestionController {
* @param questionDTO The question data to be saved.
* @return The saved question DTO.
*/
@PostMapping("/save")
public QuestionDTO saveQuestion(@RequestBody QuestionDTO questionDTO) {
Question question = questionService.createOrUpdateQuestion(questionDTO);
@PostMapping("/newQuestion")
public QuestionDTO createNewQuestion(@RequestBody QuestionDTO questionDTO) {
if (questionDTO.getId() != null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "ID should be null for new questions");
}
Question question = questionService.createQuestion(questionDTO);
return mapQuestionToQuestionDTO(question);
}
// TODO: make a mapper class to do this
return new QuestionDTO(
question.getId(),
question.getQuestionText(),
question.getType(),
question.getAnswer(),
question.getOptionsList(),
question.getScore(),
question.getQuiz().getId()
);
/**
* Endpoint for updating an existing question.
* @param questionDTO The question data to be updated
* @return The updated question DTO.
*/
@PutMapping("/questions/{id}")
public QuestionDTO updateExistingQuestion(@PathVariable Integer id, @RequestBody QuestionDTO questionDTO) {
if (questionDTO.getId() == null || !questionDTO.getId().equals(id)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Question ID mismatch");
}
Question question = questionService.updateQuestion(questionDTO);
return mapQuestionToQuestionDTO(question);
}
/**
* Endpoint for retrieving a question by ID.
*
......@@ -61,7 +72,6 @@ public class QuestionController {
public QuestionDTO getQuestion(@PathVariable Integer questionId) {
Question question = questionService.findQuestionById(questionId);
return new QuestionDTO(
question.getId(),
question.getQuestionText(),
question.getType(),
question.getAnswer(),
......@@ -92,7 +102,6 @@ public class QuestionController {
List<Question> questions = questionService.findAllQuestionsToAQuiz(quizId);
return questions.stream()
.map(question -> new QuestionDTO(
question.getId(),
question.getQuestionText(),
question.getType(),
question.getAnswer(),
......@@ -102,4 +111,16 @@ public class QuestionController {
))
.collect(Collectors.toList());
}
private QuestionDTO mapQuestionToQuestionDTO(Question question) {
return new QuestionDTO(
question.getQuestionText(),
question.getType(),
question.getAnswer(),
question.getOptionsList(),
question.getScore(),
question.getQuiz().getId()
);
}
}
......@@ -29,7 +29,6 @@ public class QuestionDTO {
/**
* Constructor for QuestionDTO.
*
* @param id The ID of the question.
* @param questionText The text of the question.
* @param type The type of the question.
* @param answer The correct answer to the question.
......@@ -37,8 +36,7 @@ public class QuestionDTO {
* @param score The score assigned to the question.
* @param quizId The ID of the quiz the question belongs to.
*/
public QuestionDTO(Integer id, String questionText, QuestionType type, String answer, List<String> options, int score, Integer quizId) {
this.id = id;
public QuestionDTO(String questionText, QuestionType type, String answer, List<String> options, int score, Integer quizId) {
this.questionText = questionText;
this.type = type;
this.answer = answer;
......
......@@ -5,6 +5,7 @@ import edu.ntnu.idatt2105.model.Question;
import edu.ntnu.idatt2105.model.QuestionType;
import edu.ntnu.idatt2105.repository.QuestionRepository;
import edu.ntnu.idatt2105.repository.QuizRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -34,41 +35,51 @@ public class QuestionService {
}
/**
* Creates or updates a question.
* Creates a question.
*
* @param questionDTO The DTO containing the question details.
* @return The created or updated question.
* @return The created question.
*/
@Transactional
public Question createOrUpdateQuestion(QuestionDTO questionDTO) {
Question question;
if (questionDTO.getId() != null) {
Optional<Question> optionalQuestion = questionRepository.findById(questionDTO.getId());
if (!optionalQuestion.isPresent()) {
return null;
public Question createQuestion(QuestionDTO questionDTO) {
Question question = new Question();
question.setQuestionText(questionDTO.getQuestionText());
question.setType(questionDTO.getType());
if (questionDTO.getType() == QuestionType.TRUE_OR_FALSE) {
question.setOptions("TRUE*FALSE");
} else if (questionDTO.getOptions() != null) {
question.setOptions(questionDTO.getOptionsAsString());
}
question = optionalQuestion.get();
} else {
question = new Question();
question.setAnswer(questionDTO.getAnswer());
question.setScore(questionDTO.getScore());
question.setQuiz(quizRepository.findById(questionDTO.getQuizId())
.orElseThrow(() -> new EntityNotFoundException("Quiz not found for ID: " + questionDTO.getQuizId())));
return questionRepository.save(question);
}
/**
* Updates a question
*
* @param questionDTO The DTO containing the question details.
* @return The updated question.
*/
public Question updateQuestion(QuestionDTO questionDTO) {
Question question = questionRepository.findById(questionDTO.getId())
.orElseThrow(() -> new EntityNotFoundException("Question not found for ID: " + questionDTO.getId()));
question.setQuestionText(questionDTO.getQuestionText());
question.setType(questionDTO.getType());
if (questionDTO.getType().equals(QuestionType.MULTIPLE_CHOICE)) {
if (questionDTO.getOptions() != null) {
question.setOptions(questionDTO.getOptionsAsString());
} else if (questionDTO.getType().equals(QuestionType.TRUE_OR_FALSE)) {
question.setOptions("TRUE*FALSE");
} else {
question.setOptions(null);
}
question.setAnswer(questionDTO.getAnswer());
question.setScore(questionDTO.getScore());
question.setQuiz(quizRepository.findById(questionDTO.getQuizId()).orElse(null));
return questionRepository.save(question);
}
/**
* Deletes a question by its ID.
*
......
/*
package edu.ntnu.idatt2105.service;
import edu.ntnu.idatt2105.dto.QuestionDTO;
import edu.ntnu.idatt2105.model.Question;
import edu.ntnu.idatt2105.model.QuestionType;
import edu.ntnu.idatt2105.model.Quiz;
import edu.ntnu.idatt2105.repository.QuestionRepository;
import edu.ntnu.idatt2105.repository.QuizRepository;
import org.junit.jupiter.api.BeforeEach;
......@@ -27,15 +29,20 @@ class QuestionServiceTest {
private QuizRepository quizRepository;
private Quiz quiz;
@BeforeEach
void setUp() {
questionRepository = mock(QuestionRepository.class);
quizRepository = mock(QuizRepository.class);
questionService = new QuestionService(questionRepository, quizRepository);
quiz = new Quiz();
quiz.setId(1);
}
@Test
void testCreateOrUpdateQuestion_NewMultipleChoice() {
void testCreateQuestion_NewMultipleChoice() {
QuestionDTO questionDTO = new QuestionDTO();
questionDTO.setType(QuestionType.MULTIPLE_CHOICE);
questionDTO.setQuestionText("What is the largest planet?");
......@@ -46,11 +53,12 @@ class QuestionServiceTest {
when(questionRepository.save(any(Question.class))).thenAnswer(invocation -> invocation.getArgument(0));
Question result = questionService.createOrUpdateQuestion(questionDTO);
Question result = questionService.createQuestion(questionDTO);
verify(questionRepository).save(any(Question.class));
assertNotNull(result);
assertEquals(1,result.getId());
assertEquals("Jupiter", result.getAnswer());
assertEquals(2, result.getScore());
assertEquals("What is the largest planet?", result.getQuestionText());
......@@ -58,7 +66,6 @@ class QuestionServiceTest {
assertEquals("Earth*Mars*Jupiter*Venus", result.getOptions());
}
@Test
void testCreateOrUpdateQuestion_TrueOrFalse() {
QuestionDTO questionDTO = new QuestionDTO();
......@@ -70,7 +77,7 @@ class QuestionServiceTest {
when(questionRepository.save(any(Question.class))).thenAnswer(invocation -> invocation.getArgument(0));
Question result = questionService.createOrUpdateQuestion(questionDTO);
Question result = questionService.createQuestion(questionDTO);
verify(questionRepository).save(any(Question.class));
......@@ -94,7 +101,7 @@ class QuestionServiceTest {
when(questionRepository.save(any(Question.class))).thenAnswer(invocation -> invocation.getArgument(0));
Question result = questionService.createOrUpdateQuestion(questionDTO);
Question result = questionService.createQuestion(questionDTO);
verify(questionRepository).save(any(Question.class));
......@@ -117,7 +124,7 @@ class QuestionServiceTest {
when(questionRepository.save(any(Question.class))).thenAnswer(invocation -> invocation.getArgument(0));
Question result = questionService.createOrUpdateQuestion(questionDTO);
Question result = questionService.createQuestion(questionDTO);
verify(questionRepository).save(any(Question.class));
......@@ -135,7 +142,7 @@ class QuestionServiceTest {
when(questionRepository.save(any(Question.class))).thenAnswer(invocation -> invocation.getArgument(0));
Question updatedResult = questionService.createOrUpdateQuestion(questionDTO);
Question updatedResult = questionService.updateQuestion(questionDTO);
assertEquals("Mercury", updatedResult.getAnswer());
......@@ -177,3 +184,6 @@ class QuestionServiceTest {
}
*/
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment