Skip to content

Commit a7510f5

Browse files
committed
add: new countrys-route which is returning a List of all countrys of db
1 parent e17c7c4 commit a7510f5

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/main/java/dev/urner/volodb/dao/CountryDAO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.urner.volodb.dao;
22

3+
import java.util.List;
4+
35
import org.springframework.data.repository.ListCrudRepository;
46
import org.springframework.stereotype.Repository;
57

@@ -8,6 +10,8 @@
810
@Repository
911
public interface CountryDAO extends ListCrudRepository<Country, Integer> {
1012

13+
public List<Country> findAll();
14+
1115
public Country findByLocalName(String localName);
1216

1317
public Country findByName(String name);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dev.urner.volodb.rest;
2+
3+
import java.util.List;
4+
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import dev.urner.volodb.entity.Country;
8+
import dev.urner.volodb.exception.CountryNotFoundException;
9+
import dev.urner.volodb.service.CountryService;
10+
import lombok.RequiredArgsConstructor;
11+
12+
import org.springframework.http.HttpStatus;
13+
import org.springframework.http.ResponseEntity;
14+
import org.springframework.web.bind.annotation.ExceptionHandler;
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
18+
@RestController
19+
@RequestMapping("/countrys")
20+
@RequiredArgsConstructor
21+
public class CountryRestController {
22+
23+
private final CountryService countryService;
24+
25+
@GetMapping()
26+
public List<Country> getCountrys() {
27+
return countryService.findAll();
28+
}
29+
30+
@ExceptionHandler
31+
public ResponseEntity<VolodbErrorResponse> handleException(CountryNotFoundException exc) {
32+
HttpStatus httpStatus = HttpStatus.NOT_FOUND;
33+
34+
VolodbErrorResponse error = new VolodbErrorResponse(
35+
httpStatus.value(),
36+
exc.getMessage(),
37+
System.currentTimeMillis());
38+
39+
return new ResponseEntity<>(error, httpStatus);
40+
}
41+
42+
}

src/main/java/dev/urner/volodb/service/CountryService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.urner.volodb.service;
22

3+
import java.util.List;
4+
35
import org.springframework.stereotype.Service;
46

57
import dev.urner.volodb.dao.CountryDAO;
@@ -13,6 +15,10 @@ public class CountryService {
1315

1416
private final CountryDAO countryDao;
1517

18+
public List<Country> findAll() {
19+
return countryDao.findAll();
20+
}
21+
1622
public Country findByName(String countryName) {
1723
return countryDao.findByLocalName(countryName);
1824
}

0 commit comments

Comments
 (0)