|
5 | 5 |
|
6 | 6 | import com.fasterxml.jackson.databind.ObjectMapper;
|
7 | 7 | import com.youdemy.model.Lesson;
|
| 8 | +import com.youdemy.model.OrderP; |
8 | 9 | import com.youdemy.model.User;
|
9 | 10 | import com.youdemy.repository.UserRepository;
|
| 11 | +import com.youdemy.service.OrderPService; |
10 | 12 | import com.youdemy.service.UserService;
|
11 | 13 |
|
12 | 14 | import org.json.JSONObject;
|
@@ -41,6 +43,9 @@ public class CourseController {
|
41 | 43 | @Autowired
|
42 | 44 | private UserRepository userRepository;
|
43 | 45 |
|
| 46 | + @Autowired |
| 47 | + private OrderPService orderPService; |
| 48 | + |
44 | 49 | @ModelAttribute
|
45 | 50 | public void addAttributes(Model model, HttpServletRequest request) {
|
46 | 51 | Principal principal = request.getUserPrincipal();
|
@@ -101,19 +106,36 @@ public Page<Course> getCurrentCourse(@RequestParam Optional<Integer> page,
|
101 | 106 | public String showCourse(Model model, @PathVariable long id, HttpServletRequest request) {
|
102 | 107 | Optional<Course> course = courseService.findById(id);
|
103 | 108 | if (course.isPresent()) {
|
104 |
| - model.addAttribute("course", course.get()); |
105 | 109 | Principal principal = request.getUserPrincipal();
|
106 | 110 | if(principal != null) {
|
107 | 111 | String userName = principal.getName();
|
108 | 112 | Optional<User> user = userRepository.findByFirstName(userName);
|
109 |
| - long userId; |
110 |
| - userId = user.get().getId(); |
| 113 | + long userId = user.get().getId(); |
111 | 114 | model.addAttribute("userId", userId);
|
112 |
| - if(course.get().getAuthor().getId() == userId) { |
| 115 | + |
| 116 | + if(course.get().getAuthor().getId() == userId |
| 117 | + || model.getAttribute("admin") == Boolean.valueOf(true)) { |
113 | 118 | model.addAttribute("owner", true);
|
| 119 | + model.addAttribute("hasAccess", true); |
114 | 120 | }
|
115 |
| - |
| 121 | + |
| 122 | + ArrayList<OrderP> orders = new ArrayList<>(orderPService.findByUserId(userId)); |
| 123 | + |
| 124 | + orders.forEach(order -> { |
| 125 | + Course orderCourse = courseService.findById(order.getCourse()).get(); |
| 126 | + if(orderCourse.getId() == course.get().getId()) |
| 127 | + model.addAttribute("hasAccess", true); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + // Empty lesson video urls if user doesn't have access to course |
| 132 | + if(model.getAttribute("hasAccess") == null) { |
| 133 | + course.get().getLessons().forEach(lesson -> { |
| 134 | + lesson.setVideoUrl(""); |
| 135 | + }); |
116 | 136 | }
|
| 137 | + |
| 138 | + model.addAttribute("course", course.get()); |
117 | 139 |
|
118 | 140 | return "course";
|
119 | 141 | } else {
|
|
0 commit comments