Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lombok intg & docker-compose file up #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,19 @@ services:
working_dir: /opt/app
depends_on:
- mongo
networks:
- springappnetwork
deploy: # This will be considered only in docker swarm.
replicas: 2
update_config:
parallelism: 1
delay: 20s
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 5
# deploy: # This will be considered only in docker swarm.
# replicas: 2
# update_config:
# parallelism: 1
# delay: 20s
# restart_policy:
# condition: on-failure
# delay: 10s
# max_attempts: 5

mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=devdb
- MONGO_INITDB_ROOT_PASSWORD=devdb123
volumes:
- mongodb:/data/db
restart: always
networks:
- springappnetwork

volumes:
mongodb:
external: true

networks:
springappnetwork:
external: true
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
67 changes: 11 additions & 56 deletions src/main/java/com/mt/springmongo/User.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package com.mt.springmongo;

import java.io.Serializable;
import java.util.Objects;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;

/**
* Represents a User which is mapped to <code>users</code> collection in MongoDB
*
* @author Mithun
*/
@Data
@EqualsAndHashCode
@NoArgsConstructor
@Document(collection = "users")
public class User implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

@Id
private static final long serialVersionUID = 1L;

@Id
private String id;
private String firstName;
private String lastName;
Expand All @@ -31,51 +33,4 @@ public User(String firstName, String lastName, String email) {
this.email = email;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(id, user.id) &&
Objects.equals(firstName, user.firstName) &&
Objects.equals(lastName, user.lastName) &&
Objects.equals(email, user.email);
}

@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, email);
}
}
11 changes: 5 additions & 6 deletions src/main/java/com/mt/springmongo/UserController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.mt.springmongo;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -13,25 +13,24 @@
* @author Mithun
*/
@Controller
@RequiredArgsConstructor
public class UserController {

private final UserRepository userRepository;

private static final Logger logger = LoggerFactory.getLogger(UserController.class);

@Autowired
public UserController(final UserRepository userRepository) {
this.userRepository = userRepository;
}

@PostMapping(value = "/save")
public String save(@RequestParam("firstName") String firstName,
@RequestParam("lastName") String lastName,
@RequestParam("email") String email) {

logger.info("Creating user name: "+firstName);
logger.info("Creating user name: {}", firstName);
User user = new User(firstName, lastName, email);
userRepository.save(user);

return "redirect:/";
}

}
14 changes: 5 additions & 9 deletions src/main/java/com/mt/springmongo/UserResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.util.List;
import java.util.Optional;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,28 +17,24 @@
* @author Mithun
*/
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/api/users")
public class UserResource {

private final UserRepository userRepository;

private static final Logger logger = LoggerFactory.getLogger(UserRepository.class);

@Autowired
public UserResource(final UserRepository userRepository) {
this.userRepository = userRepository;
}
private static final Logger logger = LoggerFactory.getLogger(UserResource.class);

@GetMapping(produces = "application/JSON")
public List<User> getAllUsers() {
List<User> users = (List<User>) userRepository.findAll();
logger.info("Get Users Total Users: "+users.size());
logger.info("Get Users Total Users: {}", users.size());
return users;
}

@GetMapping(value = "/{id}", produces = "application/JSON")
public Optional<User> findById(@PathVariable("id") String userID) {
logger.info("Get User By Id : "+userID);
logger.info("Get User By Id : {}", userID);
return userRepository.findById(userID);
}
}
3 changes: 0 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ spring:
password: ${MONGO_DB_PASSWORD}
database: users
authentication-database: admin

server:
port: 8080
2 changes: 1 addition & 1 deletion src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ spring:
application:
name: spring-boot-app
server:
port: 8080
port: 8080