From c3c574baf31c9112c95370dd94a91f0666fd6665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=A0=95=EB=AF=BC?= Date: Wed, 7 Aug 2024 15:30:54 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20qna=20=EB=8B=B5=EB=B3=80=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../edufocus/qna/service/QnaServiceImpl.java | 24 +++++++------- .../report/service/ReportServiceImpl.java | 2 +- .../user/model/service/UserServiceImpl.java | 31 +++++++++---------- 3 files changed, 28 insertions(+), 29 deletions(-) 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 80ea7ae..dbc6d84 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 @@ -7,6 +7,7 @@ import com.edufocus.edufocus.qna.entity.QnaRequestDto; import com.edufocus.edufocus.qna.entity.QnaResponseDto; import com.edufocus.edufocus.qna.repository.QnaRepository; import com.edufocus.edufocus.user.model.entity.vo.User; +import com.edufocus.edufocus.user.model.entity.vo.UserRole; import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; @@ -24,14 +25,13 @@ import java.util.stream.Collectors; @Service @Transactional @RequiredArgsConstructor -public class QnaServiceImpl implements QnaService{ +public class QnaServiceImpl implements QnaService { private final QnaRepository qnaRepository; private final LectureRepository lectureRepository; private final UserRepository userRepository; - @Override public QnaResponseDto createQna(Long id, QnaRequestDto qnaRequestDto, Long lecture_id) { @@ -41,19 +41,21 @@ public class QnaServiceImpl implements QnaService{ User user = userRepository.findById(id).orElse(null); - Qna qna = QnaRequestDto.toEntity(qnaRequestDto); + if (qna.getAnswer() != null || user.getRole() != UserRole.ADMIN) { + throw new RuntimeException(); + } qna.setLecture(lecture); qna.setUser(user); qna.setCreatedAt(new Date()); qnaRepository.save(qna); - return QnaResponseDto.toEntity(qna); + return QnaResponseDto.toEntity(qna); } @Override - public QnaResponseDto updateQna(Long id,QnaRequestDto qnaRequestDto) { + public QnaResponseDto updateQna(Long id, QnaRequestDto qnaRequestDto) { Qna findQna = qnaRepository.findById(id) @@ -65,14 +67,14 @@ public class QnaServiceImpl implements QnaService{ qnaRepository.save(findQna); - return QnaResponseDto.toEntity(findQna); + return QnaResponseDto.toEntity(findQna); } @Override public void deleteQna(Long id) { -qnaRepository.deleteById(id); + qnaRepository.deleteById(id); } @Override @@ -80,7 +82,7 @@ qnaRepository.deleteById(id); Optional qna; try { - qna= qnaRepository.findById(id); + qna = qnaRepository.findById(id); } catch (Exception e) { @@ -89,14 +91,12 @@ qnaRepository.deleteById(id); } - - return QnaResponseDto.toEntity(qna.get()); + return QnaResponseDto.toEntity(qna.get()); } @Override - public List getAllQnasByLecture(Long lectureId,int pageSize) - { + public List getAllQnasByLecture(Long lectureId, int pageSize) { Pageable pageable = PageRequest.of(0, pageSize); diff --git a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java index 5543795..d6c2cbc 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java @@ -60,7 +60,6 @@ public class ReportServiceImpl implements ReportService { Quiz quiz = quizList.get(idx); String inputAnswer = answerInputList.get(idx); Answer answer; - // if (quiz.getAnswer().equals(inputAnswer)) { correctCount++; answer = Answer.builder() @@ -183,6 +182,7 @@ public class ReportServiceImpl implements ReportService { List reportList = reportRepository.findByUser_Id(userId); + List reportListResponseDtoList = new ArrayList<>(); diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java index 77fd4ca..93ef8c5 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java @@ -23,8 +23,7 @@ public class UserServiceImpl implements UserService { private final UserRepository userRepository; - public void join(RequestJoinDto requestJoinDto) - { + public void join(RequestJoinDto requestJoinDto) { User user = User.builder() .userId(requestJoinDto.getUserId()) .email(requestJoinDto.getEmail()) @@ -36,7 +35,7 @@ public class UserServiceImpl implements UserService { } - public User login(User user){ + public User login(User user) { Optional findUser = userRepository.findByUserId(user.getUserId()); if (findUser.isEmpty()) { @@ -63,32 +62,29 @@ public class UserServiceImpl implements UserService { } - - - @Override - public String getUserName(Long id){ + public String getUserName(Long id) { return userRepository.findById(id).get().getName(); } @Override - public void changeUserInfo(InfoDto infoDto, Long id){ + public void changeUserInfo(InfoDto infoDto, Long id) { User user = userRepository.findById(id).orElseThrow(IllegalArgumentException::new); if (infoDto.getName() != null) user.setName(infoDto.getName()); - if(infoDto.getEmail()!=null) + if (infoDto.getEmail() != null) user.setEmail(infoDto.getEmail()); userRepository.save(user); -} + } @Override - public void changePassword(PasswordDto passwordDto, Long id){ + public void changePassword(PasswordDto passwordDto, Long id) { User user = userRepository.findById(id).orElse(null); if (user == null) { @@ -97,6 +93,8 @@ public class UserServiceImpl implements UserService { if (!PasswordUtils.checkPassword(passwordDto.getCurrentPassword(), user.getPassword())) { throw new UserException("Current password is incorrect"); + } else if (passwordDto.getCurrentPassword().equals(passwordDto.getNewPassword())) { + throw new UserException("New password cannot be the same as the current password"); } else { if (!passwordDto.getNewPassword().equals(passwordDto.getNewPasswordCheck())) { throw new UserException("New password confirmation does not match"); @@ -114,29 +112,30 @@ public class UserServiceImpl implements UserService { } public String getTempPassword() { - char[] charSet = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', + char[] charSet = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; String str = ""; int idx = 0; - for (int i=0; i<10; i++) { + for (int i = 0; i < 10; i++) { idx = (int) (charSet.length * Math.random()); str += charSet[idx]; } return str; } + @Override - public void saveRefreshToken(Long id, String refreshToken){ + public void saveRefreshToken(Long id, String refreshToken) { userRepository.saveRefreshToken(id, refreshToken); } @Override - public String getRefreshToken(Long id){ + public String getRefreshToken(Long id) { return userRepository.getRefreshToken(id); } @Override - public void deleteRefreshToken(Long id){ + public void deleteRefreshToken(Long id) { userRepository.deleteRefreshToken(id); } From 8d4d9e02272f5ba6784b37f1f6033bd438dd031a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=A0=95=EB=AF=BC?= Date: Wed, 7 Aug 2024 16:57:38 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20qna=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qna/controller/QnaController.java | 98 +++++++++++-------- .../edufocus/qna/service/QnaService.java | 17 +++- .../edufocus/qna/service/QnaServiceImpl.java | 30 ++++-- 3 files changed, 95 insertions(+), 50 deletions(-) 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 fe50454..90f33c6 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 @@ -4,6 +4,9 @@ import com.edufocus.edufocus.qna.entity.Qna; import com.edufocus.edufocus.qna.entity.QnaRequestDto; import com.edufocus.edufocus.qna.entity.QnaResponseDto; import com.edufocus.edufocus.qna.service.QnaService; +import com.edufocus.edufocus.user.model.entity.vo.User; +import com.edufocus.edufocus.user.model.entity.vo.UserRole; +import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.util.JWTUtil; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; @@ -23,81 +26,98 @@ import java.util.List; public class QnaController { private final QnaService qnaService; private final JWTUtil jwtUtil; - private static int PAGE_SIZE=10; - + private static int PAGE_SIZE = 10; + private final UserRepository userRepository; @PostMapping("/{lecture_id}") - public ResponseEntity createQna(@PathVariable("lecture_id") Long lecture_id, @RequestBody QnaRequestDto qnaRequestDto , HttpServletRequest request) { + public ResponseEntity createQna(@PathVariable("lecture_id") Long lecture_id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) { - try{ + try { String token = request.getHeader("Authorization"); Long userId = Long.parseLong(jwtUtil.getUserId(token)); - QnaResponseDto qnaResponseDto= qnaService.createQna(userId,qnaRequestDto,lecture_id); - return new ResponseEntity<>( qnaResponseDto,HttpStatus.CREATED); + QnaResponseDto qnaResponseDto = qnaService.createQna(userId, qnaRequestDto, lecture_id); + return new ResponseEntity<>(qnaResponseDto, HttpStatus.CREATED); - }catch (Exception e){ + } catch (Exception e) { throw new RuntimeException(e); } } @PostMapping({"/answer/create/{qna_id}"}) - public ResponseEntity createAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto) - { + public ResponseEntity createAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) { try { - QnaResponseDto responseDto = qnaService.createAnswer(qna_id,qnaRequestDto); - return new ResponseEntity<>(responseDto,HttpStatus.ACCEPTED); - } - catch (Exception e) - { + String token = request.getHeader("Authorization"); + Long userId = Long.parseLong(jwtUtil.getUserId(token)); + User findUser = userRepository.findById(userId).orElse(null); + + if (findUser.getRole() != UserRole.ADMIN) { + throw new RuntimeException(); + } + + QnaResponseDto responseDto = qnaService.createAnswer(qna_id, qnaRequestDto); + return new ResponseEntity<>(responseDto, HttpStatus.ACCEPTED); + } catch (Exception e) { throw new RuntimeException(e); } } @PutMapping({"/answer/update/{qna_id}"}) - public ResponseEntity updateAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto) - { + public ResponseEntity updateAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) { try { - QnaResponseDto responseDto = qnaService.updateAnswer(qna_id,qnaRequestDto); - return new ResponseEntity<>(responseDto,HttpStatus.ACCEPTED); - } - catch (Exception e) - { + String token = request.getHeader("Authorization"); + Long userId = Long.parseLong(jwtUtil.getUserId(token)); + User findUser = userRepository.findById(userId).orElse(null); + + if (findUser.getRole() != UserRole.ADMIN) { + throw new RuntimeException(); + } + + QnaResponseDto responseDto = qnaService.updateAnswer(qna_id, qnaRequestDto); + return new ResponseEntity<>(responseDto, HttpStatus.ACCEPTED); + } catch (Exception e) { throw new RuntimeException(e); } } @PostMapping("/answer/delete/{qna_id}") - public ResponseEntity deleteAnswer(@PathVariable("qna_id") Long qna_id) - { + public ResponseEntity deleteAnswer(@PathVariable("qna_id") Long qna_id, HttpServletRequest request) { try { - qnaService.deleteAnswer(qna_id); + String token = request.getHeader("Authorization"); + Long userId = Long.parseLong(jwtUtil.getUserId(token)); + User findUser = userRepository.findById(userId).orElse(null); + + if (findUser.getRole() != UserRole.ADMIN) { + throw new RuntimeException(); + } + qnaService.deleteAnswer(qna_id); return new ResponseEntity<>(HttpStatus.ACCEPTED); - } - catch (Exception e) - { + } catch (Exception e) { throw new RuntimeException(e); } } @PutMapping("/{id}") - public ResponseEntity updateQna(@PathVariable Long id, @RequestBody QnaRequestDto qnaRequestDto) { - - try{ - QnaResponseDto qnaResponseDto= qnaService.updateQna(id,qnaRequestDto); + public ResponseEntity updateQna(@PathVariable Long id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) { + String token = request.getHeader("Authorization"); + Long userId = Long.parseLong(jwtUtil.getUserId(token)); + try { + QnaResponseDto qnaResponseDto = qnaService.updateQna(id, qnaRequestDto, userId); return new ResponseEntity<>(qnaResponseDto, HttpStatus.ACCEPTED); - }catch (Exception e) - { - throw new RuntimeException(e); } + } catch (Exception e) { + throw new RuntimeException(e); + } } @DeleteMapping("/{id}") - public ResponseEntity deleteQna(@PathVariable Long id) { + public ResponseEntity deleteQna(@PathVariable Long id, HttpServletRequest request) { try { - qnaService.deleteQna(id); + String token = request.getHeader("Authorization"); + Long userId = Long.parseLong(jwtUtil.getUserId(token)); + qnaService.deleteQna(id, userId); return new ResponseEntity<>(HttpStatus.ACCEPTED); } catch (SQLException e) { @@ -107,8 +127,8 @@ public class QnaController { @GetMapping("/{id}") public ResponseEntity getQna(@PathVariable Long id) { - try{ - QnaResponseDto findQna= qnaService.getQna(id); + try { + QnaResponseDto findQna = qnaService.getQna(id); return new ResponseEntity<>(findQna, HttpStatus.ACCEPTED); } catch (SQLException e) { @@ -121,7 +141,7 @@ public class QnaController { try { - List qnaList= qnaService.getAllQnasByLecture(id,PAGE_SIZE); + List qnaList = qnaService.getAllQnasByLecture(id, PAGE_SIZE); return new ResponseEntity<>(qnaList, HttpStatus.ACCEPTED); } catch (SQLException 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 de97f93..912ed9e 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 @@ -10,17 +10,24 @@ import org.springframework.stereotype.Service; import java.sql.SQLException; import java.util.List; + @Service public interface QnaService { QnaResponseDto createQna(Long id, QnaRequestDto qnaRequestDto, Long lecture_id) throws SQLException; - QnaResponseDto updateQna(Long id,QnaRequestDto qnaRequestDto) throws SQLException; - void deleteQna(Long id) throws SQLException; + + QnaResponseDto updateQna(Long id, QnaRequestDto qnaRequestDto, Long userId) throws SQLException; + + void deleteQna(Long id, Long userId) throws SQLException; + QnaResponseDto getQna(Long id) throws SQLException; - List getAllQnasByLecture(Long lectureId,int pageNumber) throws SQLException; - QnaResponseDto createAnswer(Long id,QnaRequestDto qnaRequestDto) throws SQLException; - QnaResponseDto updateAnswer(Long id,QnaRequestDto qnaRequestDto) throws SQLException; + List getAllQnasByLecture(Long lectureId, int pageNumber) throws SQLException; + + QnaResponseDto createAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException; + + QnaResponseDto updateAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException; + void deleteAnswer(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 dbc6d84..637239c 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 @@ -42,9 +42,7 @@ public class QnaServiceImpl implements QnaService { Qna qna = QnaRequestDto.toEntity(qnaRequestDto); - if (qna.getAnswer() != null || user.getRole() != UserRole.ADMIN) { - throw new RuntimeException(); - } + qna.setLecture(lecture); qna.setUser(user); @@ -55,7 +53,15 @@ public class QnaServiceImpl implements QnaService { } @Override - public QnaResponseDto updateQna(Long id, QnaRequestDto qnaRequestDto) { + public QnaResponseDto updateQna(Long id, QnaRequestDto qnaRequestDto, Long userId) { + + Qna qna = qnaRepository.findById(id).orElse(null); + User user = userRepository.findById(userId).orElse(null); + if (qna.getUser().getId() == userId) { + qnaRepository.delete(qna); + } else { + throw new RuntimeException(); + } Qna findQna = qnaRepository.findById(id) @@ -73,8 +79,16 @@ public class QnaServiceImpl implements QnaService { } @Override - public void deleteQna(Long id) { - qnaRepository.deleteById(id); + public void deleteQna(Long id, Long userId) { + + Qna qna = qnaRepository.findById(id).orElse(null); + User user = userRepository.findById(userId).orElse(null); + if (qna.getUser().getId() == userId || user.getRole() == UserRole.ADMIN) { + qnaRepository.delete(qna); + } else { + throw new RuntimeException(); + } + } @Override @@ -115,6 +129,9 @@ public class QnaServiceImpl implements QnaService { Qna findQna = qnaRepository.findById(id).orElse(null); findQna.setAnswer(qnaRequestDto.getAnswer()); + if (findQna.getAnswer() != null) { + throw new RuntimeException(); + } qnaRepository.save(findQna); return QnaResponseDto.toEntity(findQna); @@ -127,6 +144,7 @@ public class QnaServiceImpl implements QnaService { Qna findQna = qnaRepository.findById(id).orElse(null); findQna.setAnswer(qnaRequestDto.getAnswer()); + qnaRepository.save(findQna); return QnaResponseDto.toEntity(findQna); From 25f7343b0430e9a8e7f072f0e584bdb7a8c27f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=A0=95=EB=AF=BC?= Date: Wed, 7 Aug 2024 17:04:19 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:qna=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/edufocus/edufocus/qna/controller/QnaController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 90f33c6..64b2e73 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 @@ -71,7 +71,7 @@ public class QnaController { User findUser = userRepository.findById(userId).orElse(null); if (findUser.getRole() != UserRole.ADMIN) { - throw new RuntimeException(); + throw new RuntimeException("update 실패"); } QnaResponseDto responseDto = qnaService.updateAnswer(qna_id, qnaRequestDto);