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 bd013a4..e532525 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,8 +1,13 @@ package com.edufocus.edufocus.lecture.controller; -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.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.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -18,16 +23,19 @@ import java.util.List; public class LectureController { private final LectureService lectureService; + private final JWTUtil jwtUtil; + private final UserService userService; @PostMapping - public ResponseEntity createLecture (@RequestBody long userId, LectureRegist lectureRegist) { - System.out.println("@@@@@@@@@@@@@@@@@@>>>>>>>>>>>>>>>>>>>>>> "+userId); - lectureService.createLecture(userId, lectureRegist); + public ResponseEntity createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + + lectureService.createLecture(userId, lectureCreateRequest); return new ResponseEntity<>(HttpStatus.CREATED); } @DeleteMapping("/{lectureId}") - public ResponseEntity deleteLecture (@RequestBody long userId, @PathVariable long lectureId) { + public ResponseEntity deleteLecture(@RequestBody long userId, @PathVariable long lectureId) { if (!lectureService.deleteLecture(userId, lectureId)) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } @@ -36,8 +44,8 @@ public class LectureController { } @GetMapping - public ResponseEntity findAllLecture () { - List lectures = lectureService.findAllLecture(); + public ResponseEntity findAllLecture() { + List lectures = lectureService.findAllLecture(); if (lectures.isEmpty()) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); @@ -46,17 +54,27 @@ public class LectureController { return new ResponseEntity<>(lectures, HttpStatus.OK); } - @GetMapping("/title/{title}") - public ResponseEntity findByTitle (@PathVariable String title) { - Lecture lecture = lectureService.findLectureByTitle(title); + @GetMapping("/{lectureId}") + public ResponseEntity findById(@PathVariable long lectureId) { + LectureDetailResponse lectureDetailResponse = lectureService.findLectureById(lectureId); - if (lecture == null) { + if (lectureDetailResponse == null) { 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 myLectures = lectureService.findMyLecture(userId); + if (myLectures.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + return new ResponseEntity<>(myLectures, HttpStatus.OK); + } } 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 8fd58e6..d2f220b 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 @@ -29,6 +29,9 @@ public class Lecture { @Lob private String description; + @Column + private String image; + @Column(name = "start_date") @Temporal(TemporalType.DATE) private Date startDate; @@ -43,6 +46,4 @@ public class Lecture { @Column private boolean online; - - } diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java similarity index 70% rename from backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java rename to backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java index 4486854..84c2181 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java @@ -1,6 +1,5 @@ package com.edufocus.edufocus.lecture.entity; -import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -10,18 +9,16 @@ import java.util.Date; @Getter @NoArgsConstructor @AllArgsConstructor -public class LectureRegist { - - private Long userId; +public class LectureCreateRequest { private String title; private String description; - @Temporal(TemporalType.DATE) + private String image; + private Date startDate; - @Temporal(TemporalType.DATE) private Date endDate; private String plan; diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureDetailResponse.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureDetailResponse.java new file mode 100644 index 0000000..08f9558 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureDetailResponse.java @@ -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; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureSearchResponse.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureSearchResponse.java new file mode 100644 index 0000000..08bf38d --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureSearchResponse.java @@ -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; +} 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 6366507..431b96a 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 @@ -12,4 +12,6 @@ import java.util.List; public interface LectureRepository extends JpaRepository { Lecture findByTitle(@Param("title") String title); + List findAllByUserId(@Param("userId") 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 2731e5a..b359aa0 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,7 +1,8 @@ package com.edufocus.edufocus.lecture.service; -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 org.springframework.stereotype.Service; import java.util.List; @@ -10,12 +11,14 @@ import java.util.List; @Service public interface LectureService { - void createLecture(long userId, LectureRegist lectureRegist); + void createLecture(long userId, LectureCreateRequest lectureCreateRequest); boolean deleteLecture(long userId, long LectureId); - List findAllLecture(); + List findAllLecture(); - Lecture findLectureByTitle(String title); + LectureDetailResponse findLectureById(long lectureId); + + List findMyLecture(long userId); } 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 4f120d8..9f9eec8 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 @@ -1,16 +1,23 @@ package com.edufocus.edufocus.lecture.service; 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.registration.entity.Registration; +import com.edufocus.edufocus.registration.repository.RegistrationRepository; import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; +import lombok.Builder; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; +@Builder @Service @Transactional @RequiredArgsConstructor @@ -20,19 +27,22 @@ public class LectureServiceImpl implements LectureService { private final UserRepository userRepository; + private final RegistrationRepository registrationRepository; + @Override - public void createLecture(long userId, LectureRegist lectureRegist) { + public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) { User user = userRepository.findById(userId).get(); Lecture lecture = new Lecture(); lecture.setUser(user); - lecture.setTitle(lectureRegist.getTitle()); - lecture.setDescription(lectureRegist.getDescription()); - lecture.setStartDate(lectureRegist.getStartDate()); - lecture.setEndDate(lectureRegist.getEndDate()); - lecture.setPlan(lectureRegist.getPlan()); + lecture.setTitle(lectureCreateRequest.getTitle()); + lecture.setDescription(lectureCreateRequest.getDescription()); + lecture.setImage(lectureCreateRequest.getImage()); + lecture.setStartDate(lectureCreateRequest.getStartDate()); + lecture.setEndDate(lectureCreateRequest.getEndDate()); + lecture.setPlan(lectureCreateRequest.getPlan()); lectureRepository.save(lecture); } @@ -50,14 +60,78 @@ public class LectureServiceImpl implements LectureService { } @Override - public List findAllLecture() { - return lectureRepository.findAll(); + public List findAllLecture() { + List lectureList = lectureRepository.findAll(); + + List 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 - public Lecture findLectureByTitle(String title) { - return lectureRepository.findByTitle(title); + public LectureDetailResponse findLectureById(long lectureId) { + + 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 findMyLecture(long userId) { + User user = userRepository.findById(userId).get(); + + List myLectureList = new ArrayList<>(); + + if (user.getRole().equals("ADMIN")) { + List 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 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; + } } diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/controller/QnaController.java b/backend/src/main/java/com/edufocus/edufocus/qna/controller/QnaController.java index fb3356f..0a4c1d2 100644 --- a/backend/src/main/java/com/edufocus/edufocus/qna/controller/QnaController.java +++ b/backend/src/main/java/com/edufocus/edufocus/qna/controller/QnaController.java @@ -2,6 +2,8 @@ package com.edufocus.edufocus.qna.controller; import com.edufocus.edufocus.qna.entity.Qna; import com.edufocus.edufocus.qna.service.QnaService; +import com.edufocus.edufocus.user.util.JWTUtil; +import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import okhttp3.Response; @@ -18,12 +20,17 @@ import java.util.List; @RequiredArgsConstructor public class QnaController { private final QnaService qnaService; - + private final JWTUtil jwtUtil; @PostMapping - public ResponseEntity createQna(@RequestBody Qna qna) { + public ResponseEntity createQna(@RequestBody Qna qna , HttpServletRequest request) { + + try{ - qnaService.createQna(qna); + String token = request.getHeader("Authorization"); + Long userId = Long.parseLong(jwtUtil.getUserId(token)); + + qnaService.createQna(userId,qna); return new ResponseEntity<>(qna, HttpStatus.CREATED); }catch (Exception e){ diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaService.java b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaService.java index a1b8d94..8acf0f5 100644 --- a/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaService.java +++ b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaService.java @@ -11,7 +11,7 @@ import java.util.List; @Service public interface QnaService { - void createQna(Qna qna) throws SQLException; + void createQna(Long id,Qna qna) throws SQLException; void updateQna(Long id,Qna qna) throws SQLException; void deleteQna(Long id) throws SQLException; Qna getQna(Long id) throws SQLException; diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaServiceImpl.java index 18a59c1..4e6cdc2 100644 --- a/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaServiceImpl.java @@ -20,9 +20,10 @@ public class QnaServiceImpl implements QnaService{ @Override - public void createQna(Qna qna) { + public void createQna(Long id,Qna qna) { + qna.setId(id); qnaRepository.save(qna); } diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java b/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java index f8dc7db..34358a2 100644 --- a/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java @@ -5,7 +5,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface RegistrationRepository extends JpaRepository { - + List findAllByUserId(@Param("userId") Long userId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java index 9689880..16e8b9f 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java @@ -5,7 +5,9 @@ import com.edufocus.edufocus.user.model.service.UserService; import com.edufocus.edufocus.user.util.JWTUtil; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -48,7 +50,7 @@ public class UserController { @Operation(summary = "로그인", description = "아이디와 비밀번호를 이용하여 로그인 처리.") @PostMapping("/login") public ResponseEntity> login( - @RequestBody @Parameter(description = "로그인 시 필요한 회원정보(아이디, 비밀번호).", required = true) User user) { + @RequestBody @Parameter(description = "로그인 시 필요한 회원정보(아이디, 비밀번호).", required = true) User user, HttpServletResponse response) { Map resultMap = new HashMap<>(); HttpStatus status = HttpStatus.ACCEPTED; try { @@ -63,8 +65,18 @@ public class UserController { // JSON 으로 token 전달. System.out.println(accessToken); resultMap.put("access-token", accessToken); - resultMap.put("refresh-token", refreshToken); + // resultMap.put("refresh-token", refreshToken); + // 쿠키 저장 + Cookie refreshCookie = new Cookie("refresh-token", refreshToken); + refreshCookie.setPath("/"); + refreshCookie.setHttpOnly(true); + refreshCookie.setSecure(true); // HTTPS에서만 전송되도록 설정 + // refreshCookie.setSameSite(Cookie.SameSite.NONE); // Cross-Origin 요청에 대해 모두 전송 + + response.addCookie(refreshCookie); + + // 쿠키저장 status = HttpStatus.CREATED; } else { resultMap.put("message", "아이디 또는 패스워드를 확인해 주세요."); @@ -78,7 +90,7 @@ public class UserController { } @Operation(summary = "회원인증", description = "회원 정보를 담은 Token 을 반환한다.") - @GetMapping("/info/{userId}") + @GetMapping("/auth/{userId}") public ResponseEntity> getInfo( @PathVariable("userId") @Parameter(description = "인증할 회원의 아이디.", required = true) Long userId, HttpServletRequest request) { @@ -129,19 +141,62 @@ public class UserController { @Operation(summary = "Access Token 재발급", description = "만료된 access token 을 재발급 받는다.") @PostMapping("/refresh") - public ResponseEntity refreshToken(@RequestBody User user, HttpServletRequest request) + public ResponseEntity refreshToken(HttpServletRequest request,HttpServletResponse response) throws Exception { + + // System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@"); + // System.out.println(user.getUserId()); Map resultMap = new HashMap<>(); HttpStatus status = HttpStatus.ACCEPTED; - String token = request.getHeader("refreshToken"); - log.debug("token : {}, memberDto : {}", token, user); + + // String token = request.getHeader("refreshToken"); + + + Cookie[] cookies = request.getCookies(); + String token = null; + + if (cookies != null) { + for (Cookie cookie : cookies) { + if (cookie.getName().equals("refresh-token")) { + token = cookie.getValue(); + break; + } + } + } + Long userId = Long.parseLong(jwtUtil.getUserId(token)); + /// System.out.println(userId); + /// System.out.println("리프레쉬 토큰 (쿠키에서 받은거 "+token); + // log.debug("token : {}, memberDto : {}", token, user); + // System.out.println(jwtUtil.checkToken(token)); if (jwtUtil.checkToken(token)) { - if (token.equals(userService.getRefreshToken(user.getId()))) { - String accessToken = jwtUtil.createAccessToken(String.valueOf(user.getId())); + + // System.out.println(token); + // System.out.println(userService.getRefreshToken(userId)); + //System.out.println("쿠키 토큰 , 디비 토큰 비교"+token.equals(userService.getRefreshToken(userId))); + if (token.equals(userService.getRefreshToken(userId))) { + System.out.println("!!"); + String accessToken = jwtUtil.createAccessToken(String.valueOf(userId)); + String refreshToken = jwtUtil.createRefreshToken(String.valueOf(userId)); + log.debug("token : {}", accessToken); log.debug("정상적으로 access token 재발급!!!"); resultMap.put("access-token", accessToken); + + + // 바뀐 리프레시db저장 + userService.saveRefreshToken(userId,refreshToken); + // 쿠키 저장 + Cookie refreshCookie = new Cookie("refresh-token", refreshToken); + refreshCookie.setPath("/"); + refreshCookie.setHttpOnly(true); + refreshCookie.setSecure(true); // HTTPS에서만 전송되도록 설정 + // refreshCookie.setSameSite(Cookie.SameSite.NONE); // Cross-Origin 요청에 대해 모두 전송 + + response.addCookie(refreshCookie); + System.out.println("바뀐 리프레쉬랑 지금꺼 비교 "+ refreshToken.equals(token)); + resultMap.put("access-token", accessToken); status = HttpStatus.CREATED; + } } else { log.debug("refresh token 도 사용 불가!!!!!!!"); @@ -151,7 +206,7 @@ public class UserController { } @Operation(summary = "회원 정보 조회", description = "토큰을 이용하여 회원 정보를 조회한다.") - @GetMapping("/member") + @GetMapping("/userinfo") public ResponseEntity> getMember(HttpServletRequest request) { Map resultMap = new HashMap<>(); HttpStatus status = HttpStatus.ACCEPTED; diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java index 69a8b96..8bc7952 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java @@ -30,9 +30,10 @@ public class User { private String password; @Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL private UserRole role; - private String refreshToken; + private String name; +