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

affichage des id des notes de chaque personne

parent 89ff2440
Branches
No related tags found
No related merge requests found
...@@ -10,14 +10,16 @@ import org.springframework.ui.Model; ...@@ -10,14 +10,16 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
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.UserService; import jez.authentication2.services.UserService;
@Controller @Controller
public class MainController { public class MainController {
@Autowired
DataSource dataSource;
@Autowired @Autowired
UserService userService; UserService userService;
@Autowired
NotesService notesService;
@GetMapping("/") @GetMapping("/")
public String displayHome() public String displayHome()
...@@ -29,12 +31,13 @@ public class MainController { ...@@ -29,12 +31,13 @@ public class MainController {
public String displayNotes(Model model) throws SQLException { public String displayNotes(Model model) throws SQLException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String notes = userService.getNotes(authentication.getName()); String notes = notesService.getNotes(authentication.getName(), 1);
System.out.println(notes); System.out.println(notes);
System.out.println(userService.getUser(authentication.getName())); System.out.println(userService.getUser(authentication.getName()));
model.addAttribute("notes", notes); model.addAttribute("notes", notes);
model.addAttribute("ids", notesService.getAllIds(authentication.getName()));
return "main_page.html"; return "main_page.html";
} }
...@@ -44,7 +47,7 @@ public class MainController { ...@@ -44,7 +47,7 @@ public class MainController {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
userService.setNotes(authentication.getName(), notes); notesService.setNotes(authentication.getName(), 1, notes);
return "redirect:/notes"; return "redirect:/notes";
} }
......
package jez.authentication2.services;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class NotesService {
@Autowired
DataSource dataSource;
public void setNotes(String username, int id, String notes) throws SQLException {
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
statement.executeUpdate("""
UPDATE notes SET notes='%s' WHERE username='%s AND id=%s';
""".formatted(notes, username, id));
}
public String getNotes(String username, int id) throws SQLException {
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
ResultSet result = statement
.executeQuery("SELECT notes FROM notes WHERE username='%s' AND id=%s;".formatted(username, id));
result.next();
String notes = result.getString(1);
return notes;
}
public Set<Integer> getAllIds(String username) throws SQLException {
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
ResultSet result = statement
.executeQuery("SELECT id FROM notes WHERE username='%s';".formatted(username));
Set<Integer> ids = new HashSet<>();
while (result.next()) {
ids.add(result.getInt(1));
}
return ids;
}
}
...@@ -48,22 +48,4 @@ public class UserService { ...@@ -48,22 +48,4 @@ public class UserService {
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));
} }
public void setNotes(String username, String notes) throws SQLException {
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
statement.executeUpdate("""
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 notes WHERE username='%s';".formatted(username));
result.next();
String notes = result.getString(1);
return notes;
}
} }
# spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect # spring.jpa.properspringties.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.hibernate.show-sql=true
......
...@@ -2,7 +2,9 @@ insert into users (username, password, enabled) ...@@ -2,7 +2,9 @@ insert into users (username, password, enabled)
values ('user', 'password', true); values ('user', 'password', true);
insert into notes (username, notes) insert into notes (username, notes)
values ('user', 'some notes ...sqd'); values ('user', 'some notes ...1');
insert into notes (username, notes)
values ('user', 'some notes ...2');
insert into users (username, password, enabled) insert into users (username, password, enabled)
values ('admin', 'password', true); values ('admin', 'password', true);
......
<!DOCTYPE html> <!DOCTYPE html>
<!-- xmlns:th="http://www.thymeleaf.org" --> <!-- xmlns:th="http://www.thymeleaf.org" -->
<html> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset='utf-8'> <meta charset='utf-8'>
...@@ -28,13 +28,20 @@ ...@@ -28,13 +28,20 @@
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<!-- <p th:text="${notes.toString()}"></p> -->
<!-- A GAUCHE AFFICHER TOUS LES IDS -->
<div class="card">
<tr th:each="id: ${ids}">
<td th:text="${id}">
</tr>
</div>
<div class="col-10 m-4"> <div class="col-10 m-4">
<!-- afficher le login en haut a gauche --> <!-- afficher le login en haut a gauche -->
<!-- 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}"> ... </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 to comment