diff --git a/majorLink/src/main/java/com/example/majorLink/controller/LectureController.java b/majorLink/src/main/java/com/example/majorLink/controller/LectureController.java index e0ac16c..1238038 100644 --- a/majorLink/src/main/java/com/example/majorLink/controller/LectureController.java +++ b/majorLink/src/main/java/com/example/majorLink/controller/LectureController.java @@ -2,6 +2,7 @@ import com.example.majorLink.domain.Lecture; import com.example.majorLink.domain.User; +import com.example.majorLink.domain.mapping.TuteeLecture; import com.example.majorLink.dto.request.LectureRequestDTO; import com.example.majorLink.dto.response.LectureResponseDTO; import com.example.majorLink.global.auth.AuthUser; @@ -28,7 +29,7 @@ public LectureResponseDTO.CreateLecture createReview(@RequestBody LectureRequest User user = authUser.getUser(); - Lecture lecture = lectureService.createLecture(user, request); + Lecture lecture = lectureService.createLecture(user.getId(), request); return LectureResponseDTO.CreateLecture.builder() .lectureId(lecture.getId()) @@ -41,10 +42,10 @@ public LectureResponseDTO.CreateLecture createReview(@RequestBody LectureRequest @ResponseBody public LectureResponseDTO.UpdateLecture updateLecture(@RequestBody LectureRequestDTO request, @AuthenticationPrincipal AuthUser authUser, - @PathVariable(name = "lectureId") Long lectureId){ + @PathVariable(name = "lectureId") Long lectureId){ User user = authUser.getUser(); - Lecture lecture = lectureService.updateLecture(user, lectureId, request); + Lecture lecture = lectureService.updateLecture(user.getId(), request, lectureId); return LectureResponseDTO.UpdateLecture.builder() .lectureId(lecture.getId()) @@ -60,7 +61,7 @@ public void deleteLecture(@PathVariable(name = "lectureId") Long lectureId, User user = authUser.getUser(); - lectureService.DeleteLecture(user, lectureId); + lectureService.DeleteLecture(user.getId(), lectureId); } // 강의 list 조회 api @@ -84,4 +85,119 @@ public LectureResponseDTO.LecturePreViewList getLectures(@RequestParam(name = "p .build(); } + // 강의 수강 신청 api + @PostMapping("/{lectureId}/register") + @ResponseBody + public LectureResponseDTO.CreateTuteeLecture createTuteeLecture(@PathVariable(name = "lectureId") Long lectureId, + @AuthenticationPrincipal AuthUser authUser){ + User user = authUser.getUser(); + + TuteeLecture tuteeLecture = lectureService.addLecture(user.getId(), lectureId); + + return LectureResponseDTO.CreateTuteeLecture.builder() + .tuteeLectureId(tuteeLecture.getId()) + .createdAt(user.getCreatedAt()) + .build(); + } + + // 강의 취소 api + @DeleteMapping("/{lectureId}/cancel") + @ResponseBody + public void cancelLecture(@PathVariable(name = "lectureId") Long lectureId, + @AuthenticationPrincipal AuthUser authUser){ + User user = authUser.getUser(); + + lectureService.cancelLecture(user.getId(), lectureId); + } + + // 강의 좋아요 토글 api + @PostMapping("/{lectureId}/like") + @ResponseBody + public Boolean toggleLike(@PathVariable(name = "lectureId") Long lectureId, + @AuthenticationPrincipal AuthUser authUser){ + User user = authUser.getUser(); + + return lectureService.toggleLike(user.getId(), lectureId); + } + + // 좋아요 많은 강의 조회 api + @GetMapping("/mostLiked") + @ResponseBody + public LectureResponseDTO.LecturePreViewList getMostLikedLectures(@RequestParam(name = "page") Integer page){ + Page lectureList = lectureService.getMostLikedLecture(page-1); + + return LectureResponseDTO.LecturePreViewList.builder() + .lectureList(lectureList.stream() + .map(lecture -> LectureResponseDTO.LecturePreView.builder() + .name(lecture.getName()) + .build()) + .collect(Collectors.toList())) + .listSize(lectureList.getNumberOfElements()) + .totalPage(lectureList.getTotalPages()) + .totalElements(lectureList.getTotalElements()) + .isFirst(lectureList.isFirst()) + .isLast(lectureList.isLast()) + .build(); + } + + // 최신 강의 조회 api + @GetMapping("/new") + @ResponseBody + public LectureResponseDTO.LecturePreViewList getNewLectures(@RequestParam(name = "page") Integer page){ + Page lectureList = lectureService.getNewLecture(page-1); + + return LectureResponseDTO.LecturePreViewList.builder() + .lectureList(lectureList.stream() + .map(lecture -> LectureResponseDTO.LecturePreView.builder() + .name(lecture.getName()) + .build()) + .collect(Collectors.toList())) + .listSize(lectureList.getNumberOfElements()) + .totalPage(lectureList.getTotalPages()) + .totalElements(lectureList.getTotalElements()) + .isFirst(lectureList.isFirst()) + .isLast(lectureList.isLast()) + .build(); + } + + // 가장 많이 모집된 강의 조회 api + @GetMapping("/mostRecruited") + @ResponseBody + public LectureResponseDTO.LecturePreViewList getMostRecruitedLectures(@RequestParam(name = "page") Integer page){ + Page lectureList = lectureService.getMostRecruitedLecture(page-1); + + return LectureResponseDTO.LecturePreViewList.builder() + .lectureList(lectureList.stream() + .map(lecture -> LectureResponseDTO.LecturePreView.builder() + .name(lecture.getName()) + .build()) + .collect(Collectors.toList())) + .listSize(lectureList.getNumberOfElements()) + .totalPage(lectureList.getTotalPages()) + .totalElements(lectureList.getTotalElements()) + .isFirst(lectureList.isFirst()) + .isLast(lectureList.isLast()) + .build(); + } + + // 카테고리별 강의 조회 api + @GetMapping("/{categoryId}") + @ResponseBody + public LectureResponseDTO.LecturePreViewList getLecturesByCategory(@RequestParam(name = "page") Integer page, + @PathVariable(name = "categoryId") Long categoryId){ + Page lectureList = lectureService.getLectureByCategory(page-1, categoryId); + + return LectureResponseDTO.LecturePreViewList.builder() + .lectureList(lectureList.stream() + .map(lecture -> LectureResponseDTO.LecturePreView.builder() + .name(lecture.getName()) + .build()) + .collect(Collectors.toList())) + .listSize(lectureList.getNumberOfElements()) + .totalPage(lectureList.getTotalPages()) + .totalElements(lectureList.getTotalElements()) + .isFirst(lectureList.isFirst()) + .isLast(lectureList.isLast()) + .build(); + } } diff --git a/majorLink/src/main/java/com/example/majorLink/controller/ReviewController.java b/majorLink/src/main/java/com/example/majorLink/controller/ReviewController.java index 06d32a5..f9d2995 100644 --- a/majorLink/src/main/java/com/example/majorLink/controller/ReviewController.java +++ b/majorLink/src/main/java/com/example/majorLink/controller/ReviewController.java @@ -22,7 +22,7 @@ public class ReviewController { private final ReviewService reviewService; // 리뷰 추가하는 api - @PostMapping("/{lectureId}/reviews") + @PostMapping("/{lectureId}") @ResponseBody public ReviewResponseDTO.CreateReview createReview(@RequestBody ReviewRequestDTO request, @PathVariable(name = "lectureId") Long lectureId, @@ -40,8 +40,10 @@ public ReviewResponseDTO.CreateReview createReview(@RequestBody ReviewRequestDTO @PutMapping("/{reviewId}") @ResponseBody public ReviewResponseDTO.UpdateReview updateReview(@RequestBody ReviewRequestDTO request, - @PathVariable(name = "reviewId") Long reviewId){ - Review review = reviewService.updateReview(reviewId, request); + @PathVariable(name = "reviewId") Long reviewId, + @AuthenticationPrincipal AuthUser authUser){ + User user = authUser.getUser(); + Review review = reviewService.updateReview(user.getId(), reviewId, request); return ReviewResponseDTO.UpdateReview.builder() .reviewId(review.getId()) @@ -52,16 +54,18 @@ public ReviewResponseDTO.UpdateReview updateReview(@RequestBody ReviewRequestDTO // 리뷰 삭제하는 api @DeleteMapping("/{reviewId}") @ResponseBody - public void deleteReview(@PathVariable(name = "reviewId") Long reviewId){ - reviewService.deleteReview(reviewId); + public void deleteReview(@PathVariable(name = "reviewId") Long reviewId, + @AuthenticationPrincipal AuthUser authUser){ + User user = authUser.getUser(); + + reviewService.deleteReview(user.getId(), reviewId); } // 리뷰 조회하는 api - @GetMapping("/{lectureId}/reviews") + @GetMapping("/{lectureId}") @ResponseBody - // 스웨거 세팅 후 파라미터 등 설명 추가 public ReviewResponseDTO.ReviewPreViewList getReviews(@PathVariable(name = "lectureId") Long lectureId, - @RequestParam(name = "page") Integer page){ + @RequestParam(name = "page", defaultValue = "1") Integer page){ Page reviewList = reviewService.getReviewList(lectureId, page-1); return ReviewResponseDTO.ReviewPreViewList.builder() @@ -69,8 +73,6 @@ public ReviewResponseDTO.ReviewPreViewList getReviews(@PathVariable(name = "lect .map(review -> ReviewResponseDTO.ReviewPreView.builder() .ownerNickname(review.getUser().getUsername()) .rate(review.getRate()) - .content(review.getContent()) - .createdAt(review.getCreatedAt()) .build()) .collect(Collectors.toList())) .listSize(reviewList.getNumberOfElements()) @@ -80,4 +82,18 @@ public ReviewResponseDTO.ReviewPreViewList getReviews(@PathVariable(name = "lect .isLast(reviewList.isLast()) .build(); } + + // 리뷰 내용 조회 api + @GetMapping("/{reviewId}/details") + @ResponseBody + public ReviewResponseDTO.ReviewPreView getReview(@PathVariable(name = "reviewId") Long reviewId){ + Review review = reviewService.getReview(reviewId); + + return ReviewResponseDTO.ReviewPreView.builder() + .ownerNickname(review.getUser().getUsername()) + .rate(review.getRate()) + .content(review.getContent()) + .createdAt(review.getCreatedAt()) + .build(); + } } diff --git a/majorLink/src/main/java/com/example/majorLink/domain/Category.java b/majorLink/src/main/java/com/example/majorLink/domain/Category.java index 7f58bb7..038d787 100644 --- a/majorLink/src/main/java/com/example/majorLink/domain/Category.java +++ b/majorLink/src/main/java/com/example/majorLink/domain/Category.java @@ -3,9 +3,6 @@ import jakarta.persistence.*; import lombok.*; -import java.util.ArrayList; -import java.util.List; - @Entity @Getter @Builder @@ -17,12 +14,10 @@ public class Category extends BaseEntity{ @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Column(nullable = false, length = 40) - private String name; + @Column(nullable = false, length = 20) + private String mainCategory; - /*@OneToMany(mappedBy = "category", cascade = CascadeType.ALL) - private List tagList = new ArrayList<>(); + @Column(nullable = false, length = 20) + private String subCategory; - @OneToMany(mappedBy = "category", cascade = CascadeType.ALL) - private List lectureList = new ArrayList<>();*/ } diff --git a/majorLink/src/main/java/com/example/majorLink/domain/Lecture.java b/majorLink/src/main/java/com/example/majorLink/domain/Lecture.java index 4b7b0f6..28b9286 100644 --- a/majorLink/src/main/java/com/example/majorLink/domain/Lecture.java +++ b/majorLink/src/main/java/com/example/majorLink/domain/Lecture.java @@ -3,16 +3,11 @@ import com.example.majorLink.domain.enums.Day; import com.example.majorLink.domain.enums.Exam; import com.example.majorLink.domain.enums.Level; -import com.example.majorLink.domain.mapping.Bookmark; -import com.example.majorLink.domain.mapping.Liked; -import com.example.majorLink.domain.mapping.UserLecture; import jakarta.persistence.*; import lombok.*; import java.time.LocalTime; -import java.util.ArrayList; import java.util.Date; -import java.util.List; @Entity @Getter @@ -38,12 +33,17 @@ public class Lecture extends BaseEntity{ private String info; @Enumerated(EnumType.STRING) - @Column(nullable = false, columnDefinition = "VARCHAR(10)") + @Column(nullable = false, columnDefinition = "VARCHAR(20)") private Level level; + // 총 인원 @Column(nullable = false) private int pNum; + // 현재 신청 인원 + @Column(nullable = false) + private int curPNum; + private LocalTime time; @Enumerated(EnumType.STRING) @@ -61,24 +61,13 @@ public class Lecture extends BaseEntity{ @JoinColumn(name = "category_id") private Category category; - @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) - @JoinColumn(name = "tag_id") - private Tag tag; - - /*@OneToMany(mappedBy = "lecture", cascade = CascadeType.ALL) - private List userLectureList = new ArrayList<>(); - - @OneToMany(mappedBy = "lecture", cascade = CascadeType.ALL) - private List likedList = new ArrayList<>(); + @Column(nullable = false, length = 20) + private String tag; - @OneToMany(mappedBy = "lecture", cascade = CascadeType.ALL) - private List bookmarkList = new ArrayList<>();*/ - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", nullable = false) - private User user; + @Column(nullable = false, length = 100) + private String tutor; - public void updateLecture(String name, String body, int curri, String info, Level level, int pNum, LocalTime time, Day day, Date startDate, Exam exam, Category category, Tag tag ){ + public void updateLecture(String name, String body, int curri, String info, Level level, int pNum, LocalTime time, Day day, Date startDate, Exam exam, Category category, String tag ){ this.name = name; this.body = body; this.curri = curri; @@ -92,4 +81,12 @@ public void updateLecture(String name, String body, int curri, String info, Leve this.category = category; this.tag = tag; } + + public void addCurPNum(){ + this.curPNum++; + } + + public void subCurPNum(){ + this.curPNum--; + } } \ No newline at end of file diff --git a/majorLink/src/main/java/com/example/majorLink/domain/Tag.java b/majorLink/src/main/java/com/example/majorLink/domain/Tag.java deleted file mode 100644 index e823ea0..0000000 --- a/majorLink/src/main/java/com/example/majorLink/domain/Tag.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.example.majorLink.domain; - -import jakarta.persistence.*; -import lombok.*; - -import java.util.ArrayList; -import java.util.List; - -@Entity -@Getter -@Builder -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -public class Tag extends BaseEntity{ - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Column(nullable = false, length = 40) - private String name; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "category_id") - private Category category; - - /*@OneToMany(mappedBy = "tag", cascade = CascadeType.ALL) - private List lectureList = new ArrayList<>();*/ -} diff --git a/majorLink/src/main/java/com/example/majorLink/domain/enums/LectureStatus.java b/majorLink/src/main/java/com/example/majorLink/domain/enums/LectureStatus.java index 4813810..2e18ace 100644 --- a/majorLink/src/main/java/com/example/majorLink/domain/enums/LectureStatus.java +++ b/majorLink/src/main/java/com/example/majorLink/domain/enums/LectureStatus.java @@ -1,5 +1,5 @@ package com.example.majorLink.domain.enums; public enum LectureStatus { - INPROGRESS, FINISHED + INPROGRESS, FINISHED, CANCELLED } diff --git a/majorLink/src/main/java/com/example/majorLink/domain/mapping/UserLecture.java b/majorLink/src/main/java/com/example/majorLink/domain/mapping/TuteeLecture.java similarity index 94% rename from majorLink/src/main/java/com/example/majorLink/domain/mapping/UserLecture.java rename to majorLink/src/main/java/com/example/majorLink/domain/mapping/TuteeLecture.java index a5712fd..30fda01 100644 --- a/majorLink/src/main/java/com/example/majorLink/domain/mapping/UserLecture.java +++ b/majorLink/src/main/java/com/example/majorLink/domain/mapping/TuteeLecture.java @@ -12,7 +12,7 @@ @Builder @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor -public class UserLecture extends BaseEntity { +public class TuteeLecture extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/majorLink/src/main/java/com/example/majorLink/domain/mapping/TutorLecture.java b/majorLink/src/main/java/com/example/majorLink/domain/mapping/TutorLecture.java new file mode 100644 index 0000000..c01b055 --- /dev/null +++ b/majorLink/src/main/java/com/example/majorLink/domain/mapping/TutorLecture.java @@ -0,0 +1,28 @@ +package com.example.majorLink.domain.mapping; + +import com.example.majorLink.domain.BaseEntity; +import com.example.majorLink.domain.Lecture; +import com.example.majorLink.domain.User; +import com.example.majorLink.domain.enums.LectureStatus; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class TutorLecture extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "lecture_id") + private Lecture lecture; +} diff --git a/majorLink/src/main/java/com/example/majorLink/dto/request/LectureRequestDTO.java b/majorLink/src/main/java/com/example/majorLink/dto/request/LectureRequestDTO.java index 009fcbd..52d4385 100644 --- a/majorLink/src/main/java/com/example/majorLink/dto/request/LectureRequestDTO.java +++ b/majorLink/src/main/java/com/example/majorLink/dto/request/LectureRequestDTO.java @@ -1,7 +1,5 @@ package com.example.majorLink.dto.request; -import com.example.majorLink.domain.Category; -import com.example.majorLink.domain.Tag; import com.example.majorLink.domain.enums.Day; import com.example.majorLink.domain.enums.Exam; import com.example.majorLink.domain.enums.Level; @@ -9,23 +7,20 @@ import java.time.LocalTime; import java.util.Date; -import java.util.UUID; @Getter public class LectureRequestDTO { // 새로운 강의 등록할때 필요한 정보들 작성 String name; - String body; - int curri; - String info; + String body; // 강의 개요 + int curri; // 강의 횟수 + String info; // 강의 정보 Level level; int pNum; LocalTime time; - Day day; + Day day; // 요일 Date startDate; - Exam exam; + Exam exam; // 시험 유무 Long categoryId; - Long tagId; - UUID userId; - + String tag; } diff --git a/majorLink/src/main/java/com/example/majorLink/dto/response/LectureResponseDTO.java b/majorLink/src/main/java/com/example/majorLink/dto/response/LectureResponseDTO.java index c24a126..a74dccc 100644 --- a/majorLink/src/main/java/com/example/majorLink/dto/response/LectureResponseDTO.java +++ b/majorLink/src/main/java/com/example/majorLink/dto/response/LectureResponseDTO.java @@ -48,5 +48,12 @@ public static class UpdateLecture { LocalDateTime updatedAt; } - + @Builder + @Getter + @AllArgsConstructor + @NoArgsConstructor + public static class CreateTuteeLecture { + Long tuteeLectureId; + LocalDateTime createdAt; + } } diff --git a/majorLink/src/main/java/com/example/majorLink/repository/LectureRepository.java b/majorLink/src/main/java/com/example/majorLink/repository/LectureRepository.java index 484d72a..8a54c65 100644 --- a/majorLink/src/main/java/com/example/majorLink/repository/LectureRepository.java +++ b/majorLink/src/main/java/com/example/majorLink/repository/LectureRepository.java @@ -2,14 +2,21 @@ import com.example.majorLink.domain.Lecture; import com.example.majorLink.domain.User; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import java.util.Optional; - public interface LectureRepository extends JpaRepository { + @Query("SELECT l FROM Lecture l ORDER BY (SELECT COUNT(lk) FROM Liked lk WHERE lk.lecture.id = l.id) DESC") + Page orderByLikedCount(Pageable pageable); + + @Query("SELECT l FROM Lecture l ORDER BY l.curPNum DESC") + Page orderByCurPNum(Pageable pageable); - @Query("SELECT lec FROM Lecture lec WHERE lec.user = :user AND lec.id = :lecId") - Optional findByUser(User user, Long lecId); + @Query("SELECT l FROM Lecture l WHERE l.category.id = :categoryId") + Page orderByCategoryId(Long categoryId, Pageable pageable); -} + @Query("SELECT l FROM Lecture l ORDER BY l.createdAt DESC") + Page orderByCreatedAt(Pageable pageable); +} \ No newline at end of file diff --git a/majorLink/src/main/java/com/example/majorLink/repository/LikedRepository.java b/majorLink/src/main/java/com/example/majorLink/repository/LikedRepository.java new file mode 100644 index 0000000..a1afd57 --- /dev/null +++ b/majorLink/src/main/java/com/example/majorLink/repository/LikedRepository.java @@ -0,0 +1,12 @@ +package com.example.majorLink.repository; + +import com.example.majorLink.domain.Lecture; +import com.example.majorLink.domain.User; +import com.example.majorLink.domain.mapping.Liked; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface LikedRepository extends JpaRepository { + Boolean existsByUserAndLecture(User user, Lecture lecture); + + void deleteByUserAndLecture(User user, Lecture lecture); +} \ No newline at end of file diff --git a/majorLink/src/main/java/com/example/majorLink/repository/TagRepository.java b/majorLink/src/main/java/com/example/majorLink/repository/TagRepository.java deleted file mode 100644 index b63f5e3..0000000 --- a/majorLink/src/main/java/com/example/majorLink/repository/TagRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.example.majorLink.repository; - -import com.example.majorLink.domain.Tag; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface TagRepository extends JpaRepository { - // 필요한 추가 쿼리 메소드가 있다면 여기에 정의 -} diff --git a/majorLink/src/main/java/com/example/majorLink/repository/TuteeLectureRepository.java b/majorLink/src/main/java/com/example/majorLink/repository/TuteeLectureRepository.java new file mode 100644 index 0000000..bba3ec3 --- /dev/null +++ b/majorLink/src/main/java/com/example/majorLink/repository/TuteeLectureRepository.java @@ -0,0 +1,11 @@ +package com.example.majorLink.repository; + +import com.example.majorLink.domain.mapping.TuteeLecture; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; +import java.util.UUID; + +public interface TuteeLectureRepository extends JpaRepository { + Optional findByUserIdAndLectureId(UUID userId, Long lectureId); +} \ No newline at end of file diff --git a/majorLink/src/main/java/com/example/majorLink/repository/TutorLectureRepository.java b/majorLink/src/main/java/com/example/majorLink/repository/TutorLectureRepository.java new file mode 100644 index 0000000..373f2ec --- /dev/null +++ b/majorLink/src/main/java/com/example/majorLink/repository/TutorLectureRepository.java @@ -0,0 +1,12 @@ +package com.example.majorLink.repository; + +import com.example.majorLink.domain.mapping.TutorLecture; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; +import java.util.UUID; + +public interface TutorLectureRepository extends JpaRepository { + Optional findByUserId(UUID id); + Optional findByLectureId(Long id); +} diff --git a/majorLink/src/main/java/com/example/majorLink/service/LectureService.java b/majorLink/src/main/java/com/example/majorLink/service/LectureService.java index c74b6ad..ae410b3 100644 --- a/majorLink/src/main/java/com/example/majorLink/service/LectureService.java +++ b/majorLink/src/main/java/com/example/majorLink/service/LectureService.java @@ -1,14 +1,26 @@ package com.example.majorLink.service; import com.example.majorLink.domain.Lecture; -import com.example.majorLink.domain.User; +import com.example.majorLink.domain.mapping.TuteeLecture; import com.example.majorLink.dto.request.LectureRequestDTO; import org.springframework.data.domain.Page; +import java.util.UUID; + public interface LectureService { - Lecture createLecture(User user, LectureRequestDTO request); - Lecture updateLecture(User user, Long lectureId, LectureRequestDTO request); - void DeleteLecture(User user, Long lectureId); + Lecture createLecture(UUID userId, LectureRequestDTO request); + Lecture updateLecture(UUID userId, LectureRequestDTO request, Long lectureId); + void DeleteLecture(UUID userId, Long lectureId); Page getLectureList(Integer page); -} + + TuteeLecture addLecture(UUID userId, Long lectureId); + void cancelLecture(UUID userId, Long lectureId); + + Boolean toggleLike(UUID userId, Long lectureId); + + Page getMostLikedLecture(Integer page); + Page getNewLecture(Integer page); + Page getMostRecruitedLecture(Integer page); + Page getLectureByCategory(Integer page, Long categoryId); +} \ No newline at end of file diff --git a/majorLink/src/main/java/com/example/majorLink/service/LectureServiceImpl.java b/majorLink/src/main/java/com/example/majorLink/service/LectureServiceImpl.java index a6cfcaf..c609dfd 100644 --- a/majorLink/src/main/java/com/example/majorLink/service/LectureServiceImpl.java +++ b/majorLink/src/main/java/com/example/majorLink/service/LectureServiceImpl.java @@ -2,19 +2,20 @@ import com.example.majorLink.domain.Category; import com.example.majorLink.domain.Lecture; -import com.example.majorLink.domain.Tag; import com.example.majorLink.domain.User; +import com.example.majorLink.domain.mapping.Liked; +import com.example.majorLink.domain.mapping.TuteeLecture; +import com.example.majorLink.domain.mapping.TutorLecture; import com.example.majorLink.dto.request.LectureRequestDTO; -import com.example.majorLink.repository.CategoryRepository; -import com.example.majorLink.repository.LectureRepository; -import com.example.majorLink.repository.TagRepository; -import com.example.majorLink.repository.UserRepository; +import com.example.majorLink.repository.*; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; +import java.util.UUID; + @Service @Transactional @RequiredArgsConstructor @@ -25,18 +26,20 @@ public class LectureServiceImpl implements LectureService { private final LectureRepository lectureRepository; private final UserRepository userRepository; private final CategoryRepository categoryRepository; - private final TagRepository tagRepository; + private final TutorLectureRepository tutorLectureRepository; + private final TuteeLectureRepository tuteeLectureRepository; + private final LikedRepository likedRepository; + // 강의 생성 @Override - public Lecture createLecture(User user, LectureRequestDTO request) { + public Lecture createLecture(UUID userId, LectureRequestDTO request) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); + // Category 엔티티 가져오기 Category category = categoryRepository.findById(request.getCategoryId()) .orElseThrow(() -> new IllegalArgumentException("Invalid category ID")); - // Tag 엔티티 가져오기 - Tag tag = tagRepository.findById(request.getTagId()) - .orElseThrow(() -> new IllegalArgumentException("Invalid tag ID")); - Lecture lecture = Lecture.builder() .name(request.getName()) .body(request.getBody()) @@ -44,29 +47,44 @@ public Lecture createLecture(User user, LectureRequestDTO request) { .info(request.getInfo()) .level(request.getLevel()) .pNum(request.getPNum()) + .curPNum(0) .time(request.getTime()) .day(request.getDay()) .startDate(request.getStartDate()) .exam(request.getExam()) .category(category) - .tag(tag) + .tag(request.getTag()) + .tutor(user.getNickname()) + .build(); + + Lecture saveLecture = lectureRepository.save(lecture); + + TutorLecture tutorLecture = TutorLecture.builder() .user(user) + .lecture(saveLecture) .build(); + tutorLectureRepository.save(tutorLecture); - return lectureRepository.save(lecture); + return saveLecture; } - + // 강의 수정 @Override - public Lecture updateLecture(User user, Long lectureId, LectureRequestDTO request) { + public Lecture updateLecture(UUID userId, LectureRequestDTO request, Long lectureId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); - // 기존의 Lecture 엔티티를 조회 - Lecture lecture = lectureRepository.findById(lectureId) + TutorLecture tutorLecture = tutorLectureRepository.findByLectureId(lectureId) .orElseThrow(() -> new IllegalArgumentException("Invalid lecture ID")); + // 기존의 Lecture 엔티티를 조회 + Lecture lecture = tutorLecture.getLecture(); + + UUID tutorId = tutorLecture.getUser().getId(); + // 요청한 사용자가 해당 강의를 개설한 사용자와 일치하는지 확인 - if (!lecture.getUser().getId().equals(user.getId())) { + if (!tutorId.equals(user.getId())) { throw new IllegalStateException("You do not have permission to update this lecture"); } @@ -74,10 +92,6 @@ public Lecture updateLecture(User user, Long lectureId, LectureRequestDTO reques Category category = categoryRepository.findById(request.getCategoryId()) .orElseThrow(() -> new IllegalArgumentException("Invalid category ID")); - // Tag 엔티티 가져오기 - Tag tag = tagRepository.findById(request.getTagId()) - .orElseThrow(() -> new IllegalArgumentException("Invalid tag ID")); - // Lecture 엔티티의 필드 업데이트 lecture.updateLecture( request.getName(), @@ -91,21 +105,28 @@ public Lecture updateLecture(User user, Long lectureId, LectureRequestDTO reques request.getStartDate(), request.getExam(), category, - tag + request.getTag() ); // Lecture 엔티티 저장 (변경 감지) return lectureRepository.save(lecture); } + // 강의 삭제 @Override - public void DeleteLecture(User user, Long lectureId) { - // 기존의 Lecture 엔티티를 조회 - Lecture lecture = lectureRepository.findById(lectureId) + public void DeleteLecture(UUID userId, Long lectureId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); + + TutorLecture tutorLecture = tutorLectureRepository.findByLectureId(lectureId) .orElseThrow(() -> new IllegalArgumentException("Invalid lecture ID")); + // 기존의 Lecture 엔티티를 조회 + Lecture lecture = tutorLecture.getLecture(); + UUID tutorId = tutorLecture.getUser().getId(); + // 요청한 사용자가 해당 강의를 개설한 사용자와 일치하는지 확인 - if (!lecture.getUser().getId().equals(user.getId())) { + if (!tutorId.equals(user.getId())) { throw new IllegalStateException("You do not have permission to delete this lecture"); } @@ -113,6 +134,7 @@ public void DeleteLecture(User user, Long lectureId) { lectureRepository.delete(lecture); } + // 강의 목록 조회 @Override public Page getLectureList(Integer page) { Page lecturePage = lectureRepository.findAll(PageRequest.of(page, 10)); @@ -120,5 +142,99 @@ public Page getLectureList(Integer page) { return lecturePage; } + // 강의 수강 신청 + @Override + public TuteeLecture addLecture(UUID userId, Long lectureId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); + + Lecture lecture = lectureRepository.findById(lectureId) + .orElseThrow(() -> new IllegalArgumentException("Invalid lecture ID")); + + if (lecture.getPNum() >= lecture.getCurPNum()) { + throw new IllegalStateException("The lecture is full"); + } + + TuteeLecture tuteeLecture = TuteeLecture.builder() + .user(user) + .lecture(lecture) + .build(); + + lecture.addCurPNum(); + + return tuteeLectureRepository.save(tuteeLecture); + } + + // 강의 수강 취소 + @Override + public void cancelLecture(UUID userId, Long lectureId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); + + Lecture lecture = lectureRepository.findById(lectureId) + .orElseThrow(() -> new IllegalArgumentException("Invalid lecture ID")); + + TuteeLecture tuteeLecture = tuteeLectureRepository.findByUserIdAndLectureId(userId, lectureId) + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID or lecture ID")); + + lecture.subCurPNum(); + + tuteeLectureRepository.delete(tuteeLecture); + } + + // 강의 좋아요 + @Override + public Boolean toggleLike(UUID userId, Long lectureId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); -} + Lecture lecture = lectureRepository.findById(lectureId) + .orElseThrow(() -> new IllegalArgumentException("Invalid lecture ID")); + + Boolean alreadyLiked = likedRepository.existsByUserAndLecture(user, lecture); + + if (alreadyLiked) { + // 좋아요 취소 + likedRepository.deleteByUserAndLecture(user, lecture); + return false; + } else { + // 좋아요 + Liked liked = Liked.builder() + .user(user) + .lecture(lecture) + .build(); + + likedRepository.save(liked); + + return true; + } + } + + // 좋아요 많은 강의 조회 + @Override + public Page getMostLikedLecture(Integer page) { + + return lectureRepository.orderByLikedCount(PageRequest.of(page, 10)); + } + + // 새로 등록된 클래스 조회 + @Override + public Page getNewLecture(Integer page) { + + return lectureRepository.orderByCreatedAt(PageRequest.of(page, 10)); + } + + // 모집인원 많은 클래스 조회 + @Override + public Page getMostRecruitedLecture(Integer page) { + + return lectureRepository.orderByCurPNum(PageRequest.of(page, 10)); + } + + // 카테고리별 클래스 조회 + @Override + public Page getLectureByCategory(Integer page, Long categoryId) { + + return lectureRepository.orderByCategoryId(categoryId, PageRequest.of(page, 10)); + } +} \ No newline at end of file diff --git a/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewService.java b/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewService.java index dd6ea5f..c1c21fd 100644 --- a/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewService.java +++ b/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewService.java @@ -10,8 +10,10 @@ public interface ReviewService { Review createReview(UUID user, Long lectureId, ReviewRequestDTO request); - Review updateReview(Long reviewId, ReviewRequestDTO request); - void deleteReview(Long reviewId); + Review updateReview(UUID userId, Long reviewId, ReviewRequestDTO request); + void deleteReview(UUID userId, Long reviewId); Page getReviewList(Long lectureId, Integer page); + + Review getReview(Long reviewId); } diff --git a/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewServiceImpl.java b/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewServiceImpl.java index a8f34ee..c8c8bbd 100644 --- a/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewServiceImpl.java +++ b/majorLink/src/main/java/com/example/majorLink/service/ReviewService/ReviewServiceImpl.java @@ -41,19 +41,29 @@ public Review createReview(UUID userId, Long lectureId, ReviewRequestDTO request // 리뷰 수정 @Override - public Review updateReview(Long reviewId, ReviewRequestDTO request) { + public Review updateReview(UUID userId, Long reviewId, ReviewRequestDTO request) { + User user = userRepository.findById(userId).get(); Review review = reviewRepository.findById(reviewId).get(); review.updateReview(request.getTitle(), request.getContent(), request.getRate()); + if (!review.getUser().getId().equals(user.getId())){ + throw new IllegalArgumentException("Invalid user ID"); + } + return reviewRepository.save(review); } // 리뷰 삭제 @Override - public void deleteReview(Long reviewId) { + public void deleteReview(UUID userId, Long reviewId) { + User user = userRepository.findById(userId).get(); Review review = reviewRepository.findById(reviewId).get(); + if (!review.getUser().getId().equals(user.getId())){ + throw new IllegalArgumentException("Invalid user ID"); + } + reviewRepository.delete(review); } @@ -66,4 +76,10 @@ public Page getReviewList(Long lectureId, Integer page) { return reviewPage; } + + // 리뷰 내용 조회 + @Override + public Review getReview(Long reviewId) { + return reviewRepository.findById(reviewId).get(); + } }