Skip to content

Commit

Permalink
rest exception handlings
Browse files Browse the repository at this point in the history
  • Loading branch information
cankurttekin committed Nov 15, 2024
1 parent c8f89fa commit c19427d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kurttekin.can.job_track.domain.exception.JobApplicationNotFoundException;
import com.kurttekin.can.job_track.domain.exception.UnauthorizedAccessException;
import com.kurttekin.can.job_track.domain.exception.UserNotFoundException;
import com.kurttekin.can.job_track.domain.model.jobapplication.JobApplication;
import com.kurttekin.can.job_track.domain.model.user.User;
import com.kurttekin.can.job_track.domain.service.JobApplicationService;
Expand Down Expand Up @@ -54,11 +55,11 @@ public Optional<JobApplication> findById(Long id) {
public JobApplication updateJobApplication(Long id, JobApplication updatedJobApplication, String username) {
// First, check if the job application exists
JobApplication jobApplication = jobApplicationRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Job Application not found"));
.orElseThrow(() -> new JobApplicationNotFoundException("Job Application not found"));

// Then, check if the user exists
User user = userService.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
.orElseThrow(() -> new UserNotFoundException("User not found"));

// Ensure the job application belongs to the user
if (!jobApplication.getUser().getId().equals(user.getId())) {
Expand All @@ -81,7 +82,6 @@ public JobApplication updateJobApplication(Long id, JobApplication updatedJobApp
return jobApplicationRepository.save(jobApplication);
}


@Transactional
@Override
public void deleteJobApplication(Long id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.kurttekin.can.job_track.domain.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class JobApplicationNotFoundException extends RuntimeException {
public JobApplicationNotFoundException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.kurttekin.can.job_track.domain.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResumeNotFoundException extends RuntimeException {
public ResumeNotFoundException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.kurttekin.can.job_track.domain.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kurttekin.can.job_track.presentation.rest;

import com.kurttekin.can.job_track.domain.exception.JobApplicationNotFoundException;
import com.kurttekin.can.job_track.domain.exception.UserNotFoundException;
import com.kurttekin.can.job_track.domain.model.jobapplication.JobApplication;
import com.kurttekin.can.job_track.domain.model.user.User;
import com.kurttekin.can.job_track.domain.service.JobApplicationService;
Expand Down Expand Up @@ -36,7 +38,7 @@ public ResponseEntity<JobApplication> createJobApplication(

// Find the user in the database
User user = userService.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
.orElseThrow(() -> new UserNotFoundException("User not found"));

jobApplication.setUser(user); // Set the user on the job application
JobApplication createdJobApplication = jobApplicationService.createJobApplication(jobApplication);
Expand All @@ -47,7 +49,7 @@ public ResponseEntity<JobApplication> createJobApplication(
public ResponseEntity<List<JobApplication>> getJobApplications(Authentication authentication) {
String username = authentication.getName(); // Get the username from authentication
User user = userService.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
.orElseThrow(() -> new UserNotFoundException("User not found"));

List<JobApplication> jobApplications = jobApplicationService.findAllByUserId(user.getId());
return ResponseEntity.ok(jobApplications);
Expand All @@ -67,7 +69,6 @@ public ResponseEntity<JobApplication> updateJobApplication(
return ResponseEntity.ok(updatedJob);
}


@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteJobApplication(
@PathVariable Long id,
Expand All @@ -76,10 +77,10 @@ public ResponseEntity<Void> deleteJobApplication(

// Find the user in the database
User user = userService.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
.orElseThrow(() -> new UserNotFoundException("User not found"));

JobApplication jobApplication = jobApplicationService.findById(id)
.orElseThrow(() -> new RuntimeException("Job Application not found"));
.orElseThrow(() -> new JobApplicationNotFoundException("Job Application not found"));

// Ensure the job application belongs to the user
if (!jobApplication.getUser().getId().equals(user.getId())) {
Expand All @@ -97,7 +98,7 @@ public ResponseEntity<Void> deleteAllJobApplications(@AuthenticationPrincipal Us

// Find the user in the database
User user = userService.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
.orElseThrow(() -> new UserNotFoundException("User not found"));

jobApplicationService.deleteAllByUserId(user.getId());
return ResponseEntity.noContent().build();
Expand All @@ -109,7 +110,7 @@ public ResponseEntity<Map<String, Object>> getJobApplicationStats(@Authenticatio

// Find the user in the database
User user = userService.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
.orElseThrow(() -> new UserNotFoundException("User not found"));

// Get all job applications for the user
List<JobApplication> jobApplications = jobApplicationService.findAllByUserId(user.getId());
Expand All @@ -129,5 +130,4 @@ public ResponseEntity<Map<String, Object>> getJobApplicationStats(@Authenticatio

return ResponseEntity.ok(stats);
}

}

0 comments on commit c19427d

Please sign in to comment.