Skip to content

Commit

Permalink
Conectando ao MongoDB com repository e service
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmaugusto committed May 30, 2020
1 parent bf1dd84 commit d2375a1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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> {


}
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);
}
Expand Down
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();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@

spring.data.mongodb.uri=mongodb://localhost:27017/workshop_mongo

0 comments on commit d2375a1

Please sign in to comment.