Skip to content

Commit 5b5c156

Browse files
committed
Endpoint para buscar estados e cidades
1 parent 6e7989f commit 5b5c156

File tree

8 files changed

+178
-1
lines changed

8 files changed

+178
-1
lines changed

src/main/java/jdc/loja/config/SecurityConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
4343

4444
private static final String[] PUBLIC_MATCHERS_GET = {
4545
"/produtos/**",
46-
"/categorias/**"
46+
"/categorias/**",
47+
"/estados/**"
4748
};
4849

4950
private static final String[] PUBLIC_MATCHERS_POST = {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package jdc.loja.dto;
2+
3+
import java.io.Serializable;
4+
5+
import jdc.loja.domain.Cidade;
6+
7+
public class CidadeDTO implements Serializable{
8+
9+
private static final long serialVersionUID = 1L;
10+
private Integer id;
11+
private String nome;
12+
13+
public Integer getId() {
14+
return id;
15+
}
16+
17+
public void setId(Integer id) {
18+
this.id = id;
19+
}
20+
21+
public String getNome() {
22+
return nome;
23+
}
24+
25+
public void setNome(String nome) {
26+
this.nome = nome;
27+
}
28+
29+
public CidadeDTO() {
30+
super();
31+
}
32+
33+
public CidadeDTO(Cidade obj) {
34+
id = obj.getId();
35+
nome = obj.getNome();
36+
}
37+
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package jdc.loja.dto;
2+
3+
import java.io.Serializable;
4+
5+
import jdc.loja.domain.Estado;
6+
7+
public class EstadoDTO implements Serializable{
8+
9+
private static final long serialVersionUID = 1L;
10+
private Integer id;
11+
private String nome;
12+
13+
public Integer getId() {
14+
return id;
15+
}
16+
17+
public void setId(Integer id) {
18+
this.id = id;
19+
}
20+
21+
public String getNome() {
22+
return nome;
23+
}
24+
25+
public void setNome(String nome) {
26+
this.nome = nome;
27+
}
28+
29+
public EstadoDTO() {
30+
super();
31+
}
32+
33+
public EstadoDTO(Estado obj) {
34+
id = obj.getId();
35+
nome = obj.getNome();
36+
}
37+
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package jdc.loja.repositories;
22

3+
import java.util.List;
4+
35
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.stereotype.Repository;
9+
import org.springframework.transaction.annotation.Transactional;
410

511
import jdc.loja.domain.Cidade;
612

13+
@Repository
714
public interface CidadeRepository extends JpaRepository<Cidade, Integer>{
815

16+
@Transactional(readOnly=true)
17+
@Query("select obj from Cidade obj where obj.estado.id = :estadoId order by obj.nome")
18+
List<Cidade> findCidades(@Param("estadoId") Integer estadoId);
919
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package jdc.loja.repositories;
22

3+
import java.util.List;
4+
35
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
import org.springframework.transaction.annotation.Transactional;
48

59
import jdc.loja.domain.Estado;
610

11+
@Repository
712
public interface EstadoRepository extends JpaRepository<Estado, Integer>{
813

14+
@Transactional(readOnly=true)
15+
List<Estado> findAllByOrderByNome();
916
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package jdc.loja.resources;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import jdc.loja.domain.Cidade;
14+
import jdc.loja.domain.Estado;
15+
import jdc.loja.dto.CidadeDTO;
16+
import jdc.loja.dto.EstadoDTO;
17+
import jdc.loja.services.CidadeService;
18+
import jdc.loja.services.EstadoService;
19+
20+
@RestController
21+
@RequestMapping(value="/estados")
22+
public class EstadoResource {
23+
24+
@Autowired
25+
private EstadoService service;
26+
27+
@Autowired
28+
private CidadeService cidService;
29+
30+
@RequestMapping(method=RequestMethod.GET)
31+
public ResponseEntity<List<EstadoDTO>> findAll() {
32+
List<Estado> list = service.findAll();
33+
List<EstadoDTO> listDto = list.stream().map(obj -> new EstadoDTO(obj)).collect(Collectors.toList());
34+
return ResponseEntity.ok().body(listDto);
35+
}
36+
37+
@RequestMapping(value="/{estadoId}/cidades", method=RequestMethod.GET)
38+
public ResponseEntity<List<CidadeDTO>> findCidades(@PathVariable Integer estadoId) {
39+
List<Cidade> list = cidService.findByEstado(estadoId);
40+
List<CidadeDTO> listDto = list.stream().map(obj -> new CidadeDTO(obj)).collect(Collectors.toList());
41+
return ResponseEntity.ok().body(listDto);
42+
}
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jdc.loja.services;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import jdc.loja.domain.Cidade;
9+
import jdc.loja.repositories.CidadeRepository;
10+
11+
@Service
12+
public class CidadeService {
13+
14+
@Autowired
15+
private CidadeRepository rep;
16+
17+
public List<Cidade> findByEstado(Integer estadoId) {
18+
return rep.findCidades(estadoId);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jdc.loja.services;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import jdc.loja.domain.Estado;
9+
import jdc.loja.repositories.EstadoRepository;
10+
11+
@Service
12+
public class EstadoService {
13+
14+
@Autowired
15+
private EstadoRepository rep;
16+
17+
public List<Estado> findAll() {
18+
return rep.findAllByOrderByNome();
19+
}
20+
}

0 commit comments

Comments
 (0)