Skip to content

Commit c968eb2

Browse files
committed
Initial commit-Project structure
1 parent fa72ea4 commit c968eb2

File tree

9 files changed

+311
-0
lines changed

9 files changed

+311
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*.zip
1919
*.tar.gz
2020
*.rar
21+
/target
22+
/.idea
23+
*.iml
2124

2225
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2326
hs_err_pid*

pom.xml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<!-- Autoconfiguration depends on Spring framework 5.2.x -->
9+
<version>2.2.4.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>net.java.forest</groupId>
13+
<artifactId>javaforest</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>javaforest-service</name>
16+
<description>Demo project for Spring Boot using R2DBC</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-webflux</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>io.projectreactor</groupId>
35+
<artifactId>reactor-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot.experimental</groupId>
41+
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>io.r2dbc</groupId>
46+
<artifactId>r2dbc-postgresql</artifactId>
47+
<scope>runtime</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.projectlombok</groupId>
52+
<artifactId>lombok</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
<dependencyManagement>
57+
<dependencies>
58+
<dependency>
59+
<groupId>org.springframework.boot.experimental</groupId>
60+
<artifactId>spring-boot-bom-r2dbc</artifactId>
61+
<version>0.1.0.M2</version>
62+
<type>pom</type>
63+
<scope>import</scope>
64+
</dependency>
65+
</dependencies>
66+
</dependencyManagement>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
<repositories>
78+
<repository>
79+
<id>spring-milestone</id>
80+
<url>https://repo.spring.io/milestone</url>
81+
</repository>
82+
</repositories>
83+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package net.java.forest;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.ApplicationRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.data.r2dbc.core.DatabaseClient;
9+
import reactor.core.publisher.Flux;
10+
import reactor.core.publisher.Mono;
11+
12+
import java.net.URISyntaxException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.util.stream.BaseStream;
17+
18+
@Slf4j
19+
@SpringBootApplication
20+
public class JavaforestService {
21+
22+
public static void main(String[] args) {
23+
SpringApplication.run(JavaforestService.class, args);
24+
}
25+
26+
@Bean
27+
public ApplicationRunner seeder(DatabaseClient client) {
28+
return args -> getSchema()
29+
.flatMap(sql -> executeSql(client, sql))
30+
.doOnSuccess(count -> log.info("Schema created, rows updated: {}", count))
31+
/*.then(repository.deleteAll())
32+
.doOnSuccess(v -> log.info("Repository cleared"))
33+
.thenMany(getPeople())
34+
.flatMap(repository::save)*/
35+
.subscribe(person -> log.info("Person saved: {}", person));
36+
}
37+
38+
39+
40+
private Mono<Integer> executeSql(DatabaseClient client, String sql) {
41+
return client.execute(sql).fetch().rowsUpdated();
42+
}
43+
44+
private Mono<String> getSchema() throws URISyntaxException {
45+
Path path = Paths.get(ClassLoader.getSystemResource("schema.sql").toURI());
46+
return Flux
47+
.using(() -> Files.lines(path), Flux::fromStream, BaseStream::close)
48+
.reduce((line1, line2) -> line1 + "\n" + line2);
49+
}
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.java.forest.controller;
2+
3+
import net.java.forest.model.Userdata;
4+
import net.java.forest.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import reactor.core.publisher.Flux;
10+
import reactor.core.publisher.Mono;
11+
12+
@RestController
13+
public class UserController {
14+
@Autowired
15+
private UserService userService;
16+
17+
@RequestMapping(value = "/alluser", method = RequestMethod.GET)
18+
public Flux<Userdata> getallUser(){
19+
return userService.getAllUser();
20+
}
21+
22+
@RequestMapping(value = "/adduser", method = RequestMethod.GET)
23+
public Mono<Userdata> addUser(){
24+
return userService.addUpdateUser(new Userdata(null, "Mahade","hassan", "12345", "mahade.hasan68@gmail.com","Bd, Khustia"));
25+
}
26+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package net.java.forest.model;
2+
3+
import org.hibernate.validator.constraints.UniqueElements;
4+
import org.springframework.data.annotation.Id;
5+
6+
import javax.validation.constraints.Email;
7+
import javax.validation.constraints.NotBlank;
8+
9+
public class Userdata {
10+
@Id
11+
private Long id;
12+
@NotBlank(message = "Firstname should not be empty")
13+
private String firstName;
14+
@NotBlank(message = "Lastname should not be empty")
15+
private String lastName;
16+
@NotBlank(message = "Password should not be empty")
17+
private String password;
18+
@Email(message = "Invalid email address")
19+
@UniqueElements(message = "Should be unique")
20+
private String email;
21+
private String address;
22+
23+
public Userdata(Long id, String firstName, String lastName, String password, String email, String address) {
24+
this.id = id;
25+
this.firstName = firstName;
26+
this.lastName = lastName;
27+
this.password = password;
28+
this.email = email;
29+
this.address = address;
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getFirstName() {
41+
return firstName;
42+
}
43+
44+
public void setFirstName(String firstName) {
45+
this.firstName = firstName;
46+
}
47+
48+
public String getLastName() {
49+
return lastName;
50+
}
51+
52+
public void setLastName(String lastName) {
53+
this.lastName = lastName;
54+
}
55+
56+
public String getPassword() {
57+
return password;
58+
}
59+
60+
public void setPassword(String password) {
61+
this.password = password;
62+
}
63+
64+
public String getEmail() {
65+
return email;
66+
}
67+
68+
public void setEmail(String email) {
69+
this.email = email;
70+
}
71+
72+
public String getAdddress() {
73+
return address;
74+
}
75+
76+
public void setAdddress(String adddress) {
77+
this.address = adddress;
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return "User{" +
83+
"id=" + id +
84+
", firstName='" + firstName + '\'' +
85+
", lastName='" + lastName + '\'' +
86+
", password='" + password + '\'' +
87+
", email='" + email + '\'' +
88+
", address='" + address + '\'' +
89+
'}';
90+
}
91+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.java.forest.repository;
2+
3+
import net.java.forest.model.Userdata;
4+
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface UserRepository extends ReactiveCrudRepository<Userdata, Long> {
9+
/*@Query("Select * from user where user.email=")
10+
Mono<User> findByEmail(String email);*/
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.java.forest.service;
2+
3+
import net.java.forest.model.Userdata;
4+
import net.java.forest.repository.UserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
@Service
11+
public class UserService {
12+
@Autowired
13+
private UserRepository userRepository;
14+
15+
public Mono<Userdata> addUpdateUser(Userdata user){
16+
return userRepository.save(user);
17+
}
18+
19+
public Flux<Userdata> getAllUser(){
20+
return userRepository.findAll();
21+
}
22+
23+
/*public Mono<User> getUserByEmail(String email){
24+
return userRepository.findByEmail(email);
25+
}*/
26+
27+
public Mono<Userdata> getUserById(long id){
28+
return userRepository.findById(id);
29+
}
30+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.r2dbc.url=r2dbc:postgresql://localhost/javaforest
2+
spring.r2dbc.username=postgres
3+
spring.r2dbc.password=12345

src/main/resources/schema.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE TABLE IF NOT EXISTS person (
2+
id SERIAL PRIMARY KEY,
3+
firstname VARCHAR(100),
4+
lastname VARCHAR(100)
5+
);
6+
CREATE TABLE IF NOT EXISTS userdata
7+
(
8+
id SERIAL PRIMARY KEY,
9+
first_name VARCHAR(50) NOT NULL,
10+
last_name VARCHAR(50) NOT NULL,
11+
password VARCHAR(500) NOT NULL,
12+
address VARCHAR(100) NOT NULL,
13+
email VARCHAR(50) UNIQUE NOT NULL
14+
);

0 commit comments

Comments
 (0)