File tree Expand file tree Collapse file tree 5 files changed +91
-6
lines changed
securetimenotes/src/main/java/securetimenotes/andrefelipebarros/securetimenotes Expand file tree Collapse file tree 5 files changed +91
-6
lines changed Original file line number Diff line number Diff line change 1+ package securetimenotes .andrefelipebarros .securetimenotes .controllers ;
2+
3+ import org .springframework .web .bind .annotation .RequestMapping ;
4+ import org .springframework .web .bind .annotation .RestController ;
5+
6+ import securetimenotes .andrefelipebarros .securetimenotes .model .note .Note ;
7+ import securetimenotes .andrefelipebarros .securetimenotes .model .user .User ;
8+ import securetimenotes .andrefelipebarros .securetimenotes .repository .NotesRespository ;
9+ import securetimenotes .andrefelipebarros .securetimenotes .repository .UserRepository ;
10+
11+ import java .util .List ;
12+
13+ import org .springframework .beans .factory .annotation .Autowired ;
14+ import org .springframework .security .access .prepost .PreAuthorize ;
15+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
16+ import org .springframework .web .bind .annotation .GetMapping ;
17+ import org .springframework .web .bind .annotation .PostMapping ;
18+
19+
20+ @ RestController
21+ @ RequestMapping ("user" )
22+ public class NotesController {
23+ @ Autowired
24+ NotesRespository noteRepository ;
25+
26+ @ Autowired
27+ UserRepository userRepository ;
28+
29+ @ GetMapping ("/notes" )
30+ @ PreAuthorize ("hasRole('USER')" )
31+ public List <Note > getNotes (@ AuthenticationPrincipal User user ) {
32+ return noteRepository .findByUser (user );
33+ }
34+
35+ @ PostMapping ("/notes" )
36+ @ PreAuthorize ("hasRole('USER')" )
37+ public void postNotes (@ AuthenticationPrincipal User user ) {
38+ Note note = new Note ();
39+
40+ note .setContent ("Conteúdo da nota" );
41+ note .setUser (user );
42+
43+ noteRepository .save (note );
44+ }
45+
46+
47+ }
Original file line number Diff line number Diff line change 1+ package securetimenotes .andrefelipebarros .securetimenotes .model .note ;
2+
3+ import securetimenotes .andrefelipebarros .securetimenotes .model .user .User ;
4+
5+ import jakarta .persistence .*;
6+ import lombok .*;
7+
8+
9+ @ Entity
10+ @ Data
11+ @ Setter
12+ @ Getter
13+ @ Table (name = "Notes" )
14+ public class Note {
15+ @ Id
16+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
17+ private Long id ;
18+
19+ private String content ;
20+
21+ // Relacionamento ManyToOne com User
22+ @ ManyToOne
23+ @ JoinColumn (name = "user_id" )
24+ private User user ;
25+
26+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11package securetimenotes .andrefelipebarros .securetimenotes .model .user ;
22
3+ import securetimenotes .andrefelipebarros .securetimenotes .model .note .Note ;
4+
35import org .springframework .security .core .authority .SimpleGrantedAuthority ;
46import org .springframework .security .core .userdetails .UserDetails ;
57import org .springframework .security .core .GrantedAuthority ;
911import jakarta .persistence .Entity ;
1012import jakarta .persistence .Table ;
1113import jakarta .persistence .Id ;
12-
14+ import jakarta . persistence . OneToMany ;
1315import lombok .AllArgsConstructor ;
1416import lombok .NoArgsConstructor ;
1517import lombok .EqualsAndHashCode ;
@@ -37,6 +39,10 @@ public class User implements UserDetails {
3739 private String password ;
3840 private UserRole role ;
3941
42+ // Relacionamento OneToMany com Note
43+ @ OneToMany (mappedBy = "user" )
44+ private List <Note > notes ;
45+
4046 //Criar constructor com 3 parâmetros(retirando id):
4147 public User (String username , String password , UserRole role ){
4248 this .username = username ;
Original file line number Diff line number Diff line change 1+ package securetimenotes .andrefelipebarros .securetimenotes .repository ;
2+
3+ import java .util .List ;
4+
5+ import org .springframework .data .jpa .repository .JpaRepository ;
6+ import securetimenotes .andrefelipebarros .securetimenotes .model .note .Note ;
7+ import securetimenotes .andrefelipebarros .securetimenotes .model .user .User ;
8+
9+ public interface NotesRespository extends JpaRepository <Note , Long >{
10+ List <Note > findByUser (User user );
11+ }
You can’t perform that action at this time.
0 commit comments