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

Develop #2

Open
wants to merge 3 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
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<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
@@ -1,13 +1,35 @@
package fr.gouv.interieur.dso;

import fr.gouv.interieur.dso.models.Customer;
import fr.gouv.interieur.dso.models.Demo;
import fr.gouv.interieur.dso.repository.CustomerRepository;
import fr.gouv.interieur.dso.repository.DemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppJavaForgeDemoApplication {
public class AppJavaForgeDemoApplication implements CommandLineRunner {

@Autowired
private CustomerRepository customerRepository;

@Autowired
private DemoRepository demoRepository;

public static void main(String[] args) {
SpringApplication.run(AppJavaForgeDemoApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
customerRepository.deleteAll();
customerRepository.save(new Customer("Zakari", "Karahacane"));
Demo demo = new Demo();
demo.setName("Zakari ;)");
demoRepository.equals(demo);
System.out.println("created $$$$");

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fr.gouv.interieur.dso.controller;

import fr.gouv.interieur.dso.models.Customer;
import fr.gouv.interieur.dso.models.Demo;
import fr.gouv.interieur.dso.repository.CustomerRepository;
import fr.gouv.interieur.dso.service.jdbc.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -20,6 +22,9 @@ public class DemoController {
@Autowired
private DemoService demoService;

@Autowired
private CustomerRepository customerRepository;

@GetMapping("/")
public ResponseDto helloWorld() {
ResponseDto responseDto = new ResponseDto();
Expand All @@ -28,8 +33,13 @@ public ResponseDto helloWorld() {
return responseDto;
}

@GetMapping("/demo")
@GetMapping("/postgres")
public ResponseEntity<List<Demo>> getDemosList(){
return new ResponseEntity<>(demoService.getListDemo(), HttpStatus.OK);
}

@GetMapping("/mongo")
public ResponseEntity<List<Customer>> getCustomers() {
return new ResponseEntity<>(customerRepository.findAll(), HttpStatus.OK);
}
}
27 changes: 27 additions & 0 deletions src/main/java/fr/gouv/interieur/dso/models/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fr.gouv.interieur.dso.models;

import org.springframework.data.annotation.Id;

public class Customer {

@Id
public String id;

public String firstName;
public String lastName;

public Customer() {}

public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.gouv.interieur.dso.repository;

import fr.gouv.interieur.dso.models.Customer;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface CustomerRepository extends MongoRepository<Customer, String> {
public List<Customer> findAll();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
@Repository
public interface DemoRepository {
public List<Demo> getAll();

public Integer insert(Demo demo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import fr.gouv.interieur.dso.repository.DemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -35,4 +37,18 @@ public List<Demo> getAll() {
});
return demoList;
}

@Override
public Integer insert(Demo demo) {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
String sql = "INSERT INTO `demo` (`name`) VALUES (?);";
int rowsAffected = jdbcTemplate.update(conn -> {
PreparedStatement preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, demo.getName());
return preparedStatement;
}, generatedKeyHolder);
// Get auto-incremented ID
Integer id = generatedKeyHolder.getKey().intValue();
return id;
}
}
9 changes: 8 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ spring:
url: jdbc:postgresql://localhost:5432/demo_dso
username: demo_dso
password: demo_dso
data:
mongodb:
uri: mongodb://root:root@localhost:27018/demo?authSource=admin
database: demo
username: root
password: root
authentication-database: admin
liquibase:
change-log: classpath:db/master.xml
change-log: classpath:db/master.xml