v0.1.2 Add more exception handlers, and updated REST API behaviour
This commit is contained in:
parent
85f61a6ce5
commit
e248766073
4 changed files with 128 additions and 37 deletions
|
|
@ -1,8 +1,11 @@
|
|||
package io.titan.portfolio.controller;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
|
@ -26,39 +29,74 @@ public class EducationController {
|
|||
}
|
||||
|
||||
@GetMapping
|
||||
List<Education> getAllEducation(){
|
||||
return educationService.getAllEducation();
|
||||
public ResponseEntity<List<Education>> getAllEducation() {
|
||||
List<Education> educations = educationService.getAllEducation();
|
||||
return ResponseEntity.ok(educations);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
ResponseEntity<Education> getEducationById(@PathVariable Long id) {
|
||||
public ResponseEntity<Education> getEducationById(@PathVariable Long id) {
|
||||
return educationService.getEducationById(id)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
ResponseEntity<Void> createEducation(@RequestBody Education education) {
|
||||
educationService.createEducation(education);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
public ResponseEntity<Education> createEducation(@RequestBody Education education) {
|
||||
try {
|
||||
Education createdEducation = educationService.createEducation(education);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(createdEducation);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.badRequest().body(null);
|
||||
}
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
ResponseEntity<Void> updateEducation(@PathVariable Long id, @RequestBody Education education) {
|
||||
public ResponseEntity<Education> updateEducation(@PathVariable Long id, @RequestBody Education education) {
|
||||
if (!id.equals(education.id())) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
educationService.updateEducation(education);
|
||||
return ResponseEntity.ok().build();
|
||||
try {
|
||||
Education updatedEducation = educationService.updateEducation(education);
|
||||
return ResponseEntity.ok(updatedEducation);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return ResponseEntity.notFound().build();
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.badRequest().body(null);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
ResponseEntity<Void> deleteEducation(@PathVariable Long id) {
|
||||
boolean deleted = educationService.deleteEducation(id);
|
||||
if (deleted) {
|
||||
return ResponseEntity.ok().build();
|
||||
} else {
|
||||
public ResponseEntity<Void> deleteEducation(@PathVariable Long id) {
|
||||
try {
|
||||
educationService.deleteEducation(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@ExceptionHandler(EmptyResultDataAccessException.class)
|
||||
public ResponseEntity<String> handleNotFound(EmptyResultDataAccessException e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public ResponseEntity<String> handleBadRequest(IllegalArgumentException e) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(DataAccessException.class)
|
||||
public ResponseEntity<String> handleDataAccessException(DataAccessException e) {
|
||||
// Log the exception
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred");
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<String> handleGeneralException(Exception e) {
|
||||
// Log the exception
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred");
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,10 @@ package io.titan.portfolio.service;
|
|||
|
||||
import io.titan.portfolio.model.Education;
|
||||
import io.titan.portfolio.repository.EducationRepository;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -16,26 +20,75 @@ public class EducationService{
|
|||
}
|
||||
|
||||
public List<Education> getAllEducation() {
|
||||
try {
|
||||
return educationRepository.findAll();
|
||||
} catch (DataAccessException e) {
|
||||
// Log the error
|
||||
System.err.println("Error retrieving all education records: " + e.getMessage());
|
||||
throw new RuntimeException("Failed to retrieve education records", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Education> getEducationById(Long id) {
|
||||
try {
|
||||
return educationRepository.findById(id);
|
||||
} catch (DataAccessException e) {
|
||||
// Log the error
|
||||
System.err.println("Error retrieving education by id: " + e.getMessage());
|
||||
throw new RuntimeException("Failed to retrieve education record", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Education createEducation(Education education) {
|
||||
try {
|
||||
return educationRepository.save(education);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
// Log the error
|
||||
System.err.println("Error creating education record: " + e.getMessage());
|
||||
throw new IllegalArgumentException("Invalid education data: " + e.getMessage());
|
||||
} catch (DataAccessException e) {
|
||||
// Log the error
|
||||
System.err.println("Error creating education record: " + e.getMessage());
|
||||
throw new RuntimeException("Failed to create education record", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateEducation(Education education) {
|
||||
public Education updateEducation(Education education) {
|
||||
try {
|
||||
if (!educationRepository.existsById(education.id())) {
|
||||
throw new EmptyResultDataAccessException("Education record not found with id: " + education.id(), 1);
|
||||
}
|
||||
educationRepository.update(education);
|
||||
return education;
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
// Log the error
|
||||
System.err.println("Education record not found: " + e.getMessage());
|
||||
throw e;
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
// Log the error
|
||||
System.err.println("Error updating education record: " + e.getMessage());
|
||||
throw new IllegalArgumentException("Invalid education data: " + e.getMessage());
|
||||
} catch (DataAccessException e) {
|
||||
// Log the error
|
||||
System.err.println("Error updating education record: " + e.getMessage());
|
||||
throw new RuntimeException("Failed to update education record", e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteEducation(Long id) {
|
||||
if (educationRepository.existsById(id)) {
|
||||
public void deleteEducation(Long id) {
|
||||
try {
|
||||
if (!educationRepository.existsById(id)) {
|
||||
throw new EmptyResultDataAccessException("Education record not found with id: " + id, 1);
|
||||
}
|
||||
educationRepository.deleteById(id);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
// Log the error
|
||||
System.err.println("Education record not found: " + e.getMessage());
|
||||
throw e;
|
||||
} catch (DataAccessException e) {
|
||||
// Log the error
|
||||
System.err.println("Error deleting education record: " + e.getMessage());
|
||||
throw new RuntimeException("Failed to delete education record", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
0.1.1
|
||||
0.1.2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue