From 45515d095abba25766b99d9a817031e4fcb052bd Mon Sep 17 00:00:00 2001
From: Dominika Zemanovicova <xzemanov@fi.muni.cz>
Date: Sat, 25 Mar 2023 03:34:27 +0100
Subject: [PATCH] Add update and delete to QuestionController

---
 .../question/QuestionController.java          | 33 ++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionController.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionController.java
index 2e997bd0..6ad8667a 100644
--- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionController.java
+++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionController.java
@@ -1,9 +1,12 @@
 package org.fuseri.moduleexercise.question;
 
+import jakarta.persistence.EntityNotFoundException;
 import org.fuseri.model.dto.common.Result;
 import org.fuseri.model.dto.exercise.QuestionCreateDto;
 import org.fuseri.model.dto.exercise.QuestionDto;
+import org.fuseri.model.dto.exercise.QuestionUpdateDto;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.server.ResponseStatusException;
 
@@ -59,6 +62,34 @@ public class QuestionController {
      */
     @PostMapping
     public QuestionDto addQuestion(@RequestBody QuestionCreateDto dto) {
-        return questionFacade.addQuestion(dto);
+        return questionFacade.create(dto);
+    }
+
+    /**
+     * Update question
+     *
+     * @param dto a QuestionDto object representing the updated question with correct id
+     * @return a QuestionUpdateDto object representing the updated question
+     * @throws ResponseStatusException if the question with id doesn't exist
+     */
+    @PutMapping("/{id}")
+    public QuestionDto updateQuestion(@PathVariable String id, @RequestBody QuestionUpdateDto dto) {
+        try {
+            return questionFacade.update(id, dto);
+        } catch (IllegalArgumentException e) {
+            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Null exercise");
+        } catch (EntityNotFoundException e) {
+            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Question not found with id: " + id);
+        }
+    }
+
+    /**
+     * Add a new question to an exercise
+     *
+     * @param id of question to delete
+     */
+    @DeleteMapping("/{id}")
+    public void deleteQuestion(@PathVariable String id) {
+        questionFacade.delete(id);
     }
 }
\ No newline at end of file
-- 
GitLab