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

register marche refonte du dao

parent e50a1048
No related branches found
No related tags found
No related merge requests found
package jez.authentication2;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
......@@ -22,6 +21,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
http.authorizeHttpRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/user").hasRole("USER")
.antMatchers("/notes").hasRole("USER")
.antMatchers("/notes/{id}").hasRole("USER")
.antMatchers("/register_notes").hasRole("USER")
.antMatchers("/").permitAll()
......
package jez.authentication2.controllers;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
......@@ -24,7 +22,6 @@ public class MainController {
@Autowired
NotesService notesService;
@GetMapping("/")
public String displayHome() {
return "home.html";
......@@ -32,7 +29,15 @@ public class MainController {
@GetMapping(value = "/notes")
public String displayNotes() {
return "redirect:/notes/1";
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByUsername(authentication.getName());
// si l'utilisateur a 0 notes
if (user.getNotes().isEmpty()) {
user = createNewNote(user);
}
return "redirect:/notes/%s".formatted(user.getNotes().get(0).getId());
}
@GetMapping(value = "/notes/{note_id}")
......@@ -43,7 +48,6 @@ public class MainController {
// si l'utilisateur a 0 notes
if (user.getNotes().isEmpty()) {
user = createNewNote(user);
// user = userService.findUserByUsername(authentication.getName());
}
user.getNotes().sort(new Comparator<Note>() {
......@@ -53,18 +57,15 @@ public class MainController {
return o1.getId() - o2.getId();
}
});
// System.out.println(user);
// System.out.println(user.getNotes().get(note_id - 1));
// model.addAttribute("user", user);
model.addAttribute("note_id", note_id);
model.addAttribute("notes", user.getNotes().get(note_id - 1));
List<Integer> ids = new ArrayList<>();
for (Note note : user.getNotes()) {
ids.add(note.getId());
}
model.addAttribute("ids", ids);
model.addAttribute("notes", notesService.findById(note_id));
// model.addAttribute("notes", user.getNotes().get(note_id - 1));
// List<String> titles = new ArrayList<>();
// for (Note note : user.getNotes()) {
// titles.add(note.getTitle());
// }
model.addAttribute("allNotes", user.getNotes());
return "main_page.html";
}
......@@ -86,7 +87,8 @@ public class MainController {
userService.saveUser(user);
// select * from users join notes on users.id=notes.user_id;
// select * from notes;
return "redirect:/notes/%s".formatted(note_id);
}
......
......@@ -15,10 +15,19 @@ public class Note {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
private String note;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
@JoinColumn(name = "user_id")
private User user;
public Note() {
......@@ -69,4 +78,6 @@ public class Note {
this.user = user;
}
}
package jez.authentication2.entities;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
......@@ -20,7 +18,8 @@ public class User {
private String password;
private boolean enabled;
private String authority;
@OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST})
@OneToMany(mappedBy = "user")
private List<Note> notes;
public User(Integer id, String username, String password, boolean enabled, List<Note> notes) {
......@@ -31,8 +30,7 @@ public class User {
this.notes = notes;
}
public User() {
}
public User() {}
public List<Note> getNotes() {
return notes;
......
......@@ -14,4 +14,9 @@ public class NotesService {
public void saveNote(Note note) {
notesRepository.save(note);
}
public Note findById(Integer id)
{
return notesRepository.findById(id).orElse(null);
}
}
insert into users (username, password, enabled, authority)
values ('user', 'password', true, 'ROLE_USER');
insert into notes (user_id, note)
values (1, 'some ...1');
insert into notes (user_id, note)
values (1, 'some notes ...2');
insert into notes (user_id, title, note)
values (1, 'voala', 'some ...1');
insert into notes (user_id, title, note)
values (1, 'course', 'some notes ...2');
insert into users (username, password, enabled, authority)
values ('admin', 'password', true, 'ROLE_ADMIN');
......
......@@ -17,6 +17,7 @@ create table users(
drop table if exists notes cascade;
create table notes (
id serial primary key,
title text,
note text,
user_id int not null,
foreign key(user_id) references users(id)
......
......@@ -37,10 +37,11 @@
<div class="btn-group-vertical">
<tr th:each="id: ${ids}">
<tr th:each="note: ${allNotes}">
<td>
<form th:action="@{/notes/{note_id}(note_id=${id})}" th:method="get">
<button class="btn btn-secondary" th:text="|Page: ${id}|" type="submit"> ...
<form th:action="@{/notes/{note_id}(note_id=${note.getId()})}" th:method="get">
<button class="btn btn-secondary" th:text="|Page: ${note.getTitle()}|"
type="submit"> ...
</button>
</form>
</td>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment