refactor: Lecture 일부 코드 리팩토링

This commit is contained in:
kgc9007 2024-07-22 17:24:51 +09:00
parent 10828ff11e
commit 588758c581
3 changed files with 58 additions and 29 deletions

View File

@ -30,8 +30,15 @@ public class LectureController {
public ResponseEntity<?> createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) { public ResponseEntity<?> createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); 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); lectureService.createLecture(userId, lectureCreateRequest);
return new ResponseEntity<>(HttpStatus.CREATED); String msg = new String("Lecture registered successfully");
return new ResponseEntity<>(msg, HttpStatus.CREATED);
} }
@PutMapping("/{lectureId}") @PutMapping("/{lectureId}")
@ -39,17 +46,21 @@ public class LectureController {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
if (!lectureService.updateLecture(userId, lectureId, lectureCreateRequest)) { 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);
} }
String msg = new String("Lecture updated successfully");
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(msg, HttpStatus.OK);
} }
@DeleteMapping("/{lectureId}") @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)) { 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); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
@ -60,7 +71,8 @@ public class LectureController {
List<LectureSearchResponse> lectures = lectureService.findAllLecture(); List<LectureSearchResponse> lectures = lectureService.findAllLecture();
if (lectures.isEmpty()) { 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); return new ResponseEntity<>(lectures, HttpStatus.OK);
@ -77,22 +89,28 @@ public class LectureController {
LectureDetailResponse lectureDetailResponse = lectureService.findLectureById(userId, lectureId); LectureDetailResponse lectureDetailResponse = lectureService.findLectureById(userId, lectureId);
if (lectureDetailResponse == null) { 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); return new ResponseEntity<>(lectureDetailResponse, HttpStatus.OK);
} }
@GetMapping("/mylecture") @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)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
List<LectureSearchResponse> myLectures = lectureService.findMyLecture(userId); List<LectureSearchResponse> myLectures = lectureService.findMyLecture(userId);
if (myLectures.isEmpty()) { 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); return new ResponseEntity<>(myLectures, HttpStatus.OK);

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.lecture.service; 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.LectureCreateRequest;
import com.edufocus.edufocus.lecture.entity.LectureSearchResponse; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
@ -23,4 +24,5 @@ public interface LectureService {
List<LectureSearchResponse> findMyLecture(long userId); List<LectureSearchResponse> findMyLecture(long userId);
Lecture findLectureByTitle(String title);
} }

View File

@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
@Builder @Builder
@Service @Service
@ -47,11 +48,9 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest) { public boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest) {
User user = userRepository.findById(userId).get();
Lecture lecture = lectureRepository.findById(lectureId).get(); Lecture lecture = lectureRepository.findById(lectureId).get();
if (lecture.getUser().getId() != user.getId()) { if (lecture.getUser().getId() != userId) {
return false; return false;
} }
@ -80,9 +79,14 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public boolean deleteLecture(long userId, long lectureId) { public boolean deleteLecture(long userId, long lectureId) {
Lecture lecture = lectureRepository.findById(lectureId).get(); Optional<Lecture> 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; return false;
} }
@ -109,11 +113,12 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public LectureDetailResponse findLectureById(Long userId, long lectureId) { public LectureDetailResponse findLectureById(Long userId, long lectureId) {
Lecture lecture = lectureRepository.findById(lectureId).get(); Optional<Lecture> lecture = lectureRepository.findById(lectureId);
if (lecture == null) { if (lecture.isEmpty()) {
return null; return null;
} }
lecture = Optional.of(lecture.get());
String userStatus; String userStatus;
if (userId == null) { if (userId == null) {
@ -122,9 +127,9 @@ public class LectureServiceImpl implements LectureService {
User user = userRepository.findById(userId).get(); User user = userRepository.findById(userId).get();
if (user.getRole() == UserRole.ADMIN) { 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); userStatus = String.valueOf(UserStatus.MANAGED_BY_ME);
} else{ } else {
userStatus = String.valueOf(UserStatus.MANAGED_BY_OTHERS); userStatus = String.valueOf(UserStatus.MANAGED_BY_OTHERS);
} }
} else { } else {
@ -141,15 +146,15 @@ public class LectureServiceImpl implements LectureService {
} }
LectureDetailResponse lectureDetailResponse = new LectureDetailResponse().builder() LectureDetailResponse lectureDetailResponse = new LectureDetailResponse().builder()
.id(lecture.getId()) .id(lecture.get().getId())
.title(lecture.getTitle()) .title(lecture.get().getTitle())
.description(lecture.getDescription()) .description(lecture.get().getDescription())
.image(lecture.getImage()) .image(lecture.get().getImage())
.startDate(lecture.getStartDate()) .startDate(lecture.get().getStartDate())
.endDate(lecture.getEndDate()) .endDate(lecture.get().getEndDate())
.plan(lecture.getPlan()) .plan(lecture.get().getPlan())
.online(lecture.isOnline()) .online(lecture.get().isOnline())
.teacherName(lecture.getUser().getName()) .teacherName(lecture.get().getUser().getName())
.status(userStatus) .status(userStatus)
.build(); .build();
@ -189,4 +194,8 @@ public class LectureServiceImpl implements LectureService {
return myLectureList; return myLectureList;
} }
@Override
public Lecture findLectureByTitle(String title) {
return lectureRepository.findByTitle(title);
}
} }