Class JobController
This controller handles HTTP requests related to job listings, including creating, retrieving, updating, and deleting job postings. It provides endpoints for both public access (browsing and searching jobs) and restricted access for recruiters (posting and managing jobs). The controller supports pagination, sorting, and filtering of job listings to enhance the job search experience.
- Since:
- 2025-05-14
-
Constructor Summary
ConstructorsConstructorDescriptionJobController(JobService jobService) Constructs a JobController with the specified service. -
Method Summary
Modifier and TypeMethodDescriptionCreates a new job posting.Deletes a job posting.getAllJobs(int page, int size, String sortBy, String sortDir) Retrieves a paginated list of all job postings.getJobById(String id) Retrieves a specific job posting by its ID.getJobsByCategory(String category) Retrieves job postings by category.getJobsByLocation(String location) Retrieves job postings by location.getJobsByRecruiter(String recruiterId) Retrieves job postings created by a specific recruiter.searchJobs(String keyword, int page, int size) Searches for job postings based on a keyword.Toggles the active status of a job posting.Updates an existing job posting.
-
Constructor Details
-
JobController
Constructs a JobController with the specified service.- Parameters:
jobService- The service for job-related operations
-
-
Method Details
-
getAllJobs
@GetMapping public ResponseEntity<Map<String,Object>> getAllJobs(@RequestParam(defaultValue="0") int page, @RequestParam(defaultValue="10") int size, @RequestParam(defaultValue="postedDate") String sortBy, @RequestParam(defaultValue="desc") String sortDir) Retrieves a paginated list of all job postings.This endpoint is publicly accessible and supports pagination and sorting to optimize the browsing experience. By default, jobs are sorted by posted date in descending order (newest first).
- Parameters:
page- The page number (zero-based)size- The size of each pagesortBy- The field to sort bysortDir- The sort direction (asc or desc)- Returns:
- ResponseEntity with paginated job listings and metadata
-
getJobById
Retrieves a specific job posting by its ID.This endpoint is publicly accessible and allows viewing the details of a particular job posting.
- Parameters:
id- The ID of the job posting to retrieve- Returns:
- ResponseEntity with the job details or a not-found status
-
createJob
@PostMapping @PreAuthorize("hasRole(\'RECRUITER\')") public ResponseEntity<?> createJob(@Valid @RequestBody @Valid JobDto jobDto) Creates a new job posting.This endpoint allows recruiters to post new job listings. It requires RECRUITER role authorization and validates the job details before creation.
- Parameters:
jobDto- The DTO containing job details- Returns:
- ResponseEntity with the created job posting
-
updateJob
@PutMapping("/{id}") @PreAuthorize("hasRole(\'RECRUITER\')") public ResponseEntity<?> updateJob(@PathVariable String id, @Valid @RequestBody @Valid JobDto jobDto) Updates an existing job posting.This endpoint allows recruiters to modify the details of a job posting they have created. It requires RECRUITER role authorization and validates the updated job details.
- Parameters:
id- The ID of the job posting to updatejobDto- The DTO containing updated job details- Returns:
- ResponseEntity with the updated job posting or a not-found status
-
deleteJob
@DeleteMapping("/{id}") @PreAuthorize("hasRole(\'RECRUITER\')") public ResponseEntity<?> deleteJob(@PathVariable String id) Deletes a job posting.This endpoint allows recruiters to remove job postings they have created. It requires RECRUITER role authorization.
- Parameters:
id- The ID of the job posting to delete- Returns:
- ResponseEntity with a success response or a not-found status
-
searchJobs
@GetMapping("/search") public ResponseEntity<Map<String,Object>> searchJobs(@RequestParam String keyword, @RequestParam(defaultValue="0") int page, @RequestParam(defaultValue="10") int size) Searches for job postings based on a keyword.This endpoint is publicly accessible and allows users to search for jobs by matching the keyword against job titles, descriptions, and company names. It supports pagination of search results.
- Parameters:
keyword- The search keywordpage- The page number (zero-based)size- The size of each page- Returns:
- ResponseEntity with paginated search results and metadata
-
getJobsByCategory
@GetMapping("/category/{category}") public ResponseEntity<?> getJobsByCategory(@PathVariable String category) Retrieves job postings by category.This endpoint is publicly accessible and allows filtering jobs by their category (e.g., IT, Marketing, Finance).
- Parameters:
category- The job category to filter by- Returns:
- ResponseEntity with a list of matching job postings
-
getJobsByLocation
@GetMapping("/location/{location}") public ResponseEntity<?> getJobsByLocation(@PathVariable String location) Retrieves job postings by location.This endpoint is publicly accessible and allows filtering jobs by their geographic location.
- Parameters:
location- The location to filter by- Returns:
- ResponseEntity with a list of matching job postings
-
getJobsByRecruiter
@GetMapping("/recruiter/{recruiterId}") @PreAuthorize("hasRole(\'RECRUITER\')") public ResponseEntity<?> getJobsByRecruiter(@PathVariable String recruiterId) Retrieves job postings created by a specific recruiter.This endpoint allows recruiters to view all the job postings they have created. It requires RECRUITER role authorization.
- Parameters:
recruiterId- The ID of the recruiter- Returns:
- ResponseEntity with a list of job postings by the recruiter
-
toggleJobActive
@PutMapping("/{id}/toggleActive") @PreAuthorize("hasRole(\'RECRUITER\')") public ResponseEntity<?> toggleJobActive(@PathVariable String id) Toggles the active status of a job posting.This endpoint allows recruiters to activate or deactivate a job posting without deleting it. Inactive jobs are not displayed in public searches. It requires RECRUITER role authorization.
- Parameters:
id- The ID of the job posting to toggle- Returns:
- ResponseEntity with the updated job posting or a not-found status
-