Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public ResponseEntity<AnswerResponse> readAnswer(@PathVariable Long id){
return ResponseEntity.ok(answerService.readAnswer(id));
}

@PutMapping("/{id}")
public ResponseEntity<AnswerResponse> updateAnswer(@PathVariable Long id, @RequestBody AnswerRequest answerRequest){
return ResponseEntity.ok(answerService.updateAnswer(id, answerRequest));
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteAnswer(@PathVariable Long id){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ public class Answer extends BaseTimeEntity {
private Long version;

@Builder
public Answer(Long id, String content, Question question, Member member, Interview interview, int commentCnt, int heartCnt, Long version) {
Objects.requireNonNull(id, "id가 null입니다.");
public Answer(String content, Question question, Member member, Interview interview, int commentCnt, int heartCnt, Long version) {
Objects.requireNonNull(question, "question이 null입니다.");
Objects.requireNonNull(member, "member이 null입니다.");

this.id = id;
this.content = content;
this.question = question;
this.member = member;
Expand All @@ -58,11 +56,16 @@ public Answer(Long id, String content, Question question, Member member, Intervi
this.version = version;
}

public void updateContent(String content){
Objects.requireNonNull(content, "content가 null입니다.");
this.content = content;
}

public static Answer createAnswerEntity(Member member, Question question, String content) {
return Answer.builder()
.member(member)
.question(question)
.content(content)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@
@NoArgsConstructor
public class AnswerRequest {

private Long id;

private Long questionId;

private String content;

@Builder
public AnswerRequest(Long id, Long questionId, String content){
Objects.requireNonNull(id, "id가 null입니다.");
public AnswerRequest(Long questionId, String content){
Objects.requireNonNull(questionId, "questionId가 null입니다.");
Objects.requireNonNull(content, "content가 null입니다.");

this.id = id;
this.questionId = questionId;
this.content = content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import java.util.Objects;

import static com.example.interviewPrep.quiz.utils.DateFormat.customLocalDateTime;

@Getter
@NoArgsConstructor
public class AnswerResponse {
Expand All @@ -20,24 +18,20 @@ public class AnswerResponse {
private String name;

@Builder
public AnswerResponse(Long id, Long questionId, String content, String createdDate, String name){
Objects.requireNonNull(id, "id가 null입니다.");
public AnswerResponse(Long questionId, String content, String name){
Objects.requireNonNull(questionId, "questionId가 null입니다.");
Objects.requireNonNull(content, "content가 null입니다.");
Objects.requireNonNull(createdDate, "createdDate가 null입니다.");
Objects.requireNonNull(name, "name이 null입니다.");

this.id = id;
this.questionId = questionId;
this.content = content;
this.createdDate = createdDate;
this.name = name;
}

public static AnswerResponse createAnswerResponse(Answer answer){
return AnswerResponse.builder()
.id(answer.getId())
.createdDate(customLocalDateTime(answer.getCreatedDate()))
.questionId(answer.getQuestion().getId())
.content(answer.getContent())
.name(answer.getMember().getName())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
public interface AnswerRepository extends JpaRepository<Answer, Long> {
Optional<Answer> findById(Long id);

Answer save(Answer answer);

Answer saveAndFlush(Answer answer);

void delete(Answer answer);


@Query("SELECT a FROM Answer a, Member m " +
"where a.question.id = ?1 and m.id not in(?2) and a.member.id = m.id ORDER BY a.heartCnt desc")
Page<Answer> findSolution(Long id, Long memberId, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
import com.example.interviewPrep.quiz.answer.domain.Answer;
import com.example.interviewPrep.quiz.exception.advice.CommonException;
import com.example.interviewPrep.quiz.heart.repository.HeartRepository;
import com.example.interviewPrep.quiz.jwt.service.JwtService;
import com.example.interviewPrep.quiz.member.domain.Member;
import com.example.interviewPrep.quiz.member.repository.MemberRepository;
import com.example.interviewPrep.quiz.question.domain.Question;
import com.example.interviewPrep.quiz.question.repository.QuestionRepository;
import com.example.interviewPrep.quiz.utils.JwtUtil;
import com.example.interviewPrep.quiz.utils.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.example.interviewPrep.quiz.answer.domain.Answer.createAnswerEntity;
Expand All @@ -31,12 +30,14 @@
@Service
public class AnswerService {

private final JwtService jwtService;
private final MemberRepository memberRepository;
private final AnswerRepository answerRepository;
private final QuestionRepository questionRepository;
private final HeartRepository heartRepository;

public AnswerService(MemberRepository memberRepository, AnswerRepository answerRepository, QuestionRepository questionRepository, HeartRepository heartRepository){
public AnswerService(JwtService jwtService, MemberRepository memberRepository, AnswerRepository answerRepository, QuestionRepository questionRepository, HeartRepository heartRepository){
this.jwtService = jwtService;
this.memberRepository = memberRepository;
this.answerRepository = answerRepository;
this.questionRepository = questionRepository;
Expand All @@ -45,15 +46,15 @@ public AnswerService(MemberRepository memberRepository, AnswerRepository answerR

public AnswerResponse createAnswer(AnswerRequest answerRequest){

Long memberId = JwtUtil.getMemberId();
Long memberId = jwtService.getMemberId();

Optional<Member> member = memberRepository.findById(memberId);
Optional<Question> question = questionRepository.findById(answerRequest.getQuestionId());
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CommonException(NOT_FOUND_MEMBER));

Member answerMember = member.get();
Question answerQuestion = question.get();
Question question = questionRepository.findById(answerRequest.getQuestionId())
.orElseThrow(() -> new CommonException(NOT_FOUND_ANSWER));

Answer answer = createAnswerEntity(answerMember, answerQuestion, answerRequest.getContent());
Answer answer = createAnswerEntity(member, question, answerRequest.getContent());

answerRepository.save(answer);

Expand All @@ -62,36 +63,40 @@ public AnswerResponse createAnswer(AnswerRequest answerRequest){

public AnswerResponse readAnswer(Long id){

Optional<Answer> answer = answerRepository.findById(id);
Answer answer = answerRepository.findById(id)
.orElseThrow(() -> new CommonException(NOT_FOUND_ANSWER));

if(answer.isEmpty()){
throw new CommonException(NOT_FOUND_ANSWER);
}
return createAnswerResponse(answer);
}

Answer answerResponse = answer.get();
public AnswerResponse updateAnswer(Long id, AnswerRequest answerRequest){

return createAnswerResponse(answerResponse);
}
jwtService.getMemberId();

Answer answer = answerRepository.findById(id)
.orElseThrow(()->new CommonException(NOT_FOUND_ANSWER));

answer.updateContent(answerRequest.getContent());

public Answer deleteAnswer(Long id){
return createAnswerResponse(answer);
}

Optional<Answer> answer = answerRepository.findById(id);
public AnswerResponse deleteAnswer(Long id){

if(answer.isEmpty()){
throw new CommonException(NOT_FOUND_ANSWER);
}
jwtService.getMemberId();

Answer answer = answerRepository.findById(id)
.orElseThrow(() -> new CommonException(NOT_FOUND_ANSWER));

Answer deleteAnswer = answer.get();
answerRepository.delete(deleteAnswer);
answerRepository.delete(answer);

return deleteAnswer;
return createAnswerResponse(answer);
}


public Page<SolutionResponse> getSolution(Long id, String type, Pageable pageable){

Long memberId = JwtUtil.getMemberId();
Long memberId = jwtService.getMemberId();
Page<Answer> answers;

Type inputType = Type.valueOf(type.toUpperCase());
Expand Down Expand Up @@ -139,7 +144,7 @@ public Page<SolutionResponse> makeSolutionResponse(Page<Answer> answers, List<Lo


public void checkMySolution(Long id){
Long memberId = JwtUtil.getMemberId();
Long memberId = jwtService.getMemberId();

List<Answer> answers = answerRepository.findAllByQuestionIdAndMemberId(id, memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import com.example.interviewPrep.quiz.answer.repository.AnswerRepository;
import com.example.interviewPrep.quiz.answer.repository.CommentRepository;
import com.example.interviewPrep.quiz.exception.advice.CommonException;
import com.example.interviewPrep.quiz.jwt.service.JwtService;
import com.example.interviewPrep.quiz.member.domain.Member;
import com.example.interviewPrep.quiz.member.repository.MemberRepository;
import com.example.interviewPrep.quiz.notification.service.NotificationService;
import com.example.interviewPrep.quiz.utils.JwtUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -22,12 +22,15 @@
@Service
public class CommentService {

private final JwtService jwtService;

private final CommentRepository commentRepository;
private final AnswerRepository answerRepository;
private final MemberRepository memberRepository;
private final NotificationService notificationService;

public CommentService(CommentRepository commentRepository, AnswerRepository answerRepository, MemberRepository memberRepository, NotificationService notificationService) {
public CommentService(JwtService jwtService, CommentRepository commentRepository, AnswerRepository answerRepository, MemberRepository memberRepository, NotificationService notificationService) {
this.jwtService = jwtService;
this.commentRepository = commentRepository;
this.answerRepository = answerRepository;
this.memberRepository = memberRepository;
Expand All @@ -37,7 +40,7 @@ public CommentService(CommentRepository commentRepository, AnswerRepository answ

public Page<CommentResponse> findAnswerComment(Long id, Pageable pageable){

Long memberId = JwtUtil.getMemberId();
Long memberId = jwtService.getMemberId();
Page<AnswerComment> comments = commentRepository.findAnswerComment(id, pageable);

if(comments.getContent().isEmpty()) {
Expand All @@ -62,7 +65,7 @@ public Page<CommentResponse> makeCommentResponse(Page<AnswerComment> comments, L

public CommentResponse createComment(CommentRequest commentReq){

Long memberId = JwtUtil.getMemberId();
Long memberId = jwtService.getMemberId();

Member member = findMember(memberId);
Answer answer = findAnswer(commentReq.getAnswerId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,44 @@


import com.example.interviewPrep.quiz.email.service.EmailService;
import com.example.interviewPrep.quiz.jwt.service.JwtService;
import com.example.interviewPrep.quiz.member.controller.MemberController;
import com.example.interviewPrep.quiz.member.dto.request.MemberRequest;
import com.example.interviewPrep.quiz.utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/service")
public class EmailController {
private static final Logger logger = LoggerFactory.getLogger(MemberController.class);
@Autowired
EmailService service;

private final JwtService jwtService;
private final EmailService emailService;

public EmailController(JwtService jwtService, EmailService emailService){
this.jwtService = jwtService;
this.emailService = emailService;
}

@PostMapping("/mail/change")
@ResponseBody
public void emailChangeConfirm(@RequestBody MemberRequest memberRequest) throws Exception {
logger.info("post emailConfirm");
Long memberId = JwtUtil.getMemberId();
jwtService.getMemberId();
String email = memberRequest.getEmail();
service.sendSimpleMessage(email, "change");
emailService.sendSimpleMessage(email, "change");
}


@PostMapping("/mail/join")
@ResponseBody
public void emailJoinConfirm(@RequestBody MemberRequest memberRequest) throws Exception {
logger.info("post emailConfirm");
Long memberId = JwtUtil.getMemberId();
jwtService.getMemberId();
String email = memberRequest.getEmail();
service.sendSimpleMessage(email, "join");
emailService.sendSimpleMessage(email, "join");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.example.interviewPrep.quiz.response.ErrorResponse;
import com.example.interviewPrep.quiz.response.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -16,9 +18,8 @@
public class CommonControllerAdvice {

@ExceptionHandler()
public ResultResponse<?> commonExHandler(CommonException ex) {
//log.error("[exceptionHandler] ex:", ex);
return ResultResponse.fail(ErrorResponse.of(ex.getError()));
public ResponseEntity<ResultResponse<?>> commonExHandler(CommonException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ResultResponse.fail(ErrorResponse.of(ex.getError())));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.interviewPrep.quiz.filter;

import com.example.interviewPrep.quiz.jwt.service.JwtService;
import com.example.interviewPrep.quiz.redis.RedisDao;
import com.example.interviewPrep.quiz.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
Expand All @@ -28,7 +28,7 @@
@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {

private final JwtUtil jwtUtil;
private final JwtService jwtService;
private final RedisDao redisDao;

@Override
Expand All @@ -40,14 +40,14 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

String accessToken = header.substring(7);

Claims claims = jwtUtil.decode(accessToken);
Claims claims = jwtService.decode(accessToken);

boolean valid = !claims.getExpiration().before(new Date());
if (valid) {
if (redisDao.getValues(accessToken) == null) {
// 토큰으로부터 유저 정보를 받아옵니다.
String memberId = claims.get("id", String.class);
Authentication authentication = jwtUtil.getAuthentication(memberId);
Authentication authentication = jwtService.getAuthentication(memberId);
// SecurityContext 에 Authentication 객체를 저장합니다.
SecurityContextHolder.getContext().setAuthentication(authentication);
} else request.setAttribute("exception", WRONG_ID_TOKEN);
Expand Down
Loading