Merge pull request #83 from TeamBNBN/be/Lecture
refactor: Lecture 일부 코드 리팩토링
This commit is contained in:
commit
9465ae0307
@ -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<LectureSearchResponse> 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<LectureSearchResponse> 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);
|
||||
|
@ -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<LectureSearchResponse> findMyLecture(long userId);
|
||||
|
||||
Lecture findLectureByTitle(String title);
|
||||
}
|
||||
|
@ -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> 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> 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user