Skip to content

Commit 2c98298

Browse files
authored
Merge pull request #15 from CodeURJC-DAW-2021-22/feature/chart
Add bought times per course to teacher user page
2 parents 2d20009 + 53ef76b commit 2c98298

File tree

10 files changed

+202
-73
lines changed

10 files changed

+202
-73
lines changed

backend/src/main/java/com/youdemy/controller/CourseController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public String showCourses(Model model,
7878

7979
if (isAdmin) return "redirect:/admin";
8080

81-
if (user.get().getId() != userId) return "redirect:/courses";
81+
if (user.get().getId() != userId) return "accessDenied";
8282

8383
if (model.getAttribute("teacher").equals(true)) {
8484
Page<Course> teacherCourses = courseService.findByAuthor(user.get(),
8585
PageRequest.of(0, 6));
8686

87-
model.addAttribute("teacherCourses", teacherCourses);
87+
model.addAttribute("courses", teacherCourses);
8888
model.addAttribute("coursesNumResults", teacherCourses.getTotalElements());
8989
model.addAttribute("totalPages", teacherCourses.getTotalPages());
9090
}

backend/src/main/java/com/youdemy/controller/UserController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.youdemy.controller;
22

3+
import com.youdemy.model.Course;
4+
import com.youdemy.model.CourseBoughtTimes;
5+
import com.youdemy.service.CourseService;
36
import com.youdemy.service.OrderPService;
47
import com.youdemy.service.UserService;
58
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.domain.Page;
10+
import org.springframework.data.domain.PageRequest;
611
import org.springframework.security.crypto.password.PasswordEncoder;
712
import org.springframework.stereotype.Controller;
813
import org.springframework.ui.Model;
@@ -15,6 +20,7 @@
1520
import com.youdemy.model.User;
1621

1722
import javax.servlet.http.HttpServletRequest;
23+
import java.lang.reflect.Array;
1824
import java.security.Principal;
1925
import java.util.ArrayList;
2026
import java.util.Objects;
@@ -31,6 +37,9 @@ public class UserController {
3137
@Autowired
3238
private PasswordEncoder passwordEncoder;
3339

40+
@Autowired
41+
private CourseService courseService;
42+
3443
@ModelAttribute
3544
public void addAttributes(Model model, HttpServletRequest request) {
3645
BasicAttributes.addAttributes(model, request, userService);
@@ -94,9 +103,24 @@ public String showUserInfo(Model model, @PathVariable long id, HttpServletReques
94103
User user = userService.findByFirstName(userName);
95104
long userId;
96105
userId = user.getId();
106+
97107
if (userId != id) {
98108
return "accessDenied";
99109
}
110+
111+
if (model.getAttribute("teacher").equals(true)) {
112+
ArrayList<Course> teacherCourses = (ArrayList<Course>) courseService.findByAuthor(user);
113+
ArrayList<CourseBoughtTimes> coursesBoughtTimes = new ArrayList<>();
114+
115+
teacherCourses.forEach(course -> {
116+
CourseBoughtTimes courseBoughtTimes = new CourseBoughtTimes(course.getTitle(), orderPService.countByCourse(course.getId()));
117+
118+
coursesBoughtTimes.add(courseBoughtTimes);
119+
});
120+
121+
model.addAttribute("coursesBoughtTimes", coursesBoughtTimes);
122+
}
123+
100124
model.addAttribute("orders", orderPService.findByUserId(userId));
101125
model.addAttribute("user", user);
102126
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.youdemy.model;
2+
3+
public class CourseBoughtTimes {
4+
5+
private String courseTitle;
6+
private long boughtTimes;
7+
8+
public CourseBoughtTimes() {}
9+
10+
public CourseBoughtTimes(String courseTitle, long boughtTimes) {
11+
this.courseTitle = courseTitle;
12+
this.boughtTimes = boughtTimes;
13+
}
14+
15+
public String getCourseTitle() {
16+
return courseTitle;
17+
}
18+
19+
public void setCourseTitle(String courseTitle) {
20+
this.courseTitle = courseTitle;
21+
}
22+
23+
public long getBoughtTimes() {
24+
return boughtTimes;
25+
}
26+
27+
public void setBoughtTimes(long boughtTimes) {
28+
this.boughtTimes = boughtTimes;
29+
}
30+
}

backend/src/main/java/com/youdemy/repository/CourseRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.springframework.data.jpa.repository.Query;
88
import org.springframework.data.repository.PagingAndSortingRepository;
99

10+
import java.util.List;
11+
1012
public interface CourseRepository extends PagingAndSortingRepository<Course, Long> {
1113

1214
@Query("select c from Course c where c.title like %:title%")
@@ -17,4 +19,6 @@ public interface CourseRepository extends PagingAndSortingRepository<Course, Lon
1719

1820
Page<Course> findByAuthor(User author, Pageable pageable);
1921

22+
List<Course> findByAuthor(User author);
23+
2024
}

backend/src/main/java/com/youdemy/repository/OrderPRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import org.springframework.data.jpa.repository.JpaRepository;
44

55
import com.youdemy.model.OrderP;
6+
import org.springframework.data.jpa.repository.Query;
67

78
import java.util.List;
89

910
public interface OrderPRepository extends JpaRepository<OrderP, Long>{
1011

1112
List<OrderP> findByUser(Long userId);
1213

14+
long countByCourse(long id);
15+
1316
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public Page<Course> findByAuthor(User author, Pageable pageable) {
3939
return repository.findByAuthor(author, pageable);
4040
}
4141

42+
public List<Course> findByAuthor(User author) {
43+
return repository.findByAuthor(author);
44+
}
45+
4246
public Page<Course> findByUser(long userId, Pageable pageable) {
4347
return repository.findByUser(userId, pageable);
4448
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,21 @@ public void init() throws IOException {
197197
orderRepository.save(order1);
198198

199199
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");
200203
orderRepository.save(order2);
204+
orderRepository.save(order22);
205+
orderRepository.save(order23);
206+
orderRepository.save(order24);
201207

202208
OrderP order3 = new OrderP(user3.getId(),10,course3.getId(),user3.getFirstName(),course3.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
203209
orderRepository.save(order3);
204210

205-
OrderP order4 = new OrderP(user4.getId(),10,course1.getId(),user4.getFirstName(),course1.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
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");
206213
orderRepository.save(order4);
214+
orderRepository.save(order42);
207215

208216
OrderP order5 = new OrderP(user1.getId(),10,course5.getId(),user1.getFirstName(),course5.getTitle(),"payment Method","billing Address","Country","Region", "data Card");
209217
orderRepository.save(order5);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ public void delete(long id) {
3939
repository.deleteById(id);
4040
}
4141

42+
public long countByCourse(long id) {
43+
return repository.countByCourse(id);
44+
}
45+
4246
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const config = {
2+
type: 'bar',
3+
data,
4+
options: {}
5+
};
6+
7+
const courseChart = new Chart(
8+
document.querySelector('#courseChart'),
9+
config
10+
);
Lines changed: 112 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,128 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1" />
6-
<meta name="description" content="" />
3+
<head>
4+
<meta charset="utf-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
6+
<meta name="description" content=""/>
77
<meta
8-
name="author"
9-
content="Mark Otto, Jacob Thornton, and Bootstrap contributors"
8+
name="author"
9+
content="Mark Otto, Jacob Thornton, and Bootstrap contributors"
1010
/>
11-
<meta name="generator" content="Hugo 0.88.1" />
11+
<meta name="generator" content="Hugo 0.88.1"/>
1212
<title>YOUDEMMY</title>
13-
14-
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
15-
13+
14+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
15+
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"/>
16+
17+
<!-- ChartJS dependency -->
18+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
19+
1620
<!-- Custom styles for this template -->
17-
<link href="../css/custom.css" rel="stylesheet" />
21+
<link href="../css/custom.css" rel="stylesheet"/>
1822

19-
</head>
23+
</head>
2024

21-
<body>
25+
<body>
2226

2327
{{> header}}
24-
<main>
25-
<div class="container rounded bg-white mt-5 mb-5">
26-
<div class="row">
27-
<div class="col-md-3 border-right">
28-
<div class="d-flex flex-column align-items-center text-center p-3 py-5"><img class="rounded-circle mt-5" width="150px" src="https://st3.depositphotos.com/15648834/17930/v/600/depositphotos_179308454-stock-illustration-unknown-person-silhouette-glasses-profile.jpg">
29-
<span class="font-weight-bold">{{user.firstName}}</span><span class="text-black-50">{{user.email}}</span><span> </span></div>
30-
</div>
31-
<div class="col-md-9 border-right">
32-
<div class="p-3 py-5">
33-
<div class="d-flex justify-content-between align-items-center mb-3">
34-
<h4 class="text-right">Hi! {{user.firstName}} {{user.lastName}} this is your profile Info</h4>
35-
</div>
36-
<div class="row mt-2">
37-
<div class="col-md-6"><label class="labels">Name</label><p>{{user.firstName}}</p></div>
38-
<div class="col-md-6"><label class="labels">Surname</label><p>{{user.lastName}}</p></div>
39-
</div>
40-
<div class="row mt-3">
41-
<div class="d-flex justify-content-between align-items-center mb-3">
42-
<h4 class="text-right">User Orders</h4>
43-
</div>
44-
<div class="col-md-12">
45-
<div class="container">
46-
<table class="table table-hover">
47-
<thead>
48-
<tr>
49-
<th scope="col">#</th>
50-
<th scope="col">ID</th>
51-
<th scope="col">Price</th>
52-
<th scope="col">Course Name</th>
53-
<th scope="col">Billing Address</th>
54-
<th scope="col">Payment method</th>
55-
<th scope="col">Date</th>
56-
</tr>
57-
</thead>
58-
<tbody>
59-
{{#orders}}
60-
<tr>
61-
<th scope="row">+</th>
62-
<td>{{id}}</td>
63-
<td>{{price}}</td>
64-
<td><a href="/courses/{{course}}">{{courseTitle}}</a></td>
65-
<td>{{billingAddress}}</td>
66-
<td>{{paymentMethod}}</td>
67-
<td>{{Date}}</td>
68-
<td><a href="/orders/export/pdf/{{id}}">PDF</a></td>
69-
</tr>
70-
{{/orders}}
28+
<main>
29+
<div class="container rounded bg-white mt-5 mb-5">
30+
<div class="row">
31+
<div class="col-md-3 border-right">
32+
<div class="d-flex flex-column align-items-center text-center p-3 py-5"><img class="rounded-circle mt-5"
33+
width="150px"
34+
src="https://st3.depositphotos.com/15648834/17930/v/600/depositphotos_179308454-stock-illustration-unknown-person-silhouette-glasses-profile.jpg">
35+
<span class="font-weight-bold">{{user.firstName}}</span><span
36+
class="text-black-50">{{user.email}}</span><span> </span></div>
37+
</div>
38+
<div class="col-md-9 border-right">
39+
<div class="p-3 py-5">
40+
<div class="d-flex justify-content-between align-items-center mb-3">
41+
<h4 class="text-right">Hi! {{user.firstName}} {{user.lastName}} this is your profile Info</h4>
42+
</div>
43+
<div class="row mt-2">
44+
<div class="col-md-6"><label class="labels">Name</label>
45+
<p>{{user.firstName}}</p></div>
46+
<div class="col-md-6"><label class="labels">Surname</label>
47+
<p>{{user.lastName}}</p></div>
48+
</div>
49+
<div class="row mt-3">
50+
<div class="d-flex justify-content-between align-items-center mb-3">
51+
<h4 class="text-right">User Orders</h4>
52+
</div>
53+
<div class="col-md-12">
54+
<div class="container">
55+
<table class="table table-hover">
56+
<thead>
57+
<tr>
58+
<th scope="col">#</th>
59+
<th scope="col">ID</th>
60+
<th scope="col">Price</th>
61+
<th scope="col">Course Name</th>
62+
<th scope="col">Billing Address</th>
63+
<th scope="col">Payment method</th>
64+
<th scope="col">Date</th>
65+
</tr>
66+
</thead>
67+
<tbody>
68+
{{#orders}}
69+
<tr>
70+
<th scope="row">+</th>
71+
<td>{{id}}</td>
72+
<td>{{price}}</td>
73+
<td><a href="/courses/{{course}}">{{courseTitle}}</a></td>
74+
<td>{{billingAddress}}</td>
75+
<td>{{paymentMethod}}</td>
76+
<td>{{Date}}</td>
77+
<td><a href="/orders/export/pdf/{{id}}">PDF</a></td>
78+
</tr>
79+
{{/orders}}
80+
81+
</tbody>
82+
</table>
83+
</div>
84+
</div>
7185

72-
</tbody>
73-
</table>
74-
</div>
75-
</div>
76-
86+
</div>
87+
</div>
88+
</div>
89+
</div>
90+
{{#teacher}}
91+
<div class="row">
92+
<div class="row-12">
93+
<canvas id="courseChart"></canvas>
7794
</div>
7895
</div>
96+
{{/teacher}}
97+
{{#teacherCourses}}
98+
<p>{{id}}</p>
99+
{{/teacherCourses}}
79100
</div>
80-
</div>
81-
</div>
82-
83-
</main>
101+
102+
{{#teacher}}
103+
<script>
104+
const labels = [];
105+
106+
const data = {
107+
labels,
108+
datasets: [{
109+
label: 'Number of buyers per course',
110+
backgroundColor: 'rgb(255, 99, 132)',
111+
borderColor: 'rgb(255, 99, 132)',
112+
data: [],
113+
}]
114+
};
115+
116+
{{#coursesBoughtTimes}}
117+
labels.push('{{courseTitle}}');
118+
119+
data.datasets[0].data.push({{boughtTimes}});
120+
{{/coursesBoughtTimes}}
121+
</script>
122+
<script src="../../js/myAccount.js"></script>
123+
{{/teacher}}
124+
125+
</main>
84126
{{> footer}}
85-
</body>
127+
</body>
86128
</html>

0 commit comments

Comments
 (0)