Skip to content

Commit be9f337

Browse files
Merge pull request #68 from OpenEnade/ano-private-controller
Add Ano private controller
2 parents 0c1e8e7 + b961d96 commit be9f337

11 files changed

Lines changed: 60 additions & 86 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<artifactId>h2</artifactId>
6161
<scope>runtime</scope>
6262
</dependency>
63+
6364
</dependencies>
6465

6566
<build>

src/main/java/br/com/openenade/api/ano/Ano.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ public class Ano {
99
@Id
1010
private Integer ano;
1111

12+
public Ano() {
13+
14+
}
15+
16+
public Ano(Integer ano) {
17+
this.ano = ano;
18+
}
1219

1320
public Integer getAno() {
1421
return this.ano;
@@ -18,13 +25,11 @@ public void setAno(Integer ano) {
1825
this.ano = ano;
1926
}
2027

21-
2228
@Override
2329
public String toString() {
2430
return "" + this.ano;
2531
}
2632

27-
2833
@Override
2934
public int hashCode() {
3035
final int prime = 31;

src/main/java/br/com/openenade/api/ano/AnoService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public Collection<Ano> getAllAnos() {
1919
}
2020

2121
public Ano getAno(Integer ano) {
22-
Optional<Ano> anoo = this.repository.findById(ano);
23-
if (anoo.isPresent()) {
24-
return anoo.get();
22+
Optional<Ano> optAno = this.repository.findById(ano);
23+
if (optAno.isPresent()) {
24+
return optAno.get();
2525
} else {
2626
throw new ResourceNotFound("" + ano);
2727
}

src/main/java/br/com/openenade/api/ano/AnoUtils.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package br.com.openenade.api.ano;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.DeleteMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping(path = PublicAnoController.ENDPOINT)
13+
public class PrivateAnoController {
14+
15+
@Autowired
16+
private AnoService service;
17+
18+
@PostMapping
19+
public Ano createAno(@RequestBody Ano ano) {
20+
return this.service.addAno(ano);
21+
}
22+
23+
@DeleteMapping(path = "/{" + PublicAnoController.ANO_ID + "}")
24+
public void deleteAno(@PathVariable(name = PublicAnoController.ANO_ID) Integer ano) {
25+
this.service.deleteAno(ano);
26+
}
27+
}

src/main/java/br/com/openenade/api/ano/AnoController.java renamed to src/main/java/br/com/openenade/api/ano/PublicAnoController.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,23 @@
22

33
import java.util.Collection;
44
import org.springframework.beans.factory.annotation.Autowired;
5-
import org.springframework.http.HttpStatus;
6-
import org.springframework.http.ResponseEntity;
75
import org.springframework.web.bind.annotation.CrossOrigin;
8-
import org.springframework.web.bind.annotation.DeleteMapping;
96
import org.springframework.web.bind.annotation.GetMapping;
107
import org.springframework.web.bind.annotation.PathVariable;
11-
import org.springframework.web.bind.annotation.PostMapping;
12-
import org.springframework.web.bind.annotation.PutMapping;
13-
import org.springframework.web.bind.annotation.RequestBody;
148
import org.springframework.web.bind.annotation.RequestMapping;
159
import org.springframework.web.bind.annotation.RestController;
1610

1711
@RestController
1812
@CrossOrigin(origins = "*")
19-
@RequestMapping(path = AnoController.ENDPOINT)
20-
public class AnoController {
13+
@RequestMapping(path = PublicAnoController.ENDPOINT)
14+
public class PublicAnoController {
2115

2216
public static final String ENDPOINT = "/anos";
23-
2417
public static final String ANO_ID = "ano";
2518

2619
@Autowired
2720
private AnoService service;
2821

29-
3022
@GetMapping
3123
public Collection<Ano> getAnos() {
3224
return service.getAllAnos();
@@ -37,19 +29,4 @@ public Ano getAno(@PathVariable(name = ANO_ID) Integer ano) {
3729
return service.getAno(ano);
3830
}
3931

40-
@PostMapping
41-
public Ano saveAno(@RequestBody Ano ano) {
42-
return this.service.addAno(ano);
43-
}
44-
45-
@PutMapping
46-
public Ano updateAno(@RequestBody Ano ano) {
47-
return this.service.updateAno(ano);
48-
}
49-
50-
@DeleteMapping(path = "/{" + ANO_ID + "}")
51-
public ResponseEntity<String> updateAno(@PathVariable(name = ANO_ID) Integer ano) {
52-
this.service.deleteAno(ano);
53-
return AnoUtils.getResponseEntity(HttpStatus.OK, "");
54-
}
5532
}

src/main/java/br/com/openenade/api/stub_data/StubDataCreator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ private Ano[] addAnos() {
128128

129129
Ano[] anos = new Ano[anosInt.length];
130130
for (int i = 0; i < anos.length; i++) {
131-
anos[i] = new Ano();
132-
anos[i].setAno(anosInt[i]);
131+
anos[i] = new Ano(anosInt[i]);
133132
this.anoRepository.save(anos[i]);
134133
}
135134

src/test/java/br/com/openenade/api/BaseUnitTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
public class BaseUnitTest {
2323

24+
public static final String BASE_URI = "/api";
25+
2426
@Autowired
2527
private AnoService anoService;
2628

src/test/java/br/com/openenade/api/ano/AnoUnityTests.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public class AnoUnityTests extends BaseUnitTest {
2222

2323
@Test
2424
public void addAnoTest() {
25-
Ano ano = new Ano();
26-
27-
ano.setAno(2016);
25+
Ano ano = new Ano(2016);
2826

2927
this.service.addAno(ano);
3028
assertEquals(ano, service.getAno(2016));
@@ -33,11 +31,8 @@ public void addAnoTest() {
3331

3432
@Test
3533
public void addAnosTests() {
36-
Ano ano1 = new Ano();
37-
Ano ano2 = new Ano();
38-
39-
ano1.setAno(2016);
40-
ano2.setAno(2017);
34+
Ano ano1 = new Ano(2016);
35+
Ano ano2 = new Ano(2017);
4136

4237
List<Ano> list = new ArrayList<>();
4338

@@ -52,9 +47,7 @@ public void addAnosTests() {
5247

5348
@Test(expected = ResourceNotFound.class)
5449
public void deleteAnoTest() {
55-
Ano ano1 = new Ano();
56-
57-
ano1.setAno(2016);
50+
Ano ano1 = new Ano(2016);
5851

5952
this.service.addAno(ano1);
6053

src/test/java/br/com/openenade/api/nota/NotaControllerTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public class NotaControllerTests extends BaseUnitTest {
4747

4848
@Test
4949
public void postTestBasic() throws Exception {
50-
Ano ano = new Ano();
51-
ano.setAno(2018);
50+
Ano ano = new Ano(2018);
5251
Regiao regiao = new Regiao("NO");
5352
Estado estado = new Estado("XD", regiao);
5453
Municipio municipio = new Municipio(123L, estado, "Capoeira Grande");
@@ -79,8 +78,7 @@ public void postTestBasic() throws Exception {
7978

8079
@Test
8180
public void getTestBasicPlusDelete() throws Exception {
82-
Ano ano = new Ano();
83-
ano.setAno(2018);
81+
Ano ano = new Ano(2018);
8482
Regiao regiao = new Regiao("NE");
8583
Estado estado = new Estado("GO", regiao);
8684
Municipio municipio = new Municipio(123L, estado, "Poeira Grande");

0 commit comments

Comments
 (0)