From d2375a1b132e3d0494e3ec67f8742d1e561be255 Mon Sep 17 00:00:00 2001 From: Miguel Augusto Date: Sat, 30 May 2020 15:51:21 +0100 Subject: [PATCH] Conectando ao MongoDB com repository e service --- pom.xml | 6 ++++++ .../workshopmongo/domain/User.java | 5 +++++ .../repository/UserRepository.java | 12 +++++++++++ .../workshopmongo/resources/UserResource.java | 13 ++++++------ .../workshopmongo/services/UserService.java | 20 +++++++++++++++++++ src/main/resources/application.properties | 2 +- 6 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/miguelaugusto/workshopmongo/repository/UserRepository.java create mode 100644 src/main/java/com/miguelaugusto/workshopmongo/services/UserService.java diff --git a/pom.xml b/pom.xml index b598203..1a17888 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,12 @@ spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-data-mongodb + + diff --git a/src/main/java/com/miguelaugusto/workshopmongo/domain/User.java b/src/main/java/com/miguelaugusto/workshopmongo/domain/User.java index 057aac1..f9aa6b5 100644 --- a/src/main/java/com/miguelaugusto/workshopmongo/domain/User.java +++ b/src/main/java/com/miguelaugusto/workshopmongo/domain/User.java @@ -2,9 +2,14 @@ import java.io.Serializable; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document(collection = "user") public class User implements Serializable { private static final long serialVersionUID = 1L; + @Id private String id; private String name; private String email; diff --git a/src/main/java/com/miguelaugusto/workshopmongo/repository/UserRepository.java b/src/main/java/com/miguelaugusto/workshopmongo/repository/UserRepository.java new file mode 100644 index 0000000..2f14553 --- /dev/null +++ b/src/main/java/com/miguelaugusto/workshopmongo/repository/UserRepository.java @@ -0,0 +1,12 @@ +package com.miguelaugusto.workshopmongo.repository; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +import com.miguelaugusto.workshopmongo.domain.User; + +@Repository +public interface UserRepository extends MongoRepository { + + +} diff --git a/src/main/java/com/miguelaugusto/workshopmongo/resources/UserResource.java b/src/main/java/com/miguelaugusto/workshopmongo/resources/UserResource.java index a1ccd47..f8bd4fa 100644 --- a/src/main/java/com/miguelaugusto/workshopmongo/resources/UserResource.java +++ b/src/main/java/com/miguelaugusto/workshopmongo/resources/UserResource.java @@ -1,28 +1,27 @@ package com.miguelaugusto.workshopmongo.resources; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.miguelaugusto.workshopmongo.domain.User; +import com.miguelaugusto.workshopmongo.services.UserService; @RestController @RequestMapping(value = "/users") public class UserResource { + @Autowired + private UserService service; + @RequestMapping(method=RequestMethod.GET) public ResponseEntity> findAll(){ - - User maria = new User ("1", "Maria Brown", "maria@gmail.com"); - User alex = new User ("2", "Alex Green", "alex@gmail.com"); - List list = new ArrayList<>(); - list.addAll(Arrays.asList(maria, alex)); + List list = service.findAll(); return ResponseEntity.ok().body(list); } diff --git a/src/main/java/com/miguelaugusto/workshopmongo/services/UserService.java b/src/main/java/com/miguelaugusto/workshopmongo/services/UserService.java new file mode 100644 index 0000000..a5309b6 --- /dev/null +++ b/src/main/java/com/miguelaugusto/workshopmongo/services/UserService.java @@ -0,0 +1,20 @@ +package com.miguelaugusto.workshopmongo.services; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.miguelaugusto.workshopmongo.domain.User; +import com.miguelaugusto.workshopmongo.repository.UserRepository; + +@Service +public class UserService { + + @Autowired + private UserRepository repo; + + public List findAll(){ + return repo.findAll(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..cc76242 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +spring.data.mongodb.uri=mongodb://localhost:27017/workshop_mongo \ No newline at end of file