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

separation notes users pour avoir plusieurs page de notes

parent 853b6a20
No related branches found
No related tags found
No related merge requests found
......@@ -21,7 +21,7 @@ public class UserService {
try {
statement.executeUpdate("""
INSERT INTO users (username, password, enabled, notes) VALUES ('%s', '%s', '%s', 'write notes here');
INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s');
""".formatted(user.getUsername(), user.getPassword(), user.isEnabled()));
} catch (SQLException e) {
e.printStackTrace();
......@@ -30,18 +30,20 @@ public class UserService {
}
}
statement.executeUpdate(
"""
statement.executeUpdate("""
INSERT INTO authorities (username, authority) VALUES ('%s', '%s');
""".formatted(user.getUsername(), "ROLE_USER")
);
""".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 * FROM users WHERE username='%s';".formatted(username));
.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));
......@@ -51,14 +53,15 @@ public class UserService {
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
statement.executeUpdate("""
UPDATE users SET notes='%s' WHERE username='%s';
UPDATE notes SET notes='%s' WHERE username='%s';
""".formatted(notes, username));
}
public String getNotes(String username) throws SQLException {
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT notes FROM users WHERE username='%s';".formatted(username));
ResultSet result = statement
.executeQuery("SELECT notes FROM notes WHERE username='%s';".formatted(username));
result.next();
String notes = result.getString(1);
return notes;
......
......@@ -4,7 +4,7 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres
spring.datasource.password=12345678aA
spring.datasource.password=postgres
spring.sql.init.schema-locations=classpath:/schema.sql
......
insert into users (username, password, enabled, notes)
values ('user', 'password', true, 'some notes .....');
insert into users (username, password, enabled)
values ('user', 'password', true);
insert into notes (username, notes)
values ('user', 'some notes ...sqd');
insert into users (username, password, enabled)
values ('admin', 'password', true);
......
......@@ -2,8 +2,7 @@ drop table if exists users cascade;
create table users(
username varchar(50) not null primary key,
password varchar(50) not null,
enabled boolean not null,
notes text
enabled boolean not null
);
drop table if exists authorities cascade;
......@@ -12,5 +11,14 @@ create table authorities (
authority varchar(50) not null,
foreign key(username) references users(username)
);
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)
);
-- constraint fk_authorities_users
-- create unique index ix_auth_username on authorities (username,authority);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment