feat: Lecture 조회 기능 추가

강의 전체조회, 상세조회, 내 강의 조회 기능 추가
This commit is contained in:
kgc9007 2024-07-19 09:58:22 +09:00
parent 6dbfc7f5af
commit afa5f494aa
9 changed files with 186 additions and 39 deletions

View File

@ -1,8 +1,13 @@
package com.edufocus.edufocus.lecture.controller; 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.LectureRegist; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import com.edufocus.edufocus.lecture.service.LectureService; import com.edufocus.edufocus.lecture.service.LectureService;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.service.UserService;
import com.edufocus.edufocus.user.model.service.UserServiceImpl;
import com.edufocus.edufocus.user.util.JWTUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -18,13 +23,14 @@ import java.util.List;
public class LectureController { public class LectureController {
private final LectureService lectureService; private final LectureService lectureService;
private final JWTUtil jwtUtil;
private final UserService userService;
@PostMapping @PostMapping
public ResponseEntity<?> createLecture (@RequestBody long userId, LectureRegist lectureRegist) { public ResponseEntity<?> createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) {
System.out.println("@@@@@@@@@@@@@@@@@@>>>>>>>>>>>>>>>>>>>>>> "+userId); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
// 여기서 id 로직 lectureService.createLecture(userId, lectureCreateRequest);
lectureService.createLecture(userId, lectureRegist);
return new ResponseEntity<>(HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.CREATED);
} }
@ -39,7 +45,7 @@ public class LectureController {
@GetMapping @GetMapping
public ResponseEntity<?> findAllLecture() { public ResponseEntity<?> findAllLecture() {
List<Lecture> lectures = lectureService.findAllLecture(); List<LectureSearchResponse> lectures = lectureService.findAllLecture();
if (lectures.isEmpty()) { if (lectures.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
@ -48,17 +54,27 @@ public class LectureController {
return new ResponseEntity<>(lectures, HttpStatus.OK); return new ResponseEntity<>(lectures, HttpStatus.OK);
} }
@GetMapping("/title/{title}") @GetMapping("/{lectureId}")
public ResponseEntity<?> findByTitle (@PathVariable String title) { public ResponseEntity<?> findById(@PathVariable long lectureId) {
Lecture lecture = lectureService.findLectureByTitle(title); LectureDetailResponse lectureDetailResponse = lectureService.findLectureById(lectureId);
if (lecture == null) { if (lectureDetailResponse == null) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} }
return new ResponseEntity<>(lecture, HttpStatus.OK); return new ResponseEntity<>(lectureDetailResponse, HttpStatus.OK);
} }
@GetMapping("/mylecture")
public ResponseEntity<?> findMyLecture(@RequestHeader("Authorization") String accessToken) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
List<LectureSearchResponse> myLectures = lectureService.findMyLecture(userId);
if (myLectures.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(myLectures, HttpStatus.OK);
}
} }

View File

@ -29,6 +29,9 @@ public class Lecture {
@Lob @Lob
private String description; private String description;
@Column
private String image;
@Column(name = "start_date") @Column(name = "start_date")
@Temporal(TemporalType.DATE) @Temporal(TemporalType.DATE)
private Date startDate; private Date startDate;
@ -43,6 +46,4 @@ public class Lecture {
@Column @Column
private boolean online; private boolean online;
} }

View File

@ -1,6 +1,5 @@
package com.edufocus.edufocus.lecture.entity; package com.edufocus.edufocus.lecture.entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -10,18 +9,16 @@ import java.util.Date;
@Getter @Getter
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class LectureRegist { public class LectureCreateRequest {
private Long userId;
private String title; private String title;
private String description; private String description;
@Temporal(TemporalType.DATE) private String image;
private Date startDate; private Date startDate;
@Temporal(TemporalType.DATE)
private Date endDate; private Date endDate;
private String plan; private String plan;

View File

@ -0,0 +1,33 @@
package com.edufocus.edufocus.lecture.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.Date;
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LectureDetailResponse {
private Long id;
private String teacherName;
private String title;
private String description;
private String image;
private Date startDate;
private Date endDate;
private String plan;
private boolean online;
}

View File

@ -0,0 +1,19 @@
package com.edufocus.edufocus.lecture.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LectureSearchResponse {
private Long id;
private String title;
private String image;
}

View File

@ -12,4 +12,6 @@ import java.util.List;
public interface LectureRepository extends JpaRepository<Lecture, Long> { public interface LectureRepository extends JpaRepository<Lecture, Long> {
Lecture findByTitle(@Param("title") String title); Lecture findByTitle(@Param("title") String title);
List<Lecture> findAllByUserId(@Param("userId") Long userId);
} }

View File

@ -1,7 +1,8 @@
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.LectureRegist; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -10,12 +11,14 @@ import java.util.List;
@Service @Service
public interface LectureService { public interface LectureService {
void createLecture(long userId, LectureRegist lectureRegist); void createLecture(long userId, LectureCreateRequest lectureCreateRequest);
boolean deleteLecture(long userId, long LectureId); boolean deleteLecture(long userId, long LectureId);
List<Lecture> findAllLecture(); List<LectureSearchResponse> findAllLecture();
Lecture findLectureByTitle(String title); LectureDetailResponse findLectureById(long lectureId);
List<LectureSearchResponse> findMyLecture(long userId);
} }

View File

@ -1,16 +1,23 @@
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.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureRegist; import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import com.edufocus.edufocus.lecture.repository.LectureRepository; 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.User;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.Builder;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Builder
@Service @Service
@Transactional @Transactional
@RequiredArgsConstructor @RequiredArgsConstructor
@ -20,19 +27,22 @@ public class LectureServiceImpl implements LectureService {
private final UserRepository userRepository; private final UserRepository userRepository;
private final RegistrationRepository registrationRepository;
@Override @Override
public void createLecture(long userId, LectureRegist lectureRegist) { public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) {
User user = userRepository.findById(userId).get(); User user = userRepository.findById(userId).get();
Lecture lecture = new Lecture(); Lecture lecture = new Lecture();
lecture.setUser(user); lecture.setUser(user);
lecture.setTitle(lectureRegist.getTitle()); lecture.setTitle(lectureCreateRequest.getTitle());
lecture.setDescription(lectureRegist.getDescription()); lecture.setDescription(lectureCreateRequest.getDescription());
lecture.setStartDate(lectureRegist.getStartDate()); lecture.setImage(lectureCreateRequest.getImage());
lecture.setEndDate(lectureRegist.getEndDate()); lecture.setStartDate(lectureCreateRequest.getStartDate());
lecture.setPlan(lectureRegist.getPlan()); lecture.setEndDate(lectureCreateRequest.getEndDate());
lecture.setPlan(lectureCreateRequest.getPlan());
lectureRepository.save(lecture); lectureRepository.save(lecture);
} }
@ -50,14 +60,78 @@ public class LectureServiceImpl implements LectureService {
} }
@Override @Override
public List<Lecture> findAllLecture() { public List<LectureSearchResponse> findAllLecture() {
return lectureRepository.findAll(); List<Lecture> lectureList = lectureRepository.findAll();
List<LectureSearchResponse> lectureSearchResponseList = new ArrayList<>();
for (Lecture lecture : lectureList) {
LectureSearchResponse lectureSearchResponse = LectureSearchResponse.builder()
.id(lecture.getId())
.title(lecture.getTitle())
.image(lecture.getImage()).build();
lectureSearchResponseList.add(lectureSearchResponse);
}
return lectureSearchResponseList;
} }
@Override @Override
public Lecture findLectureByTitle(String title) { public LectureDetailResponse findLectureById(long lectureId) {
return lectureRepository.findByTitle(title);
Lecture lecture = lectureRepository.findById(lectureId).get();
if (lecture == null) {
return null;
} }
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())
.build();
return lectureDetailResponse;
}
public List<LectureSearchResponse> findMyLecture(long userId) {
User user = userRepository.findById(userId).get();
List<LectureSearchResponse> myLectureList = new ArrayList<>();
if (user.getRole().equals("ADMIN")) {
List<Lecture> lectureList = lectureRepository.findAllByUserId(userId);
for (Lecture lecture : lectureList) {
LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder()
.id(lecture.getId())
.title(lecture.getTitle())
.image(lecture.getImage()).build();
myLectureList.add(lectureSearchResponse);
}
} else {
List<Registration> registrationList = registrationRepository.findAllByUserId(userId);
for (Registration registration : registrationList) {
Lecture lecture = registration.getLecture();
LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder()
.id(lecture.getId())
.title(lecture.getTitle())
.image(lecture.getImage()).build();
myLectureList.add(lectureSearchResponse);
}
}
return myLectureList;
}
} }

View File

@ -5,7 +5,9 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
public interface RegistrationRepository extends JpaRepository<Registration, Long> { public interface RegistrationRepository extends JpaRepository<Registration, Long> {
List<Registration> findAllByUserId(@Param("userId") Long userId);
} }