From f41fe7fc70a3b2902990d43a4ce7cbc8e92e25a9 Mon Sep 17 00:00:00 2001
From: Antoine Hazebrouck <antoine.hazebrouck.etu@univ-lille.fr>
Date: Mon, 6 Feb 2023 14:37:43 +0100
Subject: [PATCH] dao jpa, juste register la
---
pom.xml | 4 +
.../controllers/MainController.java | 10 +--
.../controllers/RegistrationController.java | 6 +-
.../jez/authentication2/entities/User.java | 49 +++++-----
.../repositories/UserRepository.java | 8 ++
.../authentication2/services/UserService.java | 90 +++++++++----------
.../services/UserService2.java | 21 +++++
src/main/resources/application.properties | 4 +-
src/main/resources/data.sql | 10 +--
src/main/resources/schema.sql | 7 +-
10 files changed, 122 insertions(+), 87 deletions(-)
create mode 100644 src/main/java/jez/authentication2/repositories/UserRepository.java
create mode 100644 src/main/java/jez/authentication2/services/UserService2.java
diff --git a/pom.xml b/pom.xml
index a7a2240..16624f3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -57,6 +57,10 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/src/main/java/jez/authentication2/controllers/MainController.java b/src/main/java/jez/authentication2/controllers/MainController.java
index 04ef824..68d70f6 100644
--- a/src/main/java/jez/authentication2/controllers/MainController.java
+++ b/src/main/java/jez/authentication2/controllers/MainController.java
@@ -11,12 +11,12 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import jez.authentication2.services.NotesService;
-import jez.authentication2.services.UserService;
+import jez.authentication2.services.UserService2;
@Controller
public class MainController {
@Autowired
- UserService userService;
+ UserService2 userService;
@Autowired
NotesService notesService;
@@ -39,9 +39,9 @@ public class MainController {
String username = authentication.getName();
String notes = notesService.getNotes(username, note_id);
- System.out.println(notes);
- System.out.println(note_id);
- System.out.println(userService.getUser(username));
+ // System.out.println(notes);
+ // System.out.println(note_id);
+ // System.out.println(userService.getUser(username));
model.addAttribute("username", username);
model.addAttribute("notes", notes);
diff --git a/src/main/java/jez/authentication2/controllers/RegistrationController.java b/src/main/java/jez/authentication2/controllers/RegistrationController.java
index 7a74ed9..0f37893 100644
--- a/src/main/java/jez/authentication2/controllers/RegistrationController.java
+++ b/src/main/java/jez/authentication2/controllers/RegistrationController.java
@@ -10,12 +10,12 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import jez.authentication2.entities.User;
import jez.authentication2.exceptions.DuplicateUsernameException;
-import jez.authentication2.services.UserService;
+import jez.authentication2.services.UserService2;
@Controller
public class RegistrationController {
@Autowired
- UserService userService;
+ UserService2 userService;
@GetMapping("/register")
public String displayRegistrationPage(Model model)
@@ -30,7 +30,7 @@ public class RegistrationController {
user.setEnabled(true);
try {
- userService.addUser(user);
+ userService.saveUser(user);
} catch (DuplicateUsernameException e) {
e.printStackTrace();
System.out.println("duplicate");
diff --git a/src/main/java/jez/authentication2/entities/User.java b/src/main/java/jez/authentication2/entities/User.java
index aa050ea..06e8c2c 100644
--- a/src/main/java/jez/authentication2/entities/User.java
+++ b/src/main/java/jez/authentication2/entities/User.java
@@ -1,58 +1,59 @@
package jez.authentication2.entities;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "users")
public class User {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Integer id;
private String username;
private String password;
private boolean enabled;
- private String text;
-
- public User(String username, String password, boolean enabled, String text) {
+
+ public User(Integer id, String username, String password, boolean enabled, String text) {
+ this.id = id;
this.username = username;
this.password = password;
this.enabled = enabled;
- this.text = text;
}
-
- public User() {
+ public User() {}
+ public Integer getId() {
+ return id;
+ }
+ public void setId(Integer id) {
+ this.id = id;
}
-
public String getUsername() {
return username;
}
-
public void setUsername(String username) {
this.username = username;
}
-
public String getPassword() {
return password;
}
-
public void setPassword(String password) {
this.password = password;
}
-
public boolean isEnabled() {
return enabled;
}
-
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
-
- public String getText() {
- return text;
- }
-
- public void setText(String text) {
- this.text = text;
- }
-
@Override
public String toString() {
- return "User [username=" + username + ", password=" + password + ", enabled=" + enabled
- + ", text=" + text + "]";
+ return "User [id=" + id + ", username=" + username + ", password=" + password + ", enabled="
+ + enabled + "]";
}
-
+
+
+
}
diff --git a/src/main/java/jez/authentication2/repositories/UserRepository.java b/src/main/java/jez/authentication2/repositories/UserRepository.java
new file mode 100644
index 0000000..c211a8f
--- /dev/null
+++ b/src/main/java/jez/authentication2/repositories/UserRepository.java
@@ -0,0 +1,8 @@
+package jez.authentication2.repositories;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import jez.authentication2.entities.User;
+
+public interface UserRepository extends JpaRepository<User, Integer> {
+
+}
diff --git a/src/main/java/jez/authentication2/services/UserService.java b/src/main/java/jez/authentication2/services/UserService.java
index 7d1db2d..61435ff 100644
--- a/src/main/java/jez/authentication2/services/UserService.java
+++ b/src/main/java/jez/authentication2/services/UserService.java
@@ -1,51 +1,51 @@
-package jez.authentication2.services;
+// package jez.authentication2.services;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import javax.sql.DataSource;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import jez.authentication2.entities.User;
-import jez.authentication2.exceptions.DuplicateUsernameException;
+// import java.sql.Connection;
+// import java.sql.ResultSet;
+// import java.sql.SQLException;
+// import java.sql.Statement;
+// import javax.sql.DataSource;
+// import org.springframework.beans.factory.annotation.Autowired;
+// import org.springframework.stereotype.Service;
+// import jez.authentication2.entities.User;
+// import jez.authentication2.exceptions.DuplicateUsernameException;
-@Service
-public class UserService {
- @Autowired
- DataSource dataSource;
+// @Service
+// public class UserService {
+// @Autowired
+// DataSource dataSource;
- public void addUser(User user) throws DuplicateUsernameException, SQLException {
- Connection conn = dataSource.getConnection();
- Statement statement = conn.createStatement();
+// public void addUser(User user) throws DuplicateUsernameException, SQLException {
+// Connection conn = dataSource.getConnection();
+// Statement statement = conn.createStatement();
- try {
- statement.executeUpdate("""
- INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s');
- """.formatted(user.getUsername(), user.getPassword(), user.isEnabled()));
- } catch (SQLException e) {
- e.printStackTrace();
- if (e.getErrorCode() == 0) {
- throw new DuplicateUsernameException();
- }
- }
+// try {
+// statement.executeUpdate("""
+// INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s');
+// """.formatted(user.getUsername(), user.getPassword(), user.isEnabled()));
+// } catch (SQLException e) {
+// e.printStackTrace();
+// if (e.getErrorCode() == 0) {
+// throw new DuplicateUsernameException();
+// }
+// }
- statement.executeUpdate("""
- INSERT INTO authorities (username, authority) VALUES ('%s', '%s');
- """.formatted(user.getUsername(), "ROLE_USER"));
- statement.executeUpdate("""
- INSERT INTO notes (username, notes)
- values ('%s', 'write notes here');
- """.formatted(user.getUsername()));
- }
+// statement.executeUpdate("""
+// INSERT INTO authorities (username, authority) VALUES ('%s', '%s');
+// """.formatted(user.getUsername(), "ROLE_USER"));
+// statement.executeUpdate("""
+// INSERT INTO notes (username, notes)
+// values ('%s', 'write notes here');
+// """.formatted(user.getUsername()));
+// }
- public User getUser(String username) throws SQLException {
- Connection conn = dataSource.getConnection();
- Statement statement = conn.createStatement();
- ResultSet result = statement
- .executeQuery("SELECT username, password, enabled, notes FROM users NATURAL JOIN notes WHERE username='%s';".formatted(username));
- result.next();
- return new User(result.getString(1), result.getString(2), result.getBoolean(3),
- result.getString(4));
- }
-}
+// public User getUser(String username) throws SQLException {
+// Connection conn = dataSource.getConnection();
+// Statement statement = conn.createStatement();
+// ResultSet result = statement
+// .executeQuery("SELECT username, password, enabled, notes FROM users NATURAL JOIN notes WHERE username='%s';".formatted(username));
+// result.next();
+// return new User(result.getString(1), result.getString(2), result.getBoolean(3),
+// result.getString(4));
+// }
+// }
diff --git a/src/main/java/jez/authentication2/services/UserService2.java b/src/main/java/jez/authentication2/services/UserService2.java
new file mode 100644
index 0000000..7b0c56b
--- /dev/null
+++ b/src/main/java/jez/authentication2/services/UserService2.java
@@ -0,0 +1,21 @@
+package jez.authentication2.services;
+
+import java.sql.SQLException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import jez.authentication2.entities.User;
+import jez.authentication2.exceptions.DuplicateUsernameException;
+import jez.authentication2.repositories.UserRepository;
+
+@Service
+public class UserService2 {
+ // @Autowired
+ // DataSource dataSource;
+ @Autowired
+ UserRepository userRepository;
+
+ public void saveUser(User user) throws DuplicateUsernameException, SQLException {
+ userRepository.save(user);
+ }
+
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 6978d38..6122db2 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1,6 +1,6 @@
-# spring.jpa.properspringties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
+spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# spring.jpa.hibernate.ddl-auto=none
-# spring.jpa.hibernate.show-sql=true
+spring.jpa.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres
diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql
index 869b83e..ec46de8 100644
--- a/src/main/resources/data.sql
+++ b/src/main/resources/data.sql
@@ -1,10 +1,10 @@
insert into users (username, password, enabled)
values ('user', 'password', true);
-insert into notes (username, notes)
-values ('user', 'some notes ...1');
-insert into notes (username, notes)
-values ('user', 'some notes ...2');
+insert into notes (user_id, notes)
+values (1, 'some notes ...1');
+insert into notes (user_id, notes)
+values (1, 'some notes ...2');
insert into users (username, password, enabled)
values ('admin', 'password', true);
@@ -13,4 +13,4 @@ insert into authorities (username, authority)
values ('user', 'ROLE_USER');
insert into authorities (username, authority)
-values ('admin', 'ROLE_ADMIN');
\ No newline at end of file
+values ('admin', 'ROLE_ADMIN');
diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql
index 4322dd8..cc02de5 100644
--- a/src/main/resources/schema.sql
+++ b/src/main/resources/schema.sql
@@ -1,6 +1,7 @@
drop table if exists users cascade;
create table users(
- username varchar(50) not null primary key,
+ id serial primary key,
+ username varchar(50) not null unique,
password varchar(50) not null,
enabled boolean not null
);
@@ -16,8 +17,8 @@ drop table if exists notes cascade;
create table notes (
id serial not null primary key,
notes text,
- username varchar(50),
- foreign key(username) references users(username)
+ user_id int not null,
+ foreign key(user_id) references users(id)
);
-- constraint fk_authorities_users
--
GitLab