feat: qna 답변 수정

This commit is contained in:
박정민 2024-08-07 15:30:54 +09:00
parent e8c88f9b1b
commit c3c574baf3
3 changed files with 28 additions and 29 deletions

View File

@ -7,6 +7,7 @@ import com.edufocus.edufocus.qna.entity.QnaRequestDto;
import com.edufocus.edufocus.qna.entity.QnaResponseDto; import com.edufocus.edufocus.qna.entity.QnaResponseDto;
import com.edufocus.edufocus.qna.repository.QnaRepository; import com.edufocus.edufocus.qna.repository.QnaRepository;
import com.edufocus.edufocus.user.model.entity.vo.User; 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.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -24,14 +25,13 @@ import java.util.stream.Collectors;
@Service @Service
@Transactional @Transactional
@RequiredArgsConstructor @RequiredArgsConstructor
public class QnaServiceImpl implements QnaService{ public class QnaServiceImpl implements QnaService {
private final QnaRepository qnaRepository; private final QnaRepository qnaRepository;
private final LectureRepository lectureRepository; private final LectureRepository lectureRepository;
private final UserRepository userRepository; private final UserRepository userRepository;
@Override @Override
public QnaResponseDto createQna(Long id, QnaRequestDto qnaRequestDto, Long lecture_id) { 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); User user = userRepository.findById(id).orElse(null);
Qna qna = QnaRequestDto.toEntity(qnaRequestDto); Qna qna = QnaRequestDto.toEntity(qnaRequestDto);
if (qna.getAnswer() != null || user.getRole() != UserRole.ADMIN) {
throw new RuntimeException();
}
qna.setLecture(lecture); qna.setLecture(lecture);
qna.setUser(user); qna.setUser(user);
qna.setCreatedAt(new Date()); qna.setCreatedAt(new Date());
qnaRepository.save(qna); qnaRepository.save(qna);
return QnaResponseDto.toEntity(qna); return QnaResponseDto.toEntity(qna);
} }
@Override @Override
public QnaResponseDto updateQna(Long id,QnaRequestDto qnaRequestDto) { public QnaResponseDto updateQna(Long id, QnaRequestDto qnaRequestDto) {
Qna findQna = qnaRepository.findById(id) Qna findQna = qnaRepository.findById(id)
@ -65,14 +67,14 @@ public class QnaServiceImpl implements QnaService{
qnaRepository.save(findQna); qnaRepository.save(findQna);
return QnaResponseDto.toEntity(findQna); return QnaResponseDto.toEntity(findQna);
} }
@Override @Override
public void deleteQna(Long id) { public void deleteQna(Long id) {
qnaRepository.deleteById(id); qnaRepository.deleteById(id);
} }
@Override @Override
@ -80,7 +82,7 @@ qnaRepository.deleteById(id);
Optional<Qna> qna; Optional<Qna> qna;
try { try {
qna= qnaRepository.findById(id); qna = qnaRepository.findById(id);
} catch (Exception e) { } catch (Exception e) {
@ -89,14 +91,12 @@ qnaRepository.deleteById(id);
} }
return QnaResponseDto.toEntity(qna.get());
return QnaResponseDto.toEntity(qna.get());
} }
@Override @Override
public List<QnaResponseDto> getAllQnasByLecture(Long lectureId,int pageSize) public List<QnaResponseDto> getAllQnasByLecture(Long lectureId, int pageSize) {
{
Pageable pageable = PageRequest.of(0, pageSize); Pageable pageable = PageRequest.of(0, pageSize);

View File

@ -60,7 +60,6 @@ public class ReportServiceImpl implements ReportService {
Quiz quiz = quizList.get(idx); Quiz quiz = quizList.get(idx);
String inputAnswer = answerInputList.get(idx); String inputAnswer = answerInputList.get(idx);
Answer answer; Answer answer;
//
if (quiz.getAnswer().equals(inputAnswer)) { if (quiz.getAnswer().equals(inputAnswer)) {
correctCount++; correctCount++;
answer = Answer.builder() answer = Answer.builder()
@ -183,6 +182,7 @@ public class ReportServiceImpl implements ReportService {
List<Report> reportList = reportRepository.findByUser_Id(userId); List<Report> reportList = reportRepository.findByUser_Id(userId);
List<ReportListResponseDto> reportListResponseDtoList = new ArrayList<>(); List<ReportListResponseDto> reportListResponseDtoList = new ArrayList<>();

View File

@ -23,8 +23,7 @@ public class UserServiceImpl implements UserService {
private final UserRepository userRepository; private final UserRepository userRepository;
public void join(RequestJoinDto requestJoinDto) public void join(RequestJoinDto requestJoinDto) {
{
User user = User.builder() User user = User.builder()
.userId(requestJoinDto.getUserId()) .userId(requestJoinDto.getUserId())
.email(requestJoinDto.getEmail()) .email(requestJoinDto.getEmail())
@ -36,7 +35,7 @@ public class UserServiceImpl implements UserService {
} }
public User login(User user){ public User login(User user) {
Optional<User> findUser = userRepository.findByUserId(user.getUserId()); Optional<User> findUser = userRepository.findByUserId(user.getUserId());
if (findUser.isEmpty()) { if (findUser.isEmpty()) {
@ -63,32 +62,29 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public String getUserName(Long id){ public String getUserName(Long id) {
return userRepository.findById(id).get().getName(); return userRepository.findById(id).get().getName();
} }
@Override @Override
public void changeUserInfo(InfoDto infoDto, Long id){ public void changeUserInfo(InfoDto infoDto, Long id) {
User user = userRepository.findById(id).orElseThrow(IllegalArgumentException::new); User user = userRepository.findById(id).orElseThrow(IllegalArgumentException::new);
if (infoDto.getName() != null) if (infoDto.getName() != null)
user.setName(infoDto.getName()); user.setName(infoDto.getName());
if(infoDto.getEmail()!=null) if (infoDto.getEmail() != null)
user.setEmail(infoDto.getEmail()); user.setEmail(infoDto.getEmail());
userRepository.save(user); userRepository.save(user);
} }
@Override @Override
public void changePassword(PasswordDto passwordDto, Long id){ public void changePassword(PasswordDto passwordDto, Long id) {
User user = userRepository.findById(id).orElse(null); User user = userRepository.findById(id).orElse(null);
if (user == null) { if (user == null) {
@ -97,6 +93,8 @@ public class UserServiceImpl implements UserService {
if (!PasswordUtils.checkPassword(passwordDto.getCurrentPassword(), user.getPassword())) { if (!PasswordUtils.checkPassword(passwordDto.getCurrentPassword(), user.getPassword())) {
throw new UserException("Current password is incorrect"); 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 { } else {
if (!passwordDto.getNewPassword().equals(passwordDto.getNewPasswordCheck())) { if (!passwordDto.getNewPassword().equals(passwordDto.getNewPasswordCheck())) {
throw new UserException("New password confirmation does not match"); throw new UserException("New password confirmation does not match");
@ -114,29 +112,30 @@ public class UserServiceImpl implements UserService {
} }
public String getTempPassword() { 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'}; 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
String str = ""; String str = "";
int idx = 0; int idx = 0;
for (int i=0; i<10; i++) { for (int i = 0; i < 10; i++) {
idx = (int) (charSet.length * Math.random()); idx = (int) (charSet.length * Math.random());
str += charSet[idx]; str += charSet[idx];
} }
return str; return str;
} }
@Override @Override
public void saveRefreshToken(Long id, String refreshToken){ public void saveRefreshToken(Long id, String refreshToken) {
userRepository.saveRefreshToken(id, refreshToken); userRepository.saveRefreshToken(id, refreshToken);
} }
@Override @Override
public String getRefreshToken(Long id){ public String getRefreshToken(Long id) {
return userRepository.getRefreshToken(id); return userRepository.getRefreshToken(id);
} }
@Override @Override
public void deleteRefreshToken(Long id){ public void deleteRefreshToken(Long id) {
userRepository.deleteRefreshToken(id); userRepository.deleteRefreshToken(id);
} }