From c04142afd1e70f5d0ca99cfc0d2aae60de4363e8 Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Fri, 19 Jul 2024 13:44:32 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Lecture=20=EA=B0=95=EC=9D=98=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=88=98=EC=A0=95,=20=EB=82=B4=20?= =?UTF-8?q?=EA=B0=95=EC=9D=98=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/controller/LectureController.java | 14 ++++- .../edufocus/lecture/entity/Lecture.java | 8 +-- .../lecture/repository/LectureRepository.java | 6 +- .../lecture/service/LectureService.java | 2 + .../lecture/service/LectureServiceImpl.java | 58 ++++++++++++++----- 5 files changed, 68 insertions(+), 20 deletions(-) diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java index e532525..ec71557 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java @@ -1,5 +1,6 @@ package com.edufocus.edufocus.lecture.controller; +import com.edufocus.edufocus.lecture.entity.Lecture; import com.edufocus.edufocus.lecture.entity.LectureCreateRequest; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse; import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; @@ -24,7 +25,6 @@ public class LectureController { private final LectureService lectureService; private final JWTUtil jwtUtil; - private final UserService userService; @PostMapping public ResponseEntity createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) { @@ -34,6 +34,18 @@ public class LectureController { return new ResponseEntity<>(HttpStatus.CREATED); } + @PutMapping("/{lectureId}") + public ResponseEntity updateLecture(@RequestHeader("Authorization") String accessToken, @PathVariable Long lectureId, @RequestBody LectureCreateRequest lectureCreateRequest) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + + if (!lectureService.updateLecture(userId, lectureId, lectureCreateRequest)) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + return new ResponseEntity<>(HttpStatus.OK); + } + + @DeleteMapping("/{lectureId}") public ResponseEntity deleteLecture(@RequestBody long userId, @PathVariable long lectureId) { if (!lectureService.deleteLecture(userId, lectureId)) { diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java index d2f220b..24adc42 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java @@ -3,16 +3,16 @@ package com.edufocus.edufocus.lecture.entity; import com.edufocus.edufocus.user.model.entity.User; import jakarta.persistence.*; -import lombok.Getter; -import lombok.Setter; +import lombok.*; -import java.util.ArrayList; import java.util.Date; -import java.util.List; @Entity @Getter @Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor public class Lecture { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java b/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java index 431b96a..a8615d2 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java @@ -10,8 +10,10 @@ import java.util.List; @Repository public interface LectureRepository extends JpaRepository { - Lecture findByTitle(@Param("title") String title); + Lecture findByTitle(String title); - List findAllByUserId(@Param("userId") Long userId); + List findAllByUserId(Long userId); + + List findLecturesByUserId(Long userId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java index b359aa0..29eb908 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java @@ -13,6 +13,8 @@ public interface LectureService { void createLecture(long userId, LectureCreateRequest lectureCreateRequest); + boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest); + boolean deleteLecture(long userId, long LectureId); List findAllLecture(); diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java index 9f9eec8..1e808ac 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java @@ -8,6 +8,7 @@ import com.edufocus.edufocus.lecture.repository.LectureRepository; import com.edufocus.edufocus.registration.entity.Registration; import com.edufocus.edufocus.registration.repository.RegistrationRepository; import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.entity.UserRole; import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.Builder; @@ -31,22 +32,54 @@ public class LectureServiceImpl implements LectureService { @Override public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) { - User user = userRepository.findById(userId).get(); - Lecture lecture = new Lecture(); - lecture.setUser(user); - - lecture.setTitle(lectureCreateRequest.getTitle()); - lecture.setDescription(lectureCreateRequest.getDescription()); - lecture.setImage(lectureCreateRequest.getImage()); - lecture.setStartDate(lectureCreateRequest.getStartDate()); - lecture.setEndDate(lectureCreateRequest.getEndDate()); - lecture.setPlan(lectureCreateRequest.getPlan()); + Lecture lecture = new Lecture().builder() + .user(user) + .title(lectureCreateRequest.getTitle()) + .description(lectureCreateRequest.getDescription()) + .image(lectureCreateRequest.getImage()) + .startDate(lectureCreateRequest.getStartDate()) + .endDate(lectureCreateRequest.getEndDate()) + .plan(lectureCreateRequest.getPlan()) + .build(); lectureRepository.save(lecture); } + @Override + public boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest) { + User user = userRepository.findById(userId).get(); + + Lecture lecture = lectureRepository.findById(lectureId).get(); + + if (lecture.getUser().getId() != user.getId()) { + return false; + } + + if (lectureCreateRequest.getTitle() != null) { + lecture.setTitle(lectureCreateRequest.getTitle()); + } + if (lectureCreateRequest.getDescription() != null) { + lecture.setDescription(lectureCreateRequest.getDescription()); + } + if (lectureCreateRequest.getImage() != null) { + lecture.setImage(lectureCreateRequest.getImage()); + } + if (lectureCreateRequest.getStartDate() != null) { + lecture.setStartDate(lectureCreateRequest.getStartDate()); + } + if (lectureCreateRequest.getEndDate() != null) { + lecture.setEndDate(lectureCreateRequest.getEndDate()); + } + if (lectureCreateRequest.getPlan() != null) { + lecture.setPlan(lectureCreateRequest.getPlan()); + } + + lectureRepository.save(lecture); + return true; + } + @Override public boolean deleteLecture(long userId, long lectureId) { Lecture lecture = lectureRepository.findById(lectureId).get(); @@ -105,9 +138,8 @@ public class LectureServiceImpl implements LectureService { List myLectureList = new ArrayList<>(); - if (user.getRole().equals("ADMIN")) { - List lectureList = lectureRepository.findAllByUserId(userId); - + if (user.getRole() == UserRole.ADMIN) { + List lectureList = lectureRepository.findLecturesByUserId(userId); for (Lecture lecture : lectureList) { LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder() .id(lecture.getId())