Skip to content

Commit

Permalink
Merge pull request #96 from Developer-Wikis/feature/#88
Browse files Browse the repository at this point in the history
Feature/#88
  • Loading branch information
dhkstnaos authored Nov 21, 2022
2 parents 1bef29e + cc41342 commit 0f29fb7
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 32 deletions.
22 changes: 19 additions & 3 deletions src/main/java/com/developer/wiki/bookmark/BookmarkController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.developer.wiki.bookmark;

import com.developer.wiki.common.exception.UnAuthorizedException;
import com.developer.wiki.oauth.User;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,9 +20,22 @@ public class BookmarkController {
private final BookmarkService bookmarkService;

@PostMapping("/{questionId}")
public ResponseEntity getUserInfo(@AuthenticationPrincipal User currentUser,
public ResponseEntity<Boolean> toggle(@AuthenticationPrincipal User currentUser,
@PathVariable Long questionId) {
bookmarkService.toggle(questionId, currentUser.getId());
return ResponseEntity.ok().build();
if (Objects.isNull(currentUser)) {
throw new UnAuthorizedException("토큰이 필요합니다.");
}
Boolean isBookmarked = bookmarkService.toggle(questionId, currentUser.getId());
return ResponseEntity.ok(isBookmarked);
}

@GetMapping("/{questionId}")
public ResponseEntity<Boolean> getBookmarked(@AuthenticationPrincipal User currentUser,
@PathVariable Long questionId) {
if (Objects.isNull(currentUser)) {
throw new UnAuthorizedException("토큰이 필요합니다.");
}
Boolean isBookmarked = bookmarkService.getBookmarked(questionId, currentUser.getId());
return ResponseEntity.ok(isBookmarked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {

Optional<Bookmark> findByUserIdAndQuestion(Long userId, Question question);

Boolean existsByUserIdAndQuestion(Long userId, Question question);
boolean existsByUserIdAndQuestion(Long userId, Question question);
}
20 changes: 19 additions & 1 deletion src/main/java/com/developer/wiki/bookmark/BookmarkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.developer.wiki.oauth.UserRepository;
import com.developer.wiki.question.command.domain.Question;
import com.developer.wiki.question.command.domain.QuestionRepository;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -17,14 +18,30 @@ public class BookmarkService {
private final UserRepository userRepository;
private final BookmarkRepository bookmarkRepository;

public void toggle(Long questionId, Long userId) {
public Boolean toggle(Long questionId, Long userId) {
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 ID입니다."));
AtomicBoolean isBookmarked = new AtomicBoolean();
bookmarkRepository.findByUserIdAndQuestion(userId, question).ifPresentOrElse(bookmark -> {
unBookmark(bookmark);
isBookmarked.set(false);
}, () -> {
bookmark(userId, question);
isBookmarked.set(true);
});
return isBookmarked.get();
}

public Boolean getBookmarked(Long questionId, Long userId) {
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 ID입니다."));
AtomicBoolean isBookmarked = new AtomicBoolean();
bookmarkRepository.findByUserIdAndQuestion(userId, question).ifPresentOrElse(bookmark -> {
isBookmarked.set(true);
}, () -> {
isBookmarked.set(false);
});
return isBookmarked.get();
}

private void bookmark(Long userId, Question question) {
Expand All @@ -35,4 +52,5 @@ private void bookmark(Long userId, Question question) {
private void unBookmark(Bookmark bookmark) {
bookmarkRepository.delete(bookmark);
}

}
33 changes: 18 additions & 15 deletions src/main/java/com/developer/wiki/oauth/jwt/newJwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

import com.developer.wiki.common.exception.BadRequestException;
import com.developer.wiki.common.exception.NotFoundException;
import com.developer.wiki.oauth.TokenService;
import com.developer.wiki.oauth.User;
import com.developer.wiki.oauth.UserRepository;
import com.developer.wiki.oauth.exception.AccessTokenException;
import com.developer.wiki.oauth.util.JwtUtil;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -18,14 +25,6 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;


@Log4j2
@RequiredArgsConstructor
Expand All @@ -37,14 +36,17 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
log.info("Token Check Filter..........................");
try{
String path = request.getRequestURI();
if ((path.startsWith("/api/v1/questions")&&request.getMethod().equals("GET"))||path.startsWith("/api/v1/oauth")||path.startsWith("/api/v1/questions/")) {
if (path.startsWith("/api/v1/oauth")) {
log.info("넘어간다~~~~");
filterChain.doFilter(request, response);
return;
}


String email = validateAccessToken(request, filterChain,response);
String email = validateAccessToken(request);
if(Objects.isNull(email)){
filterChain.doFilter(request,response);
return;
}
log.info("email: " + email);
User user=userRepository.findByEmail(email).orElseThrow(()-> new AccessTokenException(AccessTokenException.TOKEN_ERROR.NOTFOUND));
Authentication auth = getAuthentication(user);
Expand All @@ -64,9 +66,10 @@ private String validateAccessToken(HttpServletRequest request,FilterChain filter

String headerStr = request.getHeader("Authorization");

if(headerStr == null || headerStr.length() < 8){
filterChain.doFilter(request, response);
throw new AccessTokenException(AccessTokenException.TOKEN_ERROR.UNACCEPT);
if(headerStr==null){
System.out.println("222222"+headerStr);
return null;

}

//Bearer 생략
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.developer.wiki.question.command.application.comment;

import com.developer.wiki.common.exception.UnAuthorizedException;
import com.developer.wiki.question.command.application.dto.PasswordRequest;
import com.developer.wiki.question.command.domain.Comment;
import com.developer.wiki.question.command.domain.CommentRepository;
import com.developer.wiki.question.command.domain.EntityNotFoundException;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,9 +17,17 @@ public class CommentDeleteService {

private final CommentRepository commentRepository;

public void delete(Long id, PasswordRequest passwordRequest) {
public void delete(Long id, PasswordRequest passwordRequest, Long userId) {
Comment comment = commentRepository.findById(id).orElseThrow(EntityNotFoundException::new);
checkAuthorization(userId, comment);
comment.matchPassword(passwordRequest.getPassword());
commentRepository.delete(comment);
}

private void checkAuthorization(Long userId, Comment comment) {
if ((!Objects.isNull(comment.getUserId()) && Objects.isNull(userId)) || (
Objects.isNull(comment.getUserId()) && !Objects.isNull(userId))) {
throw new UnAuthorizedException("권한이 필요합니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.developer.wiki.question.command.application.comment;

import com.developer.wiki.common.exception.UnAuthorizedException;
import com.developer.wiki.question.command.application.dto.ModifyCommentRequest;
import com.developer.wiki.question.command.domain.Comment;
import com.developer.wiki.question.command.domain.CommentRepository;
Expand All @@ -18,11 +19,17 @@ public class CommentModifyService {

public void modify(Long id, ModifyCommentRequest modifyCommentRequest, Long userId) {
Comment comment = commentRepository.findById(id).orElseThrow(EntityNotFoundException::new);
if ((Objects.isNull(comment.getUserId()) && Objects.isNull(userId)) || (
!Objects.isNull(comment.getUserId()) && !Objects.isNull(userId))) {
comment.matchPassword(modifyCommentRequest.getPassword());
comment.changePassword(modifyCommentRequest.getPassword());
comment.changeContent(modifyCommentRequest.getContent());
checkAuthorization(userId, comment);
comment.matchPassword(modifyCommentRequest.getPassword());
comment.changePassword(modifyCommentRequest.getPassword());
comment.changeContent(modifyCommentRequest.getContent());
}

private void checkAuthorization(Long userId, Comment comment) {
if ((!Objects.isNull(comment.getUserId()) && Objects.isNull(userId)) || (
Objects.isNull(comment.getUserId()) && !Objects.isNull(userId))) {
throw new UnAuthorizedException("권한이 필요합니다.");
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class Comment {
@JoinColumn(name = "question_id")
private Question question;
@Column(name = "user_id")
private Long userId;
private Long userId = null;

public Comment(String nickname, String password, String content, Question question) {
this.nickname = nickname;
Expand Down Expand Up @@ -89,4 +89,8 @@ public void changePassword(String password) {
public void changeContent(String content) {
this.content = content;
}

public Long getUserId() {
return userId;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.developer.wiki.question.presentation.comment;

import com.developer.wiki.common.exception.UnAuthorizedException;
import com.developer.wiki.oauth.User;
import com.developer.wiki.question.command.application.comment.CommentDeleteService;
import com.developer.wiki.question.command.application.dto.PasswordRequest;
Expand All @@ -24,10 +23,8 @@ public class CommentDeleteController {
@DeleteMapping("/{commentId}")
public ResponseEntity<Void> delete(@AuthenticationPrincipal User currentUser,
@PathVariable Long commentId, @RequestBody PasswordRequest passwordRequest) {
if (Objects.isNull(currentUser)) {
throw new UnAuthorizedException("토큰이 필요합니다.");
}
commentDeleteService.delete(commentId, passwordRequest);
Long userId = Objects.isNull(currentUser) ? null : currentUser.getId();
commentDeleteService.delete(commentId, passwordRequest,userId);
return ResponseEntity.ok(null);
}
}

0 comments on commit 0f29fb7

Please sign in to comment.