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

app minimale fonctionne, gerer logout et login manuellement car bugs

parent ffb679bd
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,9 @@ public class MainController { ...@@ -31,6 +31,9 @@ public class MainController {
String notes = userService.getNotes(authentication.getName()); String notes = userService.getNotes(authentication.getName());
System.out.println(notes);
System.out.println(userService.getUser(authentication.getName()));
model.addAttribute("notes", notes); model.addAttribute("notes", notes);
return "main_page.html"; return "main_page.html";
......
...@@ -15,20 +15,26 @@ public class UserService { ...@@ -15,20 +15,26 @@ public class UserService {
@Autowired @Autowired
DataSource dataSource; DataSource dataSource;
public void addUser(User user) throws DuplicateUsernameException { public void addUser(User user) throws DuplicateUsernameException, SQLException {
try (Connection conn = dataSource.getConnection()) { Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
try {
statement.executeUpdate(""" statement.executeUpdate("""
INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s'); INSERT INTO users (username, password, enabled, notes) VALUES ('%s', '%s', '%s', 'write notes here');
""".formatted(user.getUsername(), user.getPassword(), user.isEnabled())); """.formatted(user.getUsername(), user.getPassword(), user.isEnabled()));
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
System.out.println(e.getErrorCode());
if (e.getErrorCode() == 0) { if (e.getErrorCode() == 0) {
throw new DuplicateUsernameException(); throw new DuplicateUsernameException();
} }
} }
statement.executeUpdate(
"""
INSERT INTO authorities (username, authority) VALUES ('%s', '%s');
""".formatted(user.getUsername(), "ROLE_USER")
);
} }
public User getUser(String username) throws SQLException { public User getUser(String username) throws SQLException {
...@@ -52,7 +58,7 @@ public class UserService { ...@@ -52,7 +58,7 @@ public class UserService {
public String getNotes(String username) throws SQLException { public String getNotes(String username) throws SQLException {
Connection conn = dataSource.getConnection(); Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT notes FROM users WHERE username='user';"); ResultSet result = statement.executeQuery("SELECT notes FROM users WHERE username='%s';".formatted(username));
result.next(); result.next();
String notes = result.getString(1); String notes = result.getString(1);
return notes; return notes;
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
<!-- afficher le logout en haut a droite --> <!-- afficher le logout en haut a droite -->
<form action="/register_notes" method="post"> <form action="/register_notes" method="post">
<textarea id="notes" name="notes" rows="..." <textarea id="notes" name="notes" rows="..."
cols="150" th:text="${notes.toString()}"> ... </textarea> cols="150" th:text="${notes}"> ... </textarea>
<br> <br>
<!-- <input type="text" name="notes" id="notes" th:text="${notes.toString()}"> --> <!-- <input type="text" name="notes" id="notes" th:text="${notes.toString()}"> -->
<input type="submit" value="Submit"> <input type="submit" value="Submit">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment