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;
|
package io.titan.portfolio.controller;
|
||||||
|
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
@ -26,39 +29,74 @@ public class EducationController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
List<Education> getAllEducation(){
|
public ResponseEntity<List<Education>> getAllEducation() {
|
||||||
return educationService.getAllEducation();
|
List<Education> educations = educationService.getAllEducation();
|
||||||
|
return ResponseEntity.ok(educations);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
ResponseEntity<Education> getEducationById(@PathVariable Long id) {
|
public ResponseEntity<Education> getEducationById(@PathVariable Long id) {
|
||||||
return educationService.getEducationById(id)
|
return educationService.getEducationById(id)
|
||||||
.map(ResponseEntity::ok)
|
.map(ResponseEntity::ok)
|
||||||
.orElse(ResponseEntity.notFound().build());
|
.orElse(ResponseEntity.notFound().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
ResponseEntity<Void> createEducation(@RequestBody Education education) {
|
public ResponseEntity<Education> createEducation(@RequestBody Education education) {
|
||||||
educationService.createEducation(education);
|
try {
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
Education createdEducation = educationService.createEducation(education);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(createdEducation);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return ResponseEntity.badRequest().body(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@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())) {
|
if (!id.equals(education.id())) {
|
||||||
return ResponseEntity.badRequest().build();
|
return ResponseEntity.badRequest().build();
|
||||||
}
|
}
|
||||||
educationService.updateEducation(education);
|
try {
|
||||||
return ResponseEntity.ok().build();
|
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}")
|
@DeleteMapping("/{id}")
|
||||||
ResponseEntity<Void> deleteEducation(@PathVariable Long id) {
|
public ResponseEntity<Void> deleteEducation(@PathVariable Long id) {
|
||||||
boolean deleted = educationService.deleteEducation(id);
|
try {
|
||||||
if (deleted) {
|
educationService.deleteEducation(id);
|
||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.noContent().build();
|
||||||
} else {
|
} catch (EmptyResultDataAccessException e) {
|
||||||
return ResponseEntity.notFound().build();
|
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.model.Education;
|
||||||
import io.titan.portfolio.repository.EducationRepository;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -15,27 +19,76 @@ public class EducationService{
|
||||||
this.educationRepository = educationRepository;
|
this.educationRepository = educationRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Education> getAllEducation(){
|
public List<Education> getAllEducation() {
|
||||||
|
try {
|
||||||
return educationRepository.findAll();
|
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){
|
public Optional<Education> getEducationById(Long id) {
|
||||||
|
try {
|
||||||
return educationRepository.findById(id);
|
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) {
|
public Education createEducation(Education education) {
|
||||||
|
try {
|
||||||
return educationRepository.save(education);
|
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);
|
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) {
|
public void deleteEducation(Long id) {
|
||||||
if (educationRepository.existsById(id)) {
|
try {
|
||||||
educationRepository.deleteById(id);
|
if (!educationRepository.existsById(id)) {
|
||||||
return true;
|
throw new EmptyResultDataAccessException("Education record not found with id: " + id, 1);
|
||||||
|
}
|
||||||
|
educationRepository.deleteById(id);
|
||||||
|
} 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);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
0.1.1
|
0.1.2
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue