Skip to content

Commit e54e137

Browse files
committed
✅ crud exception handling done
1 parent 1110d41 commit e54e137

File tree

10 files changed

+41
-13
lines changed

10 files changed

+41
-13
lines changed

QuizApp/Screenshots/ss1.PNG

253 KB
Loading

QuizApp/Screenshots/ss2.PNG

250 KB
Loading

QuizApp/Screenshots/ss3.PNG

261 KB
Loading

QuizApp/Screenshots/ss4.PNG

225 KB
Loading

QuizApp/Screenshots/ss5.PNG

255 KB
Loading

QuizApp/Screenshots/ss6.PNG

263 KB
Loading

QuizApp/Screenshots/ss7.PNG

262 KB
Loading

QuizApp/src/main/java/com/example/myquizapp/controller/QuestionController.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.example.myquizapp.controller;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
69
import org.springframework.web.bind.annotation.DeleteMapping;
710
import org.springframework.web.bind.annotation.GetMapping;
811
import org.springframework.web.bind.annotation.PathVariable;
@@ -26,23 +29,29 @@ public class QuestionController {
2629

2730
// url => http://localhost:8080/question/allQuestions
2831
@GetMapping("allQuestions")
29-
public List<Question> getAllQuestion() {
30-
31-
// return "Hi, these are your all questions2";
32-
return questionService.getAllQuestions();
32+
public ResponseEntity<List<Question>> getAllQuestion() {
33+
// either we can do exception handling in controller or in service, as per your wish!
34+
// return "Hi, these are your all questions2";
35+
try {
36+
return new ResponseEntity<>(questionService.getAllQuestions(), HttpStatus.OK);
37+
}catch(Exception e) {
38+
e.printStackTrace();
39+
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.BAD_REQUEST);
40+
}
3341
}
3442

3543
@GetMapping("category/{category}")
36-
public List<Question> getQuestionsByCategory(@PathVariable String category){
44+
public ResponseEntity<List<Question>> getQuestionsByCategory(@PathVariable String category){
3745
// if multiple pathvariables, eg. category/{mycategory}/{mydifficulty} then -
3846
// public List<Question> getQuestionsByCategory(@PathVariable("mycategory") String category, @PathVariable("mydifficulty") String difficulty){
47+
48+
// in this example, exception handling is done in service instead of inside controller
3949
return questionService.getQuestionsByCategory(category);
4050
}
4151

4252
@PostMapping("add")
43-
public String addQuestion(@RequestBody Question question) { // accepting JSON from client
44-
questionService.addQuestion(question);
45-
return "successss!!!";
53+
public ResponseEntity<String> addQuestion(@RequestBody Question question) { // accepting JSON from client
54+
return questionService.addQuestion(question);
4655
}
4756

4857
@DeleteMapping("delete/{qid}")

QuizApp/src/main/java/com/example/myquizapp/service/QuestionService.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.example.myquizapp.service;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
69
import org.springframework.stereotype.Service;
710
import org.springframework.web.bind.annotation.GetMapping;
811
import org.springframework.web.bind.annotation.PathVariable;
@@ -25,17 +28,25 @@ public List<Question> getAllQuestions(){
2528
// List<Pet> findAllByAge(Integer age);
2629
}
2730

28-
public List<Question> getQuestionsByCategory(String category){
29-
return questionDao.findByCategory(category);
31+
32+
public ResponseEntity<List<Question>> getQuestionsByCategory(String category){
33+
// no need to do exception handling in QuestionService as we have already done in QuestionController, here we are just returning data!
34+
try {
35+
return new ResponseEntity<>(questionDao.findByCategory(category), HttpStatus.OK);
36+
}catch(Exception e) {
37+
e.printStackTrace();
38+
}
39+
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.BAD_REQUEST);
3040
}
3141

32-
public Question getQuestionById(Integer id) {
42+
public Question getQuestionById(Integer id) {
3343
return questionDao.getById(id);
3444
}
3545

36-
public String addQuestion(Question question) {
46+
public ResponseEntity<String> addQuestion(Question question) {
47+
3748
questionDao.save(question);
38-
return "successss!!!";
49+
return new ResponseEntity<>("successss!!!", HttpStatus.CREATED);
3950
}
4051

4152
public String deleteQuestionById(Integer id) {

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Screenshots (Wait for 5-10 PNG screenshot files to load)
2+
3+
<img src="./QuizApp/Screenshots/ss1.PNG" alt="getAllQuestions" >
4+
<img src="./QuizApp/Screenshots/ss2.PNG" alt="getAllQuestions By Category In Params" >
5+
<img src="./QuizApp/Screenshots/ss3.PNG" alt="create new question using Post request" >
6+
<img src="./QuizApp/Screenshots/ss4.PNG" alt="create question using Delete request by passing id in params" >
7+
<img src="./QuizApp/Screenshots/ss6.PNG" alt="update question using Delete request by passing id in params and new updated body of question" >
8+
<img src="./QuizApp/Screenshots/ss6.PNG" alt="Exception handling done!" >

0 commit comments

Comments
 (0)