Skip to content

Commit c4e8f7a

Browse files
author
Marcio Lamego
committed
TEstes unitarios e desafio da arquitetura
1 parent 24701f6 commit c4e8f7a

64 files changed

Lines changed: 17154 additions & 21933 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

pom.xml

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.1.5.RELEASE</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10-
</parent>
116
<groupId>teste.springboot</groupId>
127
<artifactId>springboot</artifactId>
138
<version>0.0.1-SNAPSHOT</version>
@@ -16,11 +11,10 @@
1611

1712
<properties>
1813
<java.version>1.8</java.version>
19-
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
2014
</properties>
2115

2216
<dependencies>
23-
17+
2418
<dependency>
2519
<groupId>org.springframework.boot</groupId>
2620
<artifactId>spring-boot-starter-jdbc</artifactId>
@@ -32,48 +26,78 @@
3226
<version>2.1.5.RELEASE</version>
3327
</dependency>
3428
<dependency>
35-
<groupId>org.hsqldb</groupId>
36-
<artifactId>hsqldb</artifactId>
37-
<version>2.4.0</version>
38-
<scope>runtime</scope>
29+
<groupId>org.hsqldb</groupId>
30+
<artifactId>hsqldb</artifactId>
31+
<version>2.4.0</version>
32+
<scope>runtime</scope>
3933
</dependency>
40-
4134
<dependency>
42-
<groupId>org.springframework.cloud</groupId>
43-
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
<version>2.1.5.RELEASE</version>
4438
</dependency>
45-
4639
<dependency>
4740
<groupId>org.springframework.boot</groupId>
48-
<artifactId>spring-boot-starter-test</artifactId>
49-
<scope>test</scope>
41+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
42+
<version>2.1.5.RELEASE</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-devtools</artifactId>
47+
<version>2.1.5.RELEASE</version>
48+
<optional>true</optional>
49+
</dependency>
50+
<dependency>
51+
<groupId>javax.servlet</groupId>
52+
<artifactId>servlet-api</artifactId>
53+
<version>2.5</version>
54+
<scope>provided</scope>
5055
</dependency>
51-
5256
<dependency>
53-
<groupId>org.springframework.cloud</groupId>
54-
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-data-rest</artifactId>
59+
<version>2.1.5.RELEASE</version>
5560
</dependency>
61+
<dependency>
62+
<groupId>org.json</groupId>
63+
<artifactId>json</artifactId>
64+
<version>20180130</version>
65+
</dependency>
66+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-test</artifactId>
70+
<version>2.1.5.RELEASE</version>
71+
<scope>test</scope>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>junit</groupId>
76+
<artifactId>junit</artifactId>
77+
<version>4.11</version>
78+
<scope>test</scope>
79+
</dependency>
5680
</dependencies>
57-
58-
<dependencyManagement>
59-
<dependencies>
60-
<dependency>
61-
<groupId>org.springframework.cloud</groupId>
62-
<artifactId>spring-cloud-dependencies</artifactId>
63-
<version>${spring-cloud.version}</version>
64-
<type>pom</type>
65-
<scope>import</scope>
66-
</dependency>
67-
</dependencies>
68-
</dependencyManagement>
69-
7081
<build>
7182
<plugins>
83+
7284
<plugin>
7385
<groupId>org.springframework.boot</groupId>
7486
<artifactId>spring-boot-maven-plugin</artifactId>
7587
</plugin>
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-compiler-plugin</artifactId>
91+
<version>3.1</version>
92+
<configuration>
93+
<source>1.8</source>
94+
<target>1.8</target>
95+
</configuration>
96+
</plugin>
7697
</plugins>
7798
</build>
7899

100+
101+
102+
79103
</project>

src/main/java/teste/springboot/controller/CategoriaRestController.java

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
import java.util.HashMap;
44
import java.util.List;
55
import java.util.Map;
6+
import java.util.Optional;
67

