Skip to content

Commit 8ee56b2

Browse files
authored
Merge pull request #16 from CodeURJC-DAW-2021-22/bug/preloadImagesOnContainer
Load random images from API
2 parents 2c35bc4 + 77bb090 commit 8ee56b2

File tree

10 files changed

+172
-248
lines changed

10 files changed

+172
-248
lines changed

backend/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<dependency>
7171
<groupId>org.springdoc</groupId>
7272
<artifactId>springdoc-openapi-ui</artifactId>
73-
<version>1.5.0</version>
73+
<version>1.5.12</version>
7474
</dependency>
7575
</dependencies>
7676

backend/src/main/java/com/youdemy/security/RepositoryUserDetailsService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,5 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
3434
return new org.springframework.security.core.userdetails.User(user.getFirstName(),
3535
user.getEncodedPassword(), roles);
3636
}
37-
38-
39-
40-
4137

4238
}

backend/src/main/java/com/youdemy/security/RestSecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ protected void configure(HttpSecurity http) throws Exception {
7373
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
7474

7575
}
76+
7677
}
7778

backend/src/main/java/com/youdemy/security/WebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ protected void configure(HttpSecurity http) throws Exception {
3636
http.authorizeRequests().antMatchers("/courses").permitAll();
3737
http.authorizeRequests().antMatchers("/courses/page").permitAll();
3838
http.authorizeRequests().antMatchers("/courses/thumbnail/**").permitAll();
39+
http.authorizeRequests().antMatchers("/image/**").permitAll();
3940

4041

4142
// User pages
4243
http.authorizeRequests().antMatchers("/courses/new").hasAnyRole("TEACHER", "ADMIN");
4344
http.authorizeRequests().antMatchers("/courses/user/**").hasAnyRole("USER", "TEACHER", "ADMIN");
44-
http.authorizeRequests().antMatchers("/image/**").hasAnyRole("TEACHER", "ADMIN");
4545
http.authorizeRequests().antMatchers("/orders/**").hasAnyRole("USER");
4646
http.authorizeRequests().antMatchers("/admin").hasAnyRole("ADMIN");
4747
http.authorizeRequests().antMatchers("/myaccount/**").hasAnyRole("USER","ADMIN");

backend/src/main/java/com/youdemy/service/CourseService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public Page<Course> findByUser(long userId, Pageable pageable) {
4747
return repository.findByUser(userId, pageable);
4848
}
4949

50-
public void save(Course course) {
51-
repository.save(course);
50+
public Course save(Course course) {
51+
return repository.save(course);
5252
}
5353

5454
public void delete(long id) {
Lines changed: 164 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.youdemy.service;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.File;
45
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.net.URL;
58
import java.nio.file.Files;
69
import java.util.ArrayList;
710
import java.util.Arrays;
@@ -46,6 +49,9 @@ public class DatabaseInitializer {
4649
@Autowired
4750
private OrderPRepository orderRepository;
4851

52+
@Autowired
53+
private VideoThumbnailService videoThumbnailService;
54+
4955
@Autowired
5056
private PasswordEncoder passwordEncoder;
5157

@@ -66,165 +72,190 @@ public void init() throws IOException {
6672
userRepository.save(user4);
6773

6874

69-
byte[] thumbnail = loadRandomImage();
70-
71-
ArrayList<String> tags = new ArrayList<>();
72-
tags.add("Tag1");
73-
tags.add("Tag2");
74-
75-
76-
Course course1 = new Course("Java", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user4);
77-
78-
List<Lesson> lessons1 = new ArrayList<>();
79-
80-
Lesson lesson1 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course1, "", 1);
81-
82-
lessons1.add(lesson1);
83-
84-
course1.setLessons(lessons1);
85-
75+
// byte[] thumbnail = loadRandomImage();
76+
//
77+
// ArrayList<String> tags = new ArrayList<>();
78+
// tags.add("Tag1");
79+
// tags.add("Tag2");
80+
//
81+
//
82+
// Course course1 = new Course("Java", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user4);
83+
//
84+
// List<Lesson> lessons1 = new ArrayList<>();
85+
//
86+
// Lesson lesson1 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course1, "", 1);
87+
//
88+
// lessons1.add(lesson1);
89+
//
90+
// course1.setLessons(lessons1);
91+
//
92+
//
93+
// courseService.save(course1);
94+
//
95+
//
96+
//
97+
// Course course = new Course("Python", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
98+
//
99+
// List<Lesson> lessons = new ArrayList<>();
100+
//
101+
// Lesson lesson = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course, "", 1);
102+
//
103+
// lessons.add(lesson);
104+
//
105+
// course.setLessons(lessons);
106+
//
107+
//
108+
// courseService.save(course);
109+
//
110+
// Course course2 = new Course("SQL", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
111+
//
112+
// List<Lesson> lessons2 = new ArrayList<>();
113+
//
114+
// Lesson lesson2 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course2, "", 1);
115+
//
116+
// lessons2.add(lesson2);
117+
//
118+
// course2.setLessons(lessons2);
119+
//
120+
//
121+
// courseService.save(course2);
122+
//
123+
// Course course3 = new Course("C#", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
124+
//
125+
// List<Lesson> lessons3 = new ArrayList<>();
126+
//
127+
// Lesson lesson3 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course3, "", 1);
128+
//
129+
// lessons3.add(lesson3);
130+
//
131+
// course3.setLessons(lessons3);
132+
//
133+
//
134+
// courseService.save(course3);
135+
//
136+
// Course course4 = new Course("Ensamblador", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
137+
//
138+
// List<Lesson> lessons4 = new ArrayList<>();
139+
//
140+
// Lesson lesson4 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course4, "", 1);
141+
//
142+
// lessons4.add(lesson4);
143+
//
144+
// course4.setLessons(lessons4);
145+
//
146+
// courseService.save(course4);
147+
//
148+
//
149+
// Course course5 = new Course("Ensamblador", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
150+
//
151+
// List<Lesson> lessons5 = new ArrayList<>();
152+
//
153+
// Lesson lesson5 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course5, "", 1);
154+
//
155+
// lessons5.add(lesson5);
156+
//
157+
// course5.setLessons(lessons5);
158+
//
159+
// courseService.save(course5);
160+
//
161+
//
162+
//
163+
//
164+
// Course course6 = new Course("Relacional;", "Curso de Relacionl", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
165+
//
166+
// List<Lesson> lessons6 = new ArrayList<>();
167+
//
168+
// Lesson lesson6 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course6, "", 1);
169+
//
170+
// lessons6.add(lesson6);
171+
//
172+
// course6.setLessons(lessons6);
173+
//
174+
// courseService.save(course6);
175+
//
86176

87-
courseService.save(course1);
88-
177+
ArrayList<Course> courses = new ArrayList<>();
89178

179+
// Sample course
180+
for (int i = 0; i < 30; i++) {
181+
byte[] thumbnail = loadRandomImage();
90182

91-
Course course = new Course("Python", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
92-
93-
List<Lesson> lessons = new ArrayList<>();
94-
95-
Lesson lesson = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course, "", 1);
96-
97-
lessons.add(lesson);
98-
99-
course.setLessons(lessons);
100-
183+
ArrayList<String> tags = new ArrayList<>();
184+
tags.add("Tag1");
185+
tags.add("Tag2");
101186

102-
courseService.save(course);
103-
104-
Course course2 = new Course("SQL", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
105-
106-
List<Lesson> lessons2 = new ArrayList<>();
107-
108-
Lesson lesson2 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course2, "", 1);
109-
110-
lessons2.add(lesson2);
111-
112-
course2.setLessons(lessons2);
113-
187+
Course course = new Course("Java", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user1);
114188

115-
courseService.save(course2);
116-
117-
Course course3 = new Course("C#", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
118-
119-
List<Lesson> lessons3 = new ArrayList<>();
120-
121-
Lesson lesson3 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course3, "", 1);
122-
123-
lessons3.add(lesson3);
124-
125-
course3.setLessons(lessons3);
126-
189+
List<Lesson> lessons = new ArrayList<>();
190+
191+
for (int j = 0; j < 5; j++) {
192+
VideoThumbnail videoThumbnail = new VideoThumbnail();
193+
videoThumbnail.setName("" + j);
194+
videoThumbnail.setData(loadRandomImage());
195+
videoThumbnail.setType("jpg");
196+
197+
long thumbnailId = videoThumbnailService.save(videoThumbnail);
198+
lessons.add(new Lesson("Lesson " + j, "Desc", userRepository.getById(Long.parseLong("3")), course, "https://www.youtube.com/embed/HDhR2Yhnvfo", thumbnailId));
199+
}
200+
201+
course.setLessons(lessons);
202+
203+
204+
courses.add(courseService.save(course));
205+
}
127206

128-
courseService.save(course3);
129-
130-
Course course4 = new Course("Ensamblador", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
131-
132-
List<Lesson> lessons4 = new ArrayList<>();
133-
134-
Lesson lesson4 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course4, "", 1);
135-
136-
lessons4.add(lesson4);
137-
138-
course4.setLessons(lessons4);
139-
140-
courseService.save(course4);
141-
142-
143-
Course course5 = new Course("Ensamblador", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
144-
145-
List<Lesson> lessons5 = new ArrayList<>();
146-
147-
Lesson lesson5 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course5, "", 1);
148-
149-
lessons5.add(lesson5);
150-
151-
course5.setLessons(lessons5);
152-
153-
courseService.save(course5);
154-
155-
156-
157-
158-
Course course6 = new Course("Relacional;", "Curso de Relacionl", 100, thumbnail, tags, new ArrayList<Lesson>(), user3);
159-
160-
List<Lesson> lessons6 = new ArrayList<>();
161-
162-
Lesson lesson6 = new Lesson("Lesson 1", "Desc", userRepository.getById(Long.parseLong("3")), course6, "", 1);
163-
164-
lessons6.add(lesson6);
165-
166-
course6.setLessons(lessons6);
167-
168-
courseService.save(course6);
169-
170207

171-
172-
173-
// // Sample course
174-
// for (int i = 0; i < 0; i++) {
175-
// byte[] thumbnail = loadRandomImage();
176-
//
177-
// ArrayList<String> tags = new ArrayList<>();
178-
// tags.add("Tag1");
179-
// tags.add("Tag2");
180-
//
181-
// Course course = new Course("Java", "Curso de Java", 100, thumbnail, tags, new ArrayList<Lesson>(), user1);
182-
//
183-
// List<Lesson> lessons = new ArrayList<>();
184-
//
185-
// for (int j = 0; j < 5; j++) {
186-
// lessons.add(new Lesson(j + "", "Desc", userRepository.getById(Long.parseLong("3")), course, "https://www.youtube.com/embed/HDhR2Yhnvfo", 1));
187-
// }
188-
//
189-
// course.setLessons(lessons);
190-
//
191-
// courseService.save(course);
192-
// }
193-
//
194-
195208
// // Sample orders
196-
OrderP order1 = new OrderP(user1.getId(),10,course1.getId(),user1.getFirstName(),course1.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
209+
OrderP order1 = new OrderP(user1.getId(),10,courses.get(0).getId(),user1.getFirstName(),courses.get(0).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
197210
orderRepository.save(order1);
198211

199-
OrderP order2 = new OrderP(user2.getId(),10,course2.getId(),user2.getFirstName(),course2.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
200-
OrderP order22 = new OrderP(user2.getId(),10,course2.getId(),user2.getFirstName(),course2.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
201-
OrderP order23 = new OrderP(user2.getId(),10,course2.getId(),user2.getFirstName(),course2.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
202-
OrderP order24 = new OrderP(user2.getId(),10,course2.getId(),user2.getFirstName(),course2.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
212+
OrderP order2 = new OrderP(user2.getId(),10,courses.get(0).getId(),user2.getFirstName(),courses.get(0).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
213+
OrderP order22 = new OrderP(user2.getId(),10,courses.get(1).getId(),user2.getFirstName(),courses.get(1).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
214+
OrderP order23 = new OrderP(user2.getId(),10,courses.get(8).getId(),user2.getFirstName(),courses.get(8).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
215+
OrderP order24 = new OrderP(user2.getId(),10,courses.get(8).getId(),user2.getFirstName(),courses.get(8).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
203216
orderRepository.save(order2);
204217
orderRepository.save(order22);
205218
orderRepository.save(order23);
206219
orderRepository.save(order24);
207220

208-
OrderP order3 = new OrderP(user3.getId(),10,course3.getId(),user3.getFirstName(),course3.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
221+
OrderP order3 = new OrderP(user3.getId(),10,courses.get(8).getId(),user3.getFirstName(),courses.get(8).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
209222
orderRepository.save(order3);
210223

211-
OrderP order4 = new OrderP(user4.getId(),10,course4.getId(),user4.getFirstName(),course4.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
212-
OrderP order42 = new OrderP(user4.getId(),10,course4.getId(),user4.getFirstName(),course4.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
224+
OrderP order4 = new OrderP(user4.getId(),10,courses.get(8).getId(),user4.getFirstName(),courses.get(8).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
225+
OrderP order42 = new OrderP(user4.getId(),10,courses.get(24).getId(),user4.getFirstName(),courses.get(24).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
213226
orderRepository.save(order4);
214227
orderRepository.save(order42);
215-
216-
OrderP order5 = new OrderP(user1.getId(),10,course5.getId(),user1.getFirstName(),course5.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
228+
229+
OrderP order5 = new OrderP(user1.getId(),10,courses.get(24).getId(),user1.getFirstName(),courses.get(24).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
217230
orderRepository.save(order5);
218231

219-
OrderP order6 = new OrderP(user2.getId(),10,course6.getId(),user2.getFirstName(),course6.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
232+
OrderP order6 = new OrderP(user2.getId(),10,courses.get(24).getId(),user2.getFirstName(),courses.get(24).getTitle(),"payment Method","billing Address","Country","Region", "data Card");
220233
orderRepository.save(order6);
221234
}
222235

223236
public byte[] loadRandomImage() throws IOException {
224-
int randomImgNum = (int) Math.floor(Math.random() * 9) + 1;
225-
File image = ResourceUtils.getFile("classpath:./fakeImages/" + randomImgNum + ".jpg");
237+
URL url = new URL("https://picsum.photos/1920/1080");
238+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
239+
InputStream is = null;
240+
try {
241+
is = url.openStream ();
242+
byte[] byteChunk = new byte[4096];
243+
int n;
244+
245+
while ( (n = is.read(byteChunk)) > 0 ) {
246+
baos.write(byteChunk, 0, n);
247+
}
248+
}
249+
catch (IOException e) {
250+
System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
251+
e.printStackTrace ();
252+
}
253+
finally {
254+
if (is != null) { is.close(); }
255+
}
256+
257+
return baos.toByteArray();
226258

227-
return Files.readAllBytes(image.toPath());
228259
}
229260

230261
}

0 commit comments

Comments
 (0)