Skip to content
Snippets Groups Projects
Commit f41fe7fc authored by Antoine Hazebrouck's avatar Antoine Hazebrouck
Browse files

dao jpa, juste register la

parent e2c8f29b
No related branches found
No related tags found
No related merge requests found
...@@ -57,6 +57,10 @@ ...@@ -57,6 +57,10 @@
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -11,12 +11,12 @@ import org.springframework.web.bind.annotation.PathVariable; ...@@ -11,12 +11,12 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import jez.authentication2.services.NotesService; import jez.authentication2.services.NotesService;
import jez.authentication2.services.UserService; import jez.authentication2.services.UserService2;
@Controller @Controller
public class MainController { public class MainController {
@Autowired @Autowired
UserService userService; UserService2 userService;
@Autowired @Autowired
NotesService notesService; NotesService notesService;
...@@ -39,9 +39,9 @@ public class MainController { ...@@ -39,9 +39,9 @@ public class MainController {
String username = authentication.getName(); String username = authentication.getName();
String notes = notesService.getNotes(username, note_id); String notes = notesService.getNotes(username, note_id);
System.out.println(notes); // System.out.println(notes);
System.out.println(note_id); // System.out.println(note_id);
System.out.println(userService.getUser(username)); // System.out.println(userService.getUser(username));
model.addAttribute("username", username); model.addAttribute("username", username);
model.addAttribute("notes", notes); model.addAttribute("notes", notes);
......
...@@ -10,12 +10,12 @@ import org.springframework.web.bind.annotation.PostMapping; ...@@ -10,12 +10,12 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import jez.authentication2.entities.User; import jez.authentication2.entities.User;
import jez.authentication2.exceptions.DuplicateUsernameException; import jez.authentication2.exceptions.DuplicateUsernameException;
import jez.authentication2.services.UserService; import jez.authentication2.services.UserService2;
@Controller @Controller
public class RegistrationController { public class RegistrationController {
@Autowired @Autowired
UserService userService; UserService2 userService;
@GetMapping("/register") @GetMapping("/register")
public String displayRegistrationPage(Model model) public String displayRegistrationPage(Model model)
...@@ -30,7 +30,7 @@ public class RegistrationController { ...@@ -30,7 +30,7 @@ public class RegistrationController {
user.setEnabled(true); user.setEnabled(true);
try { try {
userService.addUser(user); userService.saveUser(user);
} catch (DuplicateUsernameException e) { } catch (DuplicateUsernameException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("duplicate"); System.out.println("duplicate");
......
package jez.authentication2.entities; 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 { public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username; private String username;
private String password; private String password;
private boolean enabled; 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.username = username;
this.password = password; this.password = password;
this.enabled = enabled; 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() { public String getUsername() {
return username; return username;
} }
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.username = username;
} }
public String getPassword() { public String getPassword() {
return password; return password;
} }
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
this.enabled = enabled; this.enabled = enabled;
} }
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override @Override
public String toString() { public String toString() {
return "User [username=" + username + ", password=" + password + ", enabled=" + enabled return "User [id=" + id + ", username=" + username + ", password=" + password + ", enabled="
+ ", text=" + text + "]"; + enabled + "]";
} }
} }
package jez.authentication2.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import jez.authentication2.entities.User;
public interface UserRepository extends JpaRepository<User, Integer> {
}
package jez.authentication2.services; // package jez.authentication2.services;
import java.sql.Connection; // import java.sql.Connection;
import java.sql.ResultSet; // import java.sql.ResultSet;
import java.sql.SQLException; // import java.sql.SQLException;
import java.sql.Statement; // import java.sql.Statement;
import javax.sql.DataSource; // import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired; // import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; // import org.springframework.stereotype.Service;
import jez.authentication2.entities.User; // import jez.authentication2.entities.User;
import jez.authentication2.exceptions.DuplicateUsernameException; // import jez.authentication2.exceptions.DuplicateUsernameException;
@Service // @Service
public class UserService { // public class UserService {
@Autowired // @Autowired
DataSource dataSource; // DataSource dataSource;
public void addUser(User user) throws DuplicateUsernameException, SQLException { // public void addUser(User user) throws DuplicateUsernameException, SQLException {
Connection conn = dataSource.getConnection(); // Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement(); // Statement statement = conn.createStatement();
try { // try {
statement.executeUpdate(""" // statement.executeUpdate("""
INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s'); // INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s');
""".formatted(user.getUsername(), user.getPassword(), user.isEnabled())); // """.formatted(user.getUsername(), user.getPassword(), user.isEnabled()));
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
if (e.getErrorCode() == 0) { // if (e.getErrorCode() == 0) {
throw new DuplicateUsernameException(); // throw new DuplicateUsernameException();
} // }
} // }
statement.executeUpdate(""" // statement.executeUpdate("""
INSERT INTO authorities (username, authority) VALUES ('%s', '%s'); // INSERT INTO authorities (username, authority) VALUES ('%s', '%s');
""".formatted(user.getUsername(), "ROLE_USER")); // """.formatted(user.getUsername(), "ROLE_USER"));
statement.executeUpdate(""" // statement.executeUpdate("""
INSERT INTO notes (username, notes) // INSERT INTO notes (username, notes)
values ('%s', 'write notes here'); // values ('%s', 'write notes here');
""".formatted(user.getUsername())); // """.formatted(user.getUsername()));
} // }
public User getUser(String username) throws SQLException { // public User getUser(String username) throws SQLException {
Connection conn = dataSource.getConnection(); // Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement(); // Statement statement = conn.createStatement();
ResultSet result = statement // ResultSet result = statement
.executeQuery("SELECT username, password, enabled, notes FROM users NATURAL JOIN notes WHERE username='%s';".formatted(username)); // .executeQuery("SELECT username, password, enabled, notes FROM users NATURAL JOIN notes WHERE username='%s';".formatted(username));
result.next(); // result.next();
return new User(result.getString(1), result.getString(2), result.getBoolean(3), // return new User(result.getString(1), result.getString(2), result.getBoolean(3),
result.getString(4)); // result.getString(4));
} // }
} // }
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);
}
}
# 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.ddl-auto=none
# spring.jpa.hibernate.show-sql=true spring.jpa.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/spring spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres spring.datasource.username=postgres
......
insert into users (username, password, enabled) insert into users (username, password, enabled)
values ('user', 'password', true); values ('user', 'password', true);
insert into notes (username, notes) insert into notes (user_id, notes)
values ('user', 'some notes ...1'); values (1, 'some notes ...1');
insert into notes (username, notes) insert into notes (user_id, notes)
values ('user', 'some notes ...2'); values (1, 'some notes ...2');
insert into users (username, password, enabled) insert into users (username, password, enabled)
values ('admin', 'password', true); values ('admin', 'password', true);
......
drop table if exists users cascade; drop table if exists users cascade;
create table users( 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, password varchar(50) not null,
enabled boolean not null enabled boolean not null
); );
...@@ -16,8 +17,8 @@ drop table if exists notes cascade; ...@@ -16,8 +17,8 @@ drop table if exists notes cascade;
create table notes ( create table notes (
id serial not null primary key, id serial not null primary key,
notes text, notes text,
username varchar(50), user_id int not null,
foreign key(username) references users(username) foreign key(user_id) references users(id)
); );
-- constraint fk_authorities_users -- constraint fk_authorities_users
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment