16
16
import org .springframework .beans .factory .annotation .Autowired ;
17
17
import org .springframework .http .HttpStatus ;
18
18
import org .springframework .http .ResponseEntity ;
19
+ import org .springframework .security .core .Authentication ;
20
+ import org .springframework .security .core .context .SecurityContextHolder ;
21
+ import org .springframework .security .core .userdetails .UserDetails ;
19
22
import org .springframework .ui .Model ;
20
23
import org .springframework .util .ResourceUtils ;
21
24
import org .springframework .web .bind .annotation .GetMapping ;
34
37
import com .youdemy .model .Course ;
35
38
import com .youdemy .model .Lesson ;
36
39
import com .youdemy .model .User ;
40
+ import com .youdemy .security .jwt .UserLoginService ;
37
41
import com .youdemy .service .CourseService ;
38
42
import com .youdemy .service .UserService ;
39
43
@@ -48,6 +52,9 @@ public class CourseRestController {
48
52
@ Autowired
49
53
UserService userService ;
50
54
55
+ @ Autowired
56
+ private UserLoginService userLoginService ;
57
+
51
58
@ GetMapping ("/" )
52
59
public Collection <Course > getCourses () {
53
60
return courseService .findAll ();
@@ -76,30 +83,41 @@ public ResponseEntity<Course> getCourse(@PathVariable long id){
76
83
// }
77
84
78
85
@ PostMapping ("/" )
79
- public String postNewCourse (@ RequestParam ("title" ) String title , @ RequestParam ("thumbnail" ) String image ,
80
- @ RequestParam ("description" ) String description , @ RequestParam ("price" ) int price ,
81
- @ RequestParam ("tags" ) List <String > tags , @ RequestBody ("lessons" ) List <Lesson > lessons ,
82
- Model model ) throws IOException {
86
+ public String postNewCourse (@ RequestBody Course newCourse , Model model ) throws IOException {
87
+
88
+
89
+
90
+
91
+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
92
+
93
+ System .out .println ("aquiiiiiiiiiiii " +authentication .getName ());
94
+
83
95
84
- Course course = new Course ();
85
- User author = userService .findByFirstName (Objects .requireNonNull (model .getAttribute ("userName" )).toString ());
86
96
87
- List <Lesson > lessonList = new ArrayList <>(Arrays .asList (new ObjectMapper ().readValues (lessons , Lesson [].class )));
88
- lessonList .forEach (lesson -> {
97
+
98
+ UserDetails userDetails = (UserDetails ) SecurityContextHolder .getContext ().getAuthentication ()
99
+ .getPrincipal ();
100
+ String username = userDetails .getUsername ();
101
+
102
+ System .out .println ("aquiiiiiiiiiiii " +username );
103
+
104
+ User author = userService .findByFirstName (Objects .requireNonNull (authentication .getName ()));
105
+
106
+
107
+ newCourse .getLessons ().forEach (lesson -> {
89
108
lesson .setAuthor (author );
90
- lesson .setCourse (course );
109
+ lesson .setCourse (newCourse );
91
110
});
92
111
93
- course .setAuthor (author );
94
- course .setThumbnail (loadRandomImage ());
95
- course .setTitle (title );
96
- course .setDescription (description );
97
- course .setPrice (price );
98
- course .setTags (tags );
99
- course .setLessons (lessonList );
112
+ newCourse .setAuthor (author );
113
+ newCourse .setThumbnail (loadRandomImage ());
114
+
100
115
101
- courseService .save (course );
102
- return "redirect:/courses" ;
116
+ courseService .save (newCourse );
117
+
118
+ System .out .println (newCourse .getLessons ());
119
+ return "redirect:/" ;
120
+
103
121
}
104
122
105
123
public byte [] loadRandomImage () throws IOException {
0 commit comments