diff --git a/src/main/java/io/titan/portfolio/controller/EducationController.java b/src/main/java/io/titan/portfolio/controller/EducationController.java index c265a82..87b939f 100644 --- a/src/main/java/io/titan/portfolio/controller/EducationController.java +++ b/src/main/java/io/titan/portfolio/controller/EducationController.java @@ -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 getAllEducation(){ - return educationService.getAllEducation(); + public ResponseEntity> getAllEducation() { + List educations = educationService.getAllEducation(); + return ResponseEntity.ok(educations); } @GetMapping("/{id}") - ResponseEntity getEducationById(@PathVariable Long id) { + public ResponseEntity getEducationById(@PathVariable Long id) { return educationService.getEducationById(id) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } @PostMapping - ResponseEntity createEducation(@RequestBody Education education) { - educationService.createEducation(education); - return ResponseEntity.status(HttpStatus.CREATED).build(); + public ResponseEntity 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 updateEducation(@PathVariable Long id, @RequestBody Education education) { + public ResponseEntity 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 deleteEducation(@PathVariable Long id) { - boolean deleted = educationService.deleteEducation(id); - if (deleted) { - return ResponseEntity.ok().build(); - } else { + public ResponseEntity deleteEducation(@PathVariable Long id) { + try { + educationService.deleteEducation(id); + return ResponseEntity.noContent().build(); + } catch (EmptyResultDataAccessException e) { return ResponseEntity.notFound().build(); } } -} \ No newline at end of file + + @ExceptionHandler(EmptyResultDataAccessException.class) + public ResponseEntity handleNotFound(EmptyResultDataAccessException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleBadRequest(IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); + } + + @ExceptionHandler(DataAccessException.class) + public ResponseEntity 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 handleGeneralException(Exception e) { + // Log the exception + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred"); + } +} diff --git a/src/main/java/io/titan/portfolio/model/Education.java b/src/main/java/io/titan/portfolio/model/Education.java index 05d6e8d..d479f03 100644 --- a/src/main/java/io/titan/portfolio/model/Education.java +++ b/src/main/java/io/titan/portfolio/model/Education.java @@ -20,10 +20,10 @@ public record Education( this(null, null, null, null, null, null, null, null, null, null, null, null); } - public Education(String title, String organization, String logo, String location, String duration, - String description, List achievements, List skills, + public Education(String title, String organization, String logo, String location, String duration, + String description, List achievements, List skills, List techStack, String skillsTitle, String techStackTitle) { - this(null, title, organization, logo, location, duration, description, + this(null, title, organization, logo, location, duration, description, achievements, skills, techStack, skillsTitle, techStackTitle); } -} \ No newline at end of file +} diff --git a/src/main/java/io/titan/portfolio/service/EducationService.java b/src/main/java/io/titan/portfolio/service/EducationService.java index 02979f1..4386c28 100644 --- a/src/main/java/io/titan/portfolio/service/EducationService.java +++ b/src/main/java/io/titan/portfolio/service/EducationService.java @@ -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; @@ -10,32 +14,81 @@ import java.util.Optional; @Service public class EducationService{ private final EducationRepository educationRepository; - + public EducationService(EducationRepository educationRepository){ this.educationRepository = educationRepository; } - public List getAllEducation(){ - return educationRepository.findAll(); + public List 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 getEducationById(Long id){ - return educationRepository.findById(id); + public Optional 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) { - return educationRepository.save(education); - } - - public void updateEducation(Education education) { - educationRepository.update(education); - } - - public boolean deleteEducation(Long id) { - if (educationRepository.existsById(id)) { - educationRepository.deleteById(id); - return true; + 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 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 void deleteEducation(Long id) { + try { + if (!educationRepository.existsById(id)) { + 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; } } diff --git a/version.txt b/version.txt index 17e51c3..d917d3e 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.1.1 +0.1.2