Merge branch 'backend' into 'Be/ReportSet'

This commit is contained in:
김한얼 2024-08-08 09:21:48 +09:00
commit 2ca7146239
12 changed files with 127 additions and 67 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.4 KiB

View File

@ -31,7 +31,7 @@ public class MailController {
@GetMapping("/verify") @GetMapping("/verify")
public ResponseEntity<?> verifyCode(@RequestParam String code, @RequestParam String email) { public ResponseEntity<?> verifyCode(@RequestParam String code, @RequestParam String email) {
if (!mailService.verifyCode(code, email)) { if (!mailService.verifyCode(code, email)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -1,7 +1,6 @@
package com.edufocus.edufocus.mail.service; package com.edufocus.edufocus.mail.service;
import com.edufocus.edufocus.redis.util.RedisUtil; import com.edufocus.edufocus.redis.util.RedisUtil;
import com.edufocus.edufocus.user.model.entity.vo.User;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
import com.edufocus.edufocus.user.model.service.UserService; import com.edufocus.edufocus.user.model.service.UserService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -11,7 +10,6 @@ import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.NoSuchElementException;
import java.util.Random; import java.util.Random;
@Service @Service
@ -31,7 +29,13 @@ public class MailServiceImpl implements MailService {
@Override @Override
public void sendMail(String email) { public void sendMail(String email) {
String code = createRandomCode(); String code = createRandomCode();
if (redisUtil.exists(email)) {
redisUtil.deleteData(redisUtil.getData(email));
}
redisUtil.setDataExpire(code, email, 60 * 5L); redisUtil.setDataExpire(code, email, 60 * 5L);
redisUtil.setDataExpire(email, code, 60 * 5L);
SimpleMailMessage mail = createEmail(email, "[EDUFOCUS] 비밀번호 찾기 안내", code); SimpleMailMessage mail = createEmail(email, "[EDUFOCUS] 비밀번호 찾기 안내", code);
mailSender.send(mail); mailSender.send(mail);
@ -46,6 +50,7 @@ public class MailServiceImpl implements MailService {
private SimpleMailMessage createEmail(String to, String title, String code) { private SimpleMailMessage createEmail(String to, String title, String code) {
SimpleMailMessage message = new SimpleMailMessage(); SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("EDUFOCUS");
message.setTo(to); message.setTo(to);
message.setSubject(title); message.setSubject(title);
message.setText("인증번호 6자리입니다 : " + code); message.setText("인증번호 6자리입니다 : " + code);

View File

@ -4,6 +4,9 @@ import com.edufocus.edufocus.qna.entity.Qna;
import com.edufocus.edufocus.qna.entity.QnaRequestDto; 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.service.QnaService; 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 com.edufocus.edufocus.user.util.JWTUtil;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -24,7 +27,7 @@ public class QnaController {
private final QnaService qnaService; private final QnaService qnaService;
private final JWTUtil jwtUtil; private final JWTUtil jwtUtil;
private static int PAGE_SIZE = 10; private static int PAGE_SIZE = 10;
private final UserRepository userRepository;
@PostMapping("/{lecture_id}") @PostMapping("/{lecture_id}")
public ResponseEntity<QnaResponseDto> createQna(@PathVariable("lecture_id") Long lecture_id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) { public ResponseEntity<QnaResponseDto> createQna(@PathVariable("lecture_id") Long lecture_id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) {
@ -43,61 +46,80 @@ public class QnaController {
} }
@PostMapping({"/answer/create/{qna_id}"}) @PostMapping({"/answer/create/{qna_id}"})
public ResponseEntity<QnaResponseDto> createAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto) public ResponseEntity<QnaResponseDto> createAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) {
{
try { try {
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); QnaResponseDto responseDto = qnaService.createAnswer(qna_id, qnaRequestDto);
return new ResponseEntity<>(responseDto, HttpStatus.ACCEPTED); return new ResponseEntity<>(responseDto, HttpStatus.ACCEPTED);
} } catch (Exception e) {
catch (Exception e)
{
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@PutMapping({"/answer/update/{qna_id}"}) @PutMapping({"/answer/update/{qna_id}"})
public ResponseEntity<QnaResponseDto> updateAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto) public ResponseEntity<QnaResponseDto> updateAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) {
{
try { try {
String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token));
User findUser = userRepository.findById(userId).orElse(null);
if (findUser.getRole() != UserRole.ADMIN) {
System.out.println("role 안맞음");
throw new RuntimeException("update 실패");
}
QnaResponseDto responseDto = qnaService.updateAnswer(qna_id, qnaRequestDto); QnaResponseDto responseDto = qnaService.updateAnswer(qna_id, qnaRequestDto);
return new ResponseEntity<>(responseDto, HttpStatus.ACCEPTED); return new ResponseEntity<>(responseDto, HttpStatus.ACCEPTED);
} } catch (Exception e) {
catch (Exception e)
{
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@PostMapping("/answer/delete/{qna_id}") @PostMapping("/answer/delete/{qna_id}")
public ResponseEntity<QnaResponseDto> deleteAnswer(@PathVariable("qna_id") Long qna_id) public ResponseEntity<QnaResponseDto> deleteAnswer(@PathVariable("qna_id") Long qna_id, HttpServletRequest request) {
{
try { try {
String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token));
User findUser = userRepository.findById(userId).orElse(null);
System.out.println("delete answer");
if (findUser.getRole() != UserRole.ADMIN) {
throw new RuntimeException();
}
qnaService.deleteAnswer(qna_id); qnaService.deleteAnswer(qna_id);
return new ResponseEntity<>(HttpStatus.ACCEPTED); return new ResponseEntity<>(HttpStatus.ACCEPTED);
} } catch (Exception e) {
catch (Exception e)
{
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public ResponseEntity<QnaResponseDto> updateQna(@PathVariable Long id, @RequestBody QnaRequestDto qnaRequestDto) { public ResponseEntity<QnaResponseDto> updateQna(@PathVariable Long id, @RequestBody QnaRequestDto qnaRequestDto, HttpServletRequest request) {
String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token));
try { try {
QnaResponseDto qnaResponseDto= qnaService.updateQna(id,qnaRequestDto); QnaResponseDto qnaResponseDto = qnaService.updateQna(id, qnaRequestDto, userId);
return new ResponseEntity<>(qnaResponseDto, HttpStatus.ACCEPTED); return new ResponseEntity<>(qnaResponseDto, HttpStatus.ACCEPTED);
}catch (Exception e) } catch (Exception e) {
{ throw new RuntimeException(e);
throw new RuntimeException(e); } }
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ResponseEntity<Qna> deleteQna(@PathVariable Long id) { public ResponseEntity<Qna> deleteQna(@PathVariable Long id, HttpServletRequest request) {
try { 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); return new ResponseEntity<>(HttpStatus.ACCEPTED);
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -10,17 +10,24 @@ import org.springframework.stereotype.Service;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
@Service @Service
public interface QnaService { public interface QnaService {
QnaResponseDto createQna(Long id, QnaRequestDto qnaRequestDto, Long lecture_id) throws SQLException; 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; QnaResponseDto getQna(Long id) throws SQLException;
List<QnaResponseDto> getAllQnasByLecture(Long lectureId, int pageNumber) throws SQLException; List<QnaResponseDto> getAllQnasByLecture(Long lectureId, int pageNumber) throws SQLException;
QnaResponseDto createAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException; QnaResponseDto createAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException;
QnaResponseDto updateAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException; QnaResponseDto updateAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException;
void deleteAnswer(Long id) throws SQLException; void deleteAnswer(Long id) throws SQLException;
} }

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;
@ -31,7 +32,6 @@ public class QnaServiceImpl implements QnaService{
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,8 +41,8 @@ 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);
qna.setLecture(lecture); qna.setLecture(lecture);
qna.setUser(user); qna.setUser(user);
@ -53,7 +53,17 @@ public class QnaServiceImpl implements QnaService{
} }
@Override @Override
public QnaResponseDto updateQna(Long id,QnaRequestDto qnaRequestDto) { public QnaResponseDto updateQna(Long id, QnaRequestDto qnaRequestDto, Long userId) {
System.out.println("userId:" + userId);
Qna qna = qnaRepository.findById(id).orElse(null);
System.out.println("quesiton에 있는거: " + qna.getUser().getId());
User user = userRepository.findById(userId).orElse(null);
if (qna.getUser().getId() != userId) {
throw new RuntimeException();
}
Qna findQna = qnaRepository.findById(id) Qna findQna = qnaRepository.findById(id)
@ -71,8 +81,16 @@ public class QnaServiceImpl implements QnaService{
} }
@Override @Override
public void deleteQna(Long id) { public void deleteQna(Long id, Long userId) {
qnaRepository.deleteById(id);
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 @Override
@ -89,14 +107,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);
@ -115,6 +131,9 @@ qnaRepository.deleteById(id);
Qna findQna = qnaRepository.findById(id).orElse(null); Qna findQna = qnaRepository.findById(id).orElse(null);
findQna.setAnswer(qnaRequestDto.getAnswer()); findQna.setAnswer(qnaRequestDto.getAnswer());
if (findQna.getAnswer() != null) {
throw new RuntimeException();
}
qnaRepository.save(findQna); qnaRepository.save(findQna);
return QnaResponseDto.toEntity(findQna); return QnaResponseDto.toEntity(findQna);
@ -127,6 +146,7 @@ qnaRepository.deleteById(id);
Qna findQna = qnaRepository.findById(id).orElse(null); Qna findQna = qnaRepository.findById(id).orElse(null);
findQna.setAnswer(qnaRequestDto.getAnswer()); findQna.setAnswer(qnaRequestDto.getAnswer());
qnaRepository.save(findQna); qnaRepository.save(findQna);
return QnaResponseDto.toEntity(findQna); return QnaResponseDto.toEntity(findQna);

View File

@ -33,4 +33,8 @@ public class RedisUtil {
stringRedisTemplate.delete(key); stringRedisTemplate.delete(key);
} }
public boolean exists(String key) {
return stringRedisTemplate.hasKey(key);
}
} }

View File

@ -71,9 +71,9 @@ public class UserController {
// 비밀번호 찾기를 통한 변경 // 비밀번호 찾기를 통한 변경
@PutMapping("/updateforgottenpassword") @PutMapping("/updateforgottenpassword")
public ResponseEntity<String> updatePassword(@RequestParam long userId, public ResponseEntity<String> updatePassword(@RequestParam String email,
@RequestParam String newPassword) { @RequestParam String newPassword) {
userService.changeForgottenPassword(userId, newPassword); userService.changeForgottenPassword(email, newPassword);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -28,7 +28,6 @@ public interface UserService {
boolean isEmailExist(String email); boolean isEmailExist(String email);
void changeForgottenPassword(Long id, String newPassword);
boolean isTeacher(long id); void changeForgottenPassword(String email, String newPassword);
} }

View File

@ -94,6 +94,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");
@ -144,8 +146,8 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public void changeForgottenPassword(Long id, String newPassword) { public void changeForgottenPassword(String email, String newPassword) {
User user = userRepository.findById(id).orElse(null); User user = userRepository.findByEmail(email).orElse(null);
if (user == null) { if (user == null) {
throw new UserException("User not found"); throw new UserException("User not found");

View File

@ -39,3 +39,4 @@ spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.enable=true
spring.data.redis.host=${REDIS_HOST} spring.data.redis.host=${REDIS_HOST}
spring.data.redis.port=${REDIS_PORT} spring.data.redis.port=${REDIS_PORT}
spring.data.redis.password=${REDIS_PASSWORD}