feat: qna 수정
This commit is contained in:
parent
47d480210e
commit
8d4d9e0227
@ -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;
|
||||||
@ -23,81 +26,98 @@ import java.util.List;
|
|||||||
public class QnaController {
|
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) {
|
||||||
|
|
||||||
|
|
||||||
try{
|
try {
|
||||||
String token = request.getHeader("Authorization");
|
String token = request.getHeader("Authorization");
|
||||||
Long userId = Long.parseLong(jwtUtil.getUserId(token));
|
Long userId = Long.parseLong(jwtUtil.getUserId(token));
|
||||||
|
|
||||||
QnaResponseDto qnaResponseDto= qnaService.createQna(userId,qnaRequestDto,lecture_id);
|
QnaResponseDto qnaResponseDto = qnaService.createQna(userId, qnaRequestDto, lecture_id);
|
||||||
return new ResponseEntity<>( qnaResponseDto,HttpStatus.CREATED);
|
return new ResponseEntity<>(qnaResponseDto, HttpStatus.CREATED);
|
||||||
|
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 {
|
||||||
QnaResponseDto responseDto = qnaService.createAnswer(qna_id,qnaRequestDto);
|
String token = request.getHeader("Authorization");
|
||||||
return new ResponseEntity<>(responseDto,HttpStatus.ACCEPTED);
|
Long userId = Long.parseLong(jwtUtil.getUserId(token));
|
||||||
}
|
User findUser = userRepository.findById(userId).orElse(null);
|
||||||
catch (Exception e)
|
|
||||||
{
|
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);
|
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 {
|
||||||
QnaResponseDto responseDto = qnaService.updateAnswer(qna_id,qnaRequestDto);
|
String token = request.getHeader("Authorization");
|
||||||
return new ResponseEntity<>(responseDto,HttpStatus.ACCEPTED);
|
Long userId = Long.parseLong(jwtUtil.getUserId(token));
|
||||||
}
|
User findUser = userRepository.findById(userId).orElse(null);
|
||||||
catch (Exception e)
|
|
||||||
{
|
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);
|
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 {
|
||||||
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);
|
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");
|
||||||
try{
|
Long userId = Long.parseLong(jwtUtil.getUserId(token));
|
||||||
QnaResponseDto qnaResponseDto= qnaService.updateQna(id,qnaRequestDto);
|
try {
|
||||||
|
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) {
|
||||||
@ -107,8 +127,8 @@ public class QnaController {
|
|||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public ResponseEntity<QnaResponseDto> getQna(@PathVariable Long id) {
|
public ResponseEntity<QnaResponseDto> getQna(@PathVariable Long id) {
|
||||||
try{
|
try {
|
||||||
QnaResponseDto findQna= qnaService.getQna(id);
|
QnaResponseDto findQna = qnaService.getQna(id);
|
||||||
return new ResponseEntity<>(findQna, HttpStatus.ACCEPTED);
|
return new ResponseEntity<>(findQna, HttpStatus.ACCEPTED);
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -121,7 +141,7 @@ public class QnaController {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
|
|
||||||
List<QnaResponseDto> qnaList= qnaService.getAllQnasByLecture(id,PAGE_SIZE);
|
List<QnaResponseDto> qnaList = qnaService.getAllQnasByLecture(id, PAGE_SIZE);
|
||||||
|
|
||||||
return new ResponseEntity<>(qnaList, HttpStatus.ACCEPTED);
|
return new ResponseEntity<>(qnaList, HttpStatus.ACCEPTED);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
@ -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 updateAnswer(Long id,QnaRequestDto qnaRequestDto) throws SQLException;
|
QnaResponseDto createAnswer(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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,9 +42,7 @@ public class QnaServiceImpl implements QnaService {
|
|||||||
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@ -55,7 +53,15 @@ public class QnaServiceImpl implements QnaService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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)
|
Qna findQna = qnaRepository.findById(id)
|
||||||
@ -73,8 +79,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
|
||||||
@ -115,6 +129,9 @@ public class QnaServiceImpl implements QnaService {
|
|||||||
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 +144,7 @@ public class QnaServiceImpl implements QnaService {
|
|||||||
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user