From 588758c581ffd040ba9d4f3d237ebd1ba38888e3 Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Mon, 22 Jul 2024 17:24:51 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Lecture=20=EC=9D=BC=EB=B6=80=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/controller/LectureController.java | 40 ++++++++++++----- .../lecture/service/LectureService.java | 2 + .../lecture/service/LectureServiceImpl.java | 45 +++++++++++-------- 3 files changed, 58 insertions(+), 29 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 6c4e0f8..5ba43cd 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 @@ -30,8 +30,15 @@ public class LectureController { public ResponseEntity createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + Lecture lecture = lectureService.findLectureByTitle(lectureCreateRequest.getTitle()); + if (lecture != null) { + String msg = new String("Duplicated Lecture"); + return new ResponseEntity<>(msg, HttpStatus.CONFLICT); + } + lectureService.createLecture(userId, lectureCreateRequest); - return new ResponseEntity<>(HttpStatus.CREATED); + String msg = new String("Lecture registered successfully"); + return new ResponseEntity<>(msg, HttpStatus.CREATED); } @PutMapping("/{lectureId}") @@ -39,17 +46,21 @@ public class LectureController { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); if (!lectureService.updateLecture(userId, lectureId, lectureCreateRequest)) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + String msg = new String("Can't update Lecture"); + return new ResponseEntity<>(msg, HttpStatus.UNAUTHORIZED); } - - return new ResponseEntity<>(HttpStatus.OK); + String msg = new String("Lecture updated successfully"); + return new ResponseEntity<>(msg, HttpStatus.OK); } @DeleteMapping("/{lectureId}") - public ResponseEntity deleteLecture(@RequestBody long userId, @PathVariable long lectureId) { + public ResponseEntity deleteLecture(@RequestHeader("Authorization") String accessToken, @PathVariable long lectureId) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + if (!lectureService.deleteLecture(userId, lectureId)) { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); + String msg = new String("Can't delete Lecture"); + return new ResponseEntity<>(msg, HttpStatus.UNAUTHORIZED); } return new ResponseEntity<>(HttpStatus.NO_CONTENT); @@ -60,7 +71,8 @@ public class LectureController { List lectures = lectureService.findAllLecture(); if (lectures.isEmpty()) { - return new ResponseEntity<>(HttpStatus.NO_CONTENT); + String msg = new String("No lectures found"); + return new ResponseEntity<>(msg, HttpStatus.OK); } return new ResponseEntity<>(lectures, HttpStatus.OK); @@ -77,22 +89,28 @@ public class LectureController { LectureDetailResponse lectureDetailResponse = lectureService.findLectureById(userId, lectureId); if (lectureDetailResponse == null) { - return new ResponseEntity<>(HttpStatus.NO_CONTENT); + String msg = new String("Can't find Lecture"); + return new ResponseEntity<>(msg, HttpStatus.OK); } - return new ResponseEntity<>(lectureDetailResponse, HttpStatus.OK); } @GetMapping("/mylecture") - public ResponseEntity findMyLecture(@RequestHeader("Authorization") String accessToken) { + public ResponseEntity findMyLecture(@RequestHeader(value = "Authorization", required = false) String accessToken) { + if (accessToken == null) { + String msg = new String("Not logged in"); + return new ResponseEntity<>(msg, HttpStatus.OK); + } + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); List myLectures = lectureService.findMyLecture(userId); if (myLectures.isEmpty()) { - return new ResponseEntity<>(HttpStatus.NO_CONTENT); + String msg = new String("No lectures found"); + return new ResponseEntity<>(msg, HttpStatus.OK); } return new ResponseEntity<>(myLectures, HttpStatus.OK); 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 e82bd25..88fc735 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 @@ -1,5 +1,6 @@ package com.edufocus.edufocus.lecture.service; +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; @@ -23,4 +24,5 @@ public interface LectureService { List findMyLecture(long userId); + Lecture findLectureByTitle(String title); } 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 1971488..56b33cf 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 @@ -15,6 +15,7 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Builder @Service @@ -47,11 +48,9 @@ public class LectureServiceImpl implements LectureService { @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()) { + if (lecture.getUser().getId() != userId) { return false; } @@ -80,9 +79,14 @@ public class LectureServiceImpl implements LectureService { @Override public boolean deleteLecture(long userId, long lectureId) { - Lecture lecture = lectureRepository.findById(lectureId).get(); + Optional lecture = lectureRepository.findById(lectureId); - if (lecture.getUser().getId() != userId) { + if (lecture.isEmpty()) { + return false; + } + lecture = Optional.of(lecture.get()); + + if (lecture.get().getUser().getId() != userId) { return false; } @@ -109,11 +113,12 @@ public class LectureServiceImpl implements LectureService { @Override public LectureDetailResponse findLectureById(Long userId, long lectureId) { - Lecture lecture = lectureRepository.findById(lectureId).get(); + Optional lecture = lectureRepository.findById(lectureId); - if (lecture == null) { + if (lecture.isEmpty()) { return null; } + lecture = Optional.of(lecture.get()); String userStatus; if (userId == null) { @@ -122,9 +127,9 @@ public class LectureServiceImpl implements LectureService { User user = userRepository.findById(userId).get(); if (user.getRole() == UserRole.ADMIN) { - if (lecture.getUser().getId() == user.getId()) { + if (lecture.get().getUser().getId() == user.getId()) { userStatus = String.valueOf(UserStatus.MANAGED_BY_ME); - } else{ + } else { userStatus = String.valueOf(UserStatus.MANAGED_BY_OTHERS); } } else { @@ -141,15 +146,15 @@ public class LectureServiceImpl implements LectureService { } LectureDetailResponse lectureDetailResponse = new LectureDetailResponse().builder() - .id(lecture.getId()) - .title(lecture.getTitle()) - .description(lecture.getDescription()) - .image(lecture.getImage()) - .startDate(lecture.getStartDate()) - .endDate(lecture.getEndDate()) - .plan(lecture.getPlan()) - .online(lecture.isOnline()) - .teacherName(lecture.getUser().getName()) + .id(lecture.get().getId()) + .title(lecture.get().getTitle()) + .description(lecture.get().getDescription()) + .image(lecture.get().getImage()) + .startDate(lecture.get().getStartDate()) + .endDate(lecture.get().getEndDate()) + .plan(lecture.get().getPlan()) + .online(lecture.get().isOnline()) + .teacherName(lecture.get().getUser().getName()) .status(userStatus) .build(); @@ -189,4 +194,8 @@ public class LectureServiceImpl implements LectureService { return myLectureList; } + @Override + public Lecture findLectureByTitle(String title) { + return lectureRepository.findByTitle(title); + } }