8+
import org.json.JSONException;
9+
import org.json.JSONObject;
710
import org.springframework.beans.factory.annotation.Autowired;
811
import org.springframework.web.bind.annotation.CrossOrigin;
912
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RequestMethod;
16+
import org.springframework.web.bind.annotation.RequestParam;
1017
import org.springframework.web.bind.annotation.RestController;
1118

1219
import teste.springboot.model.Categoria;
@@ -20,19 +27,111 @@ public class CategoriaRestController {
2027

2128
@CrossOrigin(origins = "http://localhost:8080")
2229
@GetMapping("/api/categorias")
23-
Map<String, Long> all() {
30+
Map<Long, String> all() {
2431
List<Categoria> categorias = (List<Categoria>) categoriaRepository.findAll();
2532

26-
Map<String, Long> formatted = new HashMap<String, Long>();
33+
Map<Long, String> formatted = new HashMap<Long, String>();
2734

2835
for(Categoria categoria : categorias) {
2936

30-
formatted.put(categoria.getNome(), null);
37+
formatted.put(categoria.getId(), categoria.getNome());
3138
}
3239

3340
return formatted;
3441

3542

3643
}
3744

45+
@RequestMapping(value = "/api/categorias/all", method = { RequestMethod.GET, RequestMethod.POST })
46+
List<Categoria> getAll() {
47+
List<Categoria> categorias = (List<Categoria>) categoriaRepository.findAll();
48+
49+
return categorias;
50+
}
51+
52+
@RequestMapping(value = "/api/categorias/delete", method = RequestMethod.POST, consumes = {"multipart/form-data"}, produces = "application/json")
53+
public String delete(@RequestParam String id) {
54+
55+
categoriaRepository.deleteById(Long.valueOf(id));
56+
57+
58+
return "{\"message\":\"Excluido com sucesso\"}";
59+
}
60+
61+
@RequestMapping(value = "/api/categorias/id", method = RequestMethod.POST, consumes = {"multipart/form-data"})
62+
public Optional<Categoria> findById(@RequestParam String categoriaId) {
63+
64+
Optional<Categoria> categoria = categoriaRepository.findById(Long.valueOf(categoriaId));
65+
66+
return categoria;
67+
}
68+
69+
@RequestMapping(value = "/api/categorias/insert", method = RequestMethod.POST)
70+
public String insert(@RequestBody String nome) {
71+
72+
73+
JSONObject jsonObject = null;
74+
try {
75+
jsonObject = new JSONObject(nome);
76+
}catch (JSONException err){
77+
System.out.println( err.toString());
78+
}
79+
80+
Categoria categoria = new Categoria();
81+
try {
82+
categoria.setNome((String) jsonObject.get("nome"));
83+
} catch (JSONException e) {
84+
// TODO Auto-generated catch block
85+
e.printStackTrace();
86+
}
87+
88+
categoriaRepository.save(categoria);
89+
90+
91+
92+
return "\"success\"";
93+
}
94+
95+
@RequestMapping(value = "/api/categorias/edit", method = RequestMethod.POST)
96+
public String edit(@RequestBody String post) {
97+
98+
try {
99+
100+
JSONObject jsonObject = new JSONObject(post);
101+
102+
103+
Optional<Categoria> categoria = null;
104+
105+
categoria = categoriaRepository.findById(Long.valueOf((Integer) jsonObject.get("id")));
106+
107+
108+
109+
categoria.ifPresent(cat -> {
110+
111+
try {
112+
cat.setNome((String) jsonObject.get("nome"));
113+
} catch (JSONException e) {
114+
// TODO Auto-generated catch block
115+
e.printStackTrace();
116+
}
117+
118+
119+
categoriaRepository.save(cat);
120+
121+
});
122+
} catch (NumberFormatException | JSONException e1) {
123+
// TODO Auto-generated catch block
124+
e1.printStackTrace();
125+
}
126+
127+
128+
129+
130+
131+
132+
133+
134+
return "\"success\"";
135+
}
136+
38137
}

0 commit comments

Comments
 (0)