-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseServiceImpl.java
More file actions
501 lines (443 loc) · 26.1 KB
/
CourseServiceImpl.java
File metadata and controls
501 lines (443 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package com.backend.elearning.domain.course;
import com.backend.elearning.domain.cart.Cart;
import com.backend.elearning.domain.cart.CartRepository;
import com.backend.elearning.domain.category.Category;
import com.backend.elearning.domain.category.CategoryRepository;
import com.backend.elearning.domain.common.ECurriculumType;
import com.backend.elearning.domain.common.PageableData;
import com.backend.elearning.domain.learning.learningCourse.LearningCourse;
import com.backend.elearning.domain.learning.learningCourse.LearningCourseRepository;
import com.backend.elearning.domain.learning.learningLecture.LearningLecture;
import com.backend.elearning.domain.learning.learningLecture.LearningLectureRepository;
import com.backend.elearning.domain.learning.learningQuiz.LearningQuiz;
import com.backend.elearning.domain.learning.learningQuiz.LearningQuizRepository;
import com.backend.elearning.domain.lecture.Lecture;
import com.backend.elearning.domain.lecture.LectureRepository;
import com.backend.elearning.domain.order.OrderDetail;
import com.backend.elearning.domain.order.OrderDetailRepository;
import com.backend.elearning.domain.promotion.Promotion;
import com.backend.elearning.domain.quiz.Quiz;
import com.backend.elearning.domain.quiz.QuizRepository;
import com.backend.elearning.domain.review.Review;
import com.backend.elearning.domain.review.ReviewService;
import com.backend.elearning.domain.section.Section;
import com.backend.elearning.domain.section.SectionService;
import com.backend.elearning.domain.section.SectionVM;
import com.backend.elearning.domain.topic.Topic;
import com.backend.elearning.domain.topic.TopicRepository;
import com.backend.elearning.domain.user.*;
import com.backend.elearning.exception.BadRequestException;
import com.backend.elearning.exception.DuplicateException;
import com.backend.elearning.exception.NotFoundException;
import com.backend.elearning.utils.Constants;
import com.backend.elearning.utils.ConvertTitleToSlug;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.*;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@Service
@Slf4j
public class CourseServiceImpl implements CourseService{
private final CourseRepository courseRepository;
private final CategoryRepository categoryRepository;
private final TopicRepository topicRepository;
private final SectionService sectionService;
private final QuizRepository quizRepository;
private final LectureRepository lectureRepository ;
private final ReviewService reviewService;
private final LearningLectureRepository learningLectureRepository;
private final LearningQuizRepository learningQuizRepository;
private final LearningCourseRepository learningCourseRepository;
private final OrderDetailRepository orderDetailRepository;
private final CourseRepositoryCustomImpl courseRepositoryCustom;
private final CartRepository cartRepository;
private final UserService userService;
private final UserRepository userRepository;
private static final String LECTURE_TYPE = "lecture";
private static final String QUIZ_TYPE = "quiz";
private static final String sortBy = "updatedAt";
public CourseServiceImpl(CourseRepository courseRepository, CategoryRepository categoryRepository, TopicRepository topicRepository, SectionService sectionService, QuizRepository quizRepository, LectureRepository lectureRepository, ReviewService reviewService, LearningLectureRepository learningLectureRepository, LearningQuizRepository learningQuizRepository, LearningCourseRepository learningCourseRepository, OrderDetailRepository orderDetailRepository, CourseRepositoryCustomImpl courseRepositoryCustom, CartRepository cartRepository, UserService userService, UserRepository userRepository) {
this.courseRepository = courseRepository;
this.categoryRepository = categoryRepository;
this.topicRepository = topicRepository;
this.sectionService = sectionService;
this.quizRepository = quizRepository;
this.lectureRepository = lectureRepository;
this.reviewService = reviewService;
this.learningLectureRepository = learningLectureRepository;
this.learningQuizRepository = learningQuizRepository;
this.learningCourseRepository = learningCourseRepository;
this.orderDetailRepository = orderDetailRepository;
this.courseRepositoryCustom = courseRepositoryCustom;
this.cartRepository = cartRepository;
this.userService = userService;
this.userRepository = userRepository;
}
@Override
public PageableData<CourseVM> getPageableCourses(int pageNum, int pageSize, String keyword, CourseStatus status) {
log.info("received pageNum: {}, pageSize: {}, keyword: {}, status: {}", pageNum, pageSize, keyword, status);
String email = SecurityContextHolder.getContext().getAuthentication().getName();
Sort sort = Sort.by(sortBy);
sort.descending();
if (email != null) {
log.info("email from token: {}", email);
User user = userRepository.findByEmail(email).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.USER_NOT_FOUND));
if (user.getRole().equals(ERole.ROLE_ADMIN)) {
email = null;
}
}
List<CourseVM> courseVMS = new ArrayList<>();
Pageable pageable = PageRequest.of(pageNum, pageSize, sort);
Page<Course> coursePage = null;
if (status != null) {
coursePage = courseRepository.findAllCustomByRoleAndStatus(pageable, email, keyword, status);
}else {
coursePage = courseRepository.findAllCustomByRole(pageable, email, keyword);
}
List<Course> courses = coursePage.getContent();
for (Course course : courses) {
courseVMS.add(CourseVM.fromModel(course ,new ArrayList<>(),0, 0.0,0,"" , null, false, 0L));
}
return new PageableData(
pageNum,
pageSize,
(int) coursePage.getTotalElements(),
coursePage.getTotalPages(),
courseVMS
);
}
@Override
public CourseVM create(CoursePostVM coursePostVM) {
log.info("received coursePostVM: {}", coursePostVM);
String email = SecurityContextHolder.getContext().getAuthentication().getName();
if (courseRepository.countExistByTitle(coursePostVM.title(), null) > 0) {
throw new DuplicateException(Constants.ERROR_CODE.COURSE_TITLE_DUPLICATED);
}
Category category = categoryRepository.findById(coursePostVM.categoryId()).orElseThrow(
() -> new NotFoundException(Constants.ERROR_CODE.CATEGORY_NOT_FOUND)
);
Topic topic = topicRepository.findById(coursePostVM.topicId()).orElseThrow(
() -> new NotFoundException(Constants.ERROR_CODE.TOPIC_NOT_FOUND)
);
User user = userService.getByEmail(email);
String slug = ConvertTitleToSlug.convertTitleToSlug(coursePostVM.title());
Course course = Course.builder()
.title(coursePostVM.title())
.category(category)
.topic(topic)
.user(user)
.free(true)
.status(CourseStatus.UNPUBLISHED)
.slug(slug)
.build();
if (!course.isFree()) {
course.setPrice(coursePostVM.price());
}
return CourseVM.fromModel(courseRepository.save(course), new ArrayList<>(),0, 0.0,0,"", null, false, 0L);
}
@Override
public CourseVM update(CoursePostVM coursePutVM, Long userId, Long courseId) {
log.info("received coursePutVM: {}", coursePutVM);
Course oldCourse = courseRepository.findByIdReturnSections(courseId).orElseThrow(() -> new NotFoundException(
Constants.ERROR_CODE.COURSE_NOT_FOUND, courseId
));
if (courseRepository.countExistByTitle(coursePutVM.title(), courseId) > 0) {
throw new DuplicateException(Constants.ERROR_CODE.COURSE_TITLE_DUPLICATED);
}
if (!Objects.equals(oldCourse.getCategory().getId(), coursePutVM.categoryId())) {
Category category = categoryRepository.findById(coursePutVM.categoryId()).orElseThrow();
oldCourse.setCategory(category);
}
if (!Objects.equals(oldCourse.getTopic().getId(), coursePutVM.topicId())) {
Topic topic = topicRepository.findById(coursePutVM.topicId()).orElseThrow();
oldCourse.setTopic(topic);
}
oldCourse.setTitle(coursePutVM.title());
oldCourse.setHeadline(coursePutVM.headline());
oldCourse.setObjectives(coursePutVM.objectives());
oldCourse.setRequirements(coursePutVM.requirements());
oldCourse.setDescription(coursePutVM.description());
oldCourse.setTargetAudiences(coursePutVM.targetAudiences());
oldCourse.setFree(coursePutVM.free());
oldCourse.setStatus(CourseStatus.UNPUBLISHED);
if (!coursePutVM.free()) {
oldCourse.setPrice(coursePutVM.price());
}
if (!Objects.equals(coursePutVM.image(), "")) {
oldCourse.setImageId(coursePutVM.image());
}
if (coursePutVM.level() != null) {
oldCourse.setLevel(coursePutVM.level());
}
return CourseVM.fromModel(courseRepository.save(oldCourse), new ArrayList<>(),0, 0.0,0,"", null , false, 0L);
}
@Override
public CourseVM getCourseById(Long id) {
Course course = courseRepository.findByIdReturnSections(id).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND, id));
course = courseRepository.findByIdWithPromotions(course).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND, id));
Long courseId = course.getId();
List<Review> reviews = reviewService.findByCourseId(courseId);
int ratingCount = reviews.size();
Double averageRating = reviews.stream().map(review -> review.getRatingStar()).mapToDouble(Integer::doubleValue).average().orElse(0.0);
double roundedAverageRating = Math.round(averageRating * 10) / 10.0;
AtomicInteger totalCurriculumCourse = new AtomicInteger();
AtomicInteger totalDurationCourse = new AtomicInteger();
course.getSections().forEach(section -> {
Long sectionId = section.getId();
List<Lecture> lectures = lectureRepository.findBySectionId(sectionId);
int totalSeconds = lectures.stream()
.mapToInt(lecture -> lecture.getDuration())
.sum();
totalDurationCourse.addAndGet(totalSeconds);
});
String formattedHours = convertSeconds(totalDurationCourse.get());
List<SectionVM> sections = new ArrayList<>(course.getSections()
.stream().map(section -> sectionService.getById(section.getId(), null)).toList());
sections.forEach(sectionVM -> {
int countCurriculumPerSection = sectionVM.curriculums().size();
totalCurriculumCourse.addAndGet(countCurriculumPerSection);
});
sections.sort(Comparator.comparing(SectionVM::number));
Long userId = course.getUser().getId();
UserProfileVM userProfileVM = userService.getById(userId);
boolean learning = true;
String email = SecurityContextHolder.getContext().getAuthentication().getName();
if (email != null && !learningCourseRepository.findByStudentAndCourse(email, courseId).isPresent()) {
learning = false;
}
Set<Promotion> promotions = course.getPromotions();
Long discountedPrice = getDiscountedPriceByCourse(promotions, course);
return CourseVM.fromModel(course, sections,ratingCount, roundedAverageRating, totalCurriculumCourse.get(),formattedHours, userProfileVM, learning, discountedPrice);
}
private Long getDiscountedPriceByCourse(Set<Promotion> promotions, Course course) {
LocalDateTime now = LocalDateTime.now();
Optional<Promotion> currentPromotion = promotions.stream()
.filter(promotion -> promotion.getStartTime().isBefore(now) && promotion.getEndTime().isAfter(now))
.findFirst();
if (course.isFree()) {
return null;
}
Long discountedPrice = currentPromotion.isPresent() ? course.getPrice() - (currentPromotion.get().getDiscountPercent()*course.getPrice()/100) : course.getPrice();
return discountedPrice;
}
@Override
public CourseListGetVM getCourseListGetVMById(Long id) {
log.info("received id: {}", id);
Course course = courseRepository.findByIdReturnSections(id).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND, id));
course = courseRepository.findByIdWithPromotions(course).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND, id));
Long courseId = course.getId();
List<Review> reviews = reviewService.findByCourseId(courseId);
int ratingCount = reviews.size();
Double averageRating = reviews.stream().map(review -> review.getRatingStar()).mapToDouble(Integer::doubleValue).average().orElse(0.0);
double roundedAverageRating = Math.round(averageRating * 10) / 10.0;
// get totalCurriculum, total duration of course by section
AtomicInteger totalCurriculumCourse = new AtomicInteger();
AtomicInteger totalDurationCourse = new AtomicInteger();
course.getSections().forEach(section -> {
Long sectionId = section.getId();
List<Lecture> lectures = lectureRepository.findBySectionId(sectionId);
List<Quiz> quizzes = quizRepository.findBySectionId(sectionId );
totalCurriculumCourse.addAndGet(lectures.size());
totalCurriculumCourse.addAndGet(quizzes.size());
int totalSeconds = lectures.stream()
.mapToInt(lecture -> lecture.getDuration())
.sum();
totalDurationCourse.addAndGet(totalSeconds);
});
String formattedHours = convertSeconds(totalDurationCourse.get());
Set<Promotion> promotions = course.getPromotions();
Long discountedPrice = getDiscountedPriceByCourse(promotions, course);
return CourseListGetVM.fromModel(course, formattedHours, totalCurriculumCourse.get(), roundedAverageRating, ratingCount, discountedPrice);
}
@Override
public CourseLearningVm getCourseBySlug(String slug) {
log.info("Received slug: {}", slug);
String email = SecurityContextHolder.getContext().getAuthentication().getName();
Course course = courseRepository.findBySlugReturnSections(slug).orElseThrow(
() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND, slug)
);
AtomicInteger totalCurriculumCourse = new AtomicInteger();
List<SectionVM> sections = new ArrayList<>(course.getSections()
.stream().map(section -> sectionService.getById(section.getId(), email)).toList());
sections.forEach(sectionVM -> {
int countCurriculumPerSection = sectionVM.curriculums().size();
totalCurriculumCourse.addAndGet(countCurriculumPerSection);
});
sections.sort(Comparator.comparing(SectionVM::number));
CourseVM courseVM = CourseVM.fromModel(course, sections ,0, 0.0, totalCurriculumCourse.get(),"", null, true, 0L );
Optional<LearningLecture> maxAccessTimeByEmailAndCourseSlugLecture = learningLectureRepository.findMaxAccessTimeByEmailAndCourseSlug(email, slug);
Optional<LearningQuiz> maxAccessTimeByEmailAndCourseSlugQuiz = learningQuizRepository.findMaxAccessTimeByEmailAndCourseSlug(email, slug);
if (maxAccessTimeByEmailAndCourseSlugLecture.isPresent() && maxAccessTimeByEmailAndCourseSlugQuiz.isPresent()) {
var lecture = maxAccessTimeByEmailAndCourseSlugLecture.get();
var quiz = maxAccessTimeByEmailAndCourseSlugQuiz.get();
if (lecture.getAccessTime().isAfter(quiz.getAccessTime())) {
return new CourseLearningVm(courseVM, lecture.getLecture().getSection().getId(), lecture.getLecture().getId(), lecture.getWatchingSecond(), LECTURE_TYPE);
} else {
return new CourseLearningVm(courseVM, quiz.getQuiz().getSection().getId(), quiz.getQuiz().getId(), null, QUIZ_TYPE);
}
} else if (maxAccessTimeByEmailAndCourseSlugLecture.isPresent()) {
var lecture = maxAccessTimeByEmailAndCourseSlugLecture.get();
return new CourseLearningVm(courseVM, lecture.getLecture().getSection().getId(), lecture.getLecture().getId(), lecture.getWatchingSecond(), LECTURE_TYPE);
} else if (maxAccessTimeByEmailAndCourseSlugQuiz.isPresent()) {
var quiz = maxAccessTimeByEmailAndCourseSlugQuiz.get();
return new CourseLearningVm(courseVM, quiz.getQuiz().getSection().getId(), quiz.getQuiz().getId(), null, QUIZ_TYPE);
}
// if (sections.size() == 0) {
// throw new BadRequestException("course dont have lecture");
// }
Long sectionId = sections.get(0).id();
Long curriculumId = sections.get(0).curriculums().get(0).getId();
if (sections.get(0).curriculums().get(0).getType().equals(ECurriculumType.lecture)){
return new CourseLearningVm(courseVM, sectionId ,curriculumId, 0, LECTURE_TYPE);
}
return new CourseLearningVm(courseVM, sectionId ,curriculumId, 0, QUIZ_TYPE);
}
@Override
public List<CourseListGetVM> getByUserId(Long userId) {
log.info("received userId: {}", userId);
List<Course> courses = courseRepository.findByUserIdReturnSections(userId);
List<CourseListGetVM> courseListGetVMS = courses.stream().map(course -> getCourseListGetVMById(course.getId())).toList();
return courseListGetVMS;
}
@Override
public PageableData<CourseListGetVM> getCoursesByMultiQuery(int pageNum,
int pageSize,
String title,
Float rating,
String[] level,
Boolean[] free,
String categoryName, Integer topicId
) {
log.info("received pageNum: {}, pageSize: {}, title: {}, rating: {}, level: {}, free: {}, categoryName: {}, " +
"topicId: {}", pageNum, pageSize, title, rating, level, free, categoryName, topicId);
Pageable pageable = PageRequest.of(pageNum, pageSize);
Page<Course> coursePage = courseRepositoryCustom.findByMultiFilter(title, rating, level, free, categoryName, topicId, pageable);
List<Course> courses = coursePage.getContent();
List<CourseListGetVM> courseListGetVMS = courses.stream().map(course -> {
course = courseRepository.findByIdWithPromotions(course).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND));
List<Review> reviews = course.getReviews();
int ratingCount = reviews.size();
Double averageRating = reviews.stream().map(review -> review.getRatingStar()).mapToDouble(Integer::doubleValue).average().orElse(0.0);
double roundedAverageRating = Math.round(averageRating * 10) / 10.0;
AtomicInteger totalCurriculumCourse = new AtomicInteger();
AtomicInteger totalDurationCourse = new AtomicInteger();
List<Section> sections = sectionService.findByCourseId(course.getId());
sections.forEach(section -> {
List<Lecture> lectures = section.getLectures();
List<Quiz> quizzes = section.getQuizzes();
totalCurriculumCourse.addAndGet(lectures.size());
totalCurriculumCourse.addAndGet(quizzes.size());
int totalSeconds = lectures.stream()
.mapToInt(lecture -> lecture.getDuration())
.sum();
totalDurationCourse.addAndGet(totalSeconds);
});
String formattedHours = convertSeconds(totalDurationCourse.get());
Set<Promotion> promotions = course.getPromotions();
Long discountedPrice = getDiscountedPriceByCourse(promotions, course);
return CourseListGetVM.fromModel(course, formattedHours, totalCurriculumCourse.get(), roundedAverageRating, ratingCount, discountedPrice);
}).toList();
return new PageableData(
pageNum,
pageSize,
coursePage.getTotalElements(),coursePage.getTotalPages()
,
courseListGetVMS
);
}
@Override
public List<CourseListGetVM> getCoursesByMultiQueryReturnList(int pageNum, int pageSize, String title, Float rating, String[] level, Boolean[] free, String categoryName, Integer topicId) {
log.info("received pageNum: {}, pageSize: {}, title: {}, rating: {}, level: {}, free: {}, categoryName: {}, " +
"topicId: {}", pageNum, pageSize, title, rating, level, free, categoryName, topicId);
List<Course> courses = title != null ? courseRepository.findByMultiQueryWithKeyword(title, rating, level, free, categoryName, topicId) :
courseRepository.findByMultiQuery(rating, level, free, categoryName, topicId);
List<CourseListGetVM> courseListGetVMS = courses.stream().map(course -> {
course = courseRepository.findByIdWithPromotions(course).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND));
List<Review> reviews = course.getReviews();
int ratingCount = reviews.size();
Double averageRating = reviews.stream().map(review -> review.getRatingStar()).mapToDouble(Integer::doubleValue).average().orElse(0.0);
double roundedAverageRating = Math.round(averageRating * 10) / 10.0;
AtomicInteger totalCurriculumCourse = new AtomicInteger();
AtomicInteger totalDurationCourse = new AtomicInteger();
List<Section> sections = sectionService.findByCourseId(course.getId());
sections.forEach(section -> {
List<Lecture> lectures = section.getLectures();
List<Quiz> quizzes = section.getQuizzes();
totalCurriculumCourse.addAndGet(lectures.size());
totalCurriculumCourse.addAndGet(quizzes.size());
int totalSeconds = lectures.stream()
.mapToInt(lecture -> lecture.getDuration())
.sum();
totalDurationCourse.addAndGet(totalSeconds);
});
Set<Promotion> promotions = course.getPromotions();
Long discountedPrice = getDiscountedPriceByCourse(promotions, course);
String formattedHours = convertSeconds(totalDurationCourse.get());
return CourseListGetVM.fromModel(course, formattedHours, totalCurriculumCourse.get(), roundedAverageRating, ratingCount, discountedPrice);
}).toList();
return courseListGetVMS;
}
@Override
public List<CourseListGetVM> getCoursesByCategoryId(Integer categoryId) {
log.info("received categoryId: {}", categoryId);
List<Course> courses = courseRepository.findByCategoryIdWithStatus(categoryId);
List<CourseListGetVM> courseListGetVMS = courses.stream().map(course -> getCourseListGetVMById(course.getId())).toList();
return courseListGetVMS;
}
@Override
public void delete(Long id) {
log.info("received courseId: {}", id);
Course course = courseRepository.findByIdReturnSections(id).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND, id));
if (course.getSections().size() > 0 ) {
throw new BadRequestException(Constants.ERROR_CODE.COURSE_HAD_SECTION, id);
}
List<LearningCourse> learningCourses = learningCourseRepository.findByCourseId(id);
if (learningCourses.size() > 0) {
throw new BadRequestException(Constants.ERROR_CODE.COURSE_HAD_BEEN_BOUGHT, id);
}
List<Cart> carts = cartRepository.findByCourseId(id);
if (carts.size() > 0) {
throw new BadRequestException(Constants.ERROR_CODE.COURSE_HAD_CART, id);
}
List<OrderDetail> orderDetails = orderDetailRepository.findByCourseId(id);
if (orderDetails.size() > 0) {
throw new BadRequestException(Constants.ERROR_CODE.COURSE_HAD_BEEN_BOUGHT, id);
}
courseRepository.delete(course);
}
@Override
public void updateStatusCourse(CourseStatusPostVM courseStatusPostVM, Long courseId) {
log.info("received status: {}, courseId: {}", courseStatusPostVM, courseId);
Course course = courseRepository.findById(courseId).orElseThrow();
if (courseStatusPostVM.status().equals(CourseStatus.UNPUBLISHED) && courseStatusPostVM.reason() != null) {
course.setReasonRefused(courseStatusPostVM.reason());
}
course.setStatus(courseStatusPostVM.status());
courseRepository.saveAndFlush(course);
}
@Override
public List<CourseAssignPromotion> getByPromotionId(Long promotionId) {
List<Course> courses = courseRepository.findCoursesNotInPromotionToday(promotionId);
List<CourseAssignPromotion> courseAssignPromotions = courses.stream().map(course -> {
Set<Promotion> promotions = course.getPromotions();
boolean isNotInPromotions = promotions.stream()
.noneMatch(promotion -> promotion.getId().equals(promotionId));
return CourseAssignPromotion.fromModel(course, !isNotInPromotions);
}).toList();
return courseAssignPromotions;
}
private String convertSeconds(int seconds) {
if (seconds < 3600) {
int minutes = seconds / 60;
int remainingSeconds = seconds % 60;
return minutes + " phút " + remainingSeconds + " giây";
} else {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
return hours + " giờ " + minutes + " phút";
}
}
}