Skip to content

Commit

Permalink
More error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ImLewel committed Dec 21, 2023
1 parent 96d6e2d commit a87239e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
7 changes: 7 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ paths:
type: "array"
items:
$ref: "#/components/schemas/Vacancy"
"400":
description: "Bad request"
content:
'application/text':
schema:
type: "string"
example: "Wrong search param"
"404":
description: "Not Found"
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import com.springcourse.findjob.models.Vacancy
import org.springframework.http.ResponseEntity

interface CompanyRestController {
fun getCompanyVacancies(company: String): ResponseEntity<List<Vacancy>>
fun getCompanyVacancies(company: String): ResponseEntity<Any>

fun getVacancyById(id: Int): ResponseEntity<Vacancy>
fun getVacancyById(id: Int): ResponseEntity<Any>

fun createVacancy(vacancy: Vacancy, company: String): ResponseEntity<Int>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ import org.springframework.web.bind.annotation.*
@RequestMapping("/api/company/{company}/vacancies")
class CompanyRestControllerImpl(@Autowired private val service: GeneralService) : CompanyRestController {
@GetMapping("/")
override fun getCompanyVacancies(@PathVariable("company") company: String): ResponseEntity<List<Vacancy>> = ResponseEntity.ok(service.getCompanyVacancies(company))
override fun getCompanyVacancies(@PathVariable("company") company: String): ResponseEntity<Any> {
val found = service.getCompanyVacancies(company)
return if (found.any()) ResponseEntity.ok(found) else ResponseEntity.notFound().build()
}

@GetMapping("/{id}")
override fun getVacancyById(@PathVariable("id") id: Int): ResponseEntity<Vacancy> = ResponseEntity.ok(service.getVacancyById(id))
override fun getVacancyById(@PathVariable("id") id: Int): ResponseEntity<Any> {
val found: Any;
try {
found = service.getVacancyById(id)
return ResponseEntity.ok(found)
} catch (e: Exception) {
return ResponseEntity.notFound().build()
}
}

@PostMapping("/create")
override fun createVacancy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface WorkerRestController {

fun getAllVacanciesByKeyword(keyword: String): ResponseEntity<List<Vacancy>>

fun getAllVacanciesByAge(age: Int): ResponseEntity<List<Vacancy>>
fun getAllVacanciesByAge(age: Int): ResponseEntity<Any>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ class WorkerRestControllerImpl(@Autowired private val service: GeneralService) :
override fun getAllVacanciesByKeyword(@RequestParam("keywords", required = false) keyword: String) = ResponseEntity.ok(service.getByKeyWordVacancy(keyword))

@GetMapping("/searchByAge")
override fun getAllVacanciesByAge(@RequestParam("age", required = false) age: Int) = ResponseEntity.ok(service.getVacanciesByAge(age))
override fun getAllVacanciesByAge(@RequestParam("age", required = false) age: Int): ResponseEntity<Any> {
val found = service.getVacanciesByAge(age)
return if (found.any()) ResponseEntity.ok(found) else ResponseEntity.badRequest().build()
}
}

0 comments on commit a87239e

Please sign in to comment.