-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conectando ao MongoDB com repository e service
- Loading branch information
1 parent
bf1dd84
commit d2375a1
Showing
6 changed files
with
50 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/miguelaugusto/workshopmongo/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<User, String> { | ||
|
||
|
||
} |
13 changes: 6 additions & 7 deletions
13
src/main/java/com/miguelaugusto/workshopmongo/resources/UserResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<User>> findAll(){ | ||
|
||
User maria = new User ("1", "Maria Brown", "[email protected]"); | ||
User alex = new User ("2", "Alex Green", "[email protected]"); | ||
|
||
List<User> list = new ArrayList<>(); | ||
list.addAll(Arrays.asList(maria, alex)); | ||
List<User> list = service.findAll(); | ||
|
||
return ResponseEntity.ok().body(list); | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/main/java/com/miguelaugusto/workshopmongo/services/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<User> findAll(){ | ||
return repo.findAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
spring.data.mongodb.uri=mongodb://localhost:27017/workshop_mongo |