Skip to content

Commit

Permalink
Merge branch 'main' into feat/report
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaktmchl committed Mar 15, 2023
2 parents fd549ff + 69cad2d commit fcbe262
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.yogit.server.applelogin.model.ServicesResponse;
import com.yogit.server.applelogin.model.TokenResponse;
import com.yogit.server.applelogin.util.AppleUtils;
import com.yogit.server.global.service.TokenService;
import com.yogit.server.user.dto.request.CreateUserAppleReq;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.entity.UserStatus;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class AppleServiceImpl implements AppleService {
private final AppleUtils appleUtils;
private final UserService userService;
private final UserRepository userRepository;
private final TokenService tokenService;

@Value("${APPLE.AUD}")
String client_id;
Expand Down Expand Up @@ -163,7 +165,7 @@ public void deleteUser(DeleteUserReq deleteUserReq) {
restTemplate.postForEntity(revokeUrl, httpEntity, String.class);

// 유저 정보 삭제 및 유저 상태 변경 (DELETE)
userService.validateRefreshToken(deleteUserReq.getUserId(), deleteUserReq.getRefreshToken());
tokenService.validateRefreshToken(deleteUserReq.getUserId(), deleteUserReq.getRefreshToken());
User user = userRepository.findByUserId(deleteUserReq.getUserId()).orElseThrow(() -> new NotFoundUserException());
user.deleteUser();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface BlockRepository extends JpaRepository<Block, Long> {

@Query("select bl from Block bl where bl.blockingUser.id = :blockingUserId and bl.status = 'ACTIVE'")
List<Block> findBlocksByBlockingUserId(@Param("blockingUserId") Long blockingUserId);

boolean existsByBlockingUserIdAndBlockedUserId(Long blockingUserId, Long blockedUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.yogit.server.block.dto.req.CreateBlockReq;
import com.yogit.server.block.dto.res.BlockRes;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.user.entity.User;

public interface BlockService {

ApplicationResponse<BlockRes> createBlock(CreateBlockReq createBlockReq);
boolean isBlockingUser(Long blockingUserId, Long blockedUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.yogit.server.board.repository.BoardUserRepository;
import com.yogit.server.config.domain.BaseStatus;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.global.service.TokenService;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.exception.NotFoundUserException;
import com.yogit.server.user.repository.UserRepository;
Expand All @@ -28,15 +29,15 @@ public class BlockServiceImpl implements BlockService{

private final BlockRepository blockRepository;
private final UserRepository userRepository;
private final UserService userService;
private final BoardRepository boardRepository;
private final BoardUserRepository boardUserRepository;
private final TokenService tokenService;

@Override
@Transactional(readOnly = false)
public ApplicationResponse<BlockRes> createBlock(CreateBlockReq dto){

userService.validateRefreshToken(dto.getBlockingUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getBlockingUserId(), dto.getRefreshToken());

// 차단 생성하는 유저 조회
User blockingUser = userRepository.findByUserId(dto.getBlockingUserId())
Expand Down Expand Up @@ -93,4 +94,9 @@ public ApplicationResponse<BlockRes> createBlock(CreateBlockReq dto){
BlockRes res = BlockRes.toDto(block);
return ApplicationResponse.create("자단하였습니다.", res);
}

@Override
public boolean isBlockingUser(Long blockingUserId, Long blockedUserId){
return blockRepository.existsByBlockingUserIdAndBlockedUserId(blockingUserId, blockedUserId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.yogit.server.board.repository.CategoryRepository;
import com.yogit.server.board.repository.BoardRepository;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.global.service.TokenService;
import com.yogit.server.s3.AwsS3Service;
import com.yogit.server.user.entity.City;
import com.yogit.server.user.entity.User;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class BoardServiceImpl implements BoardService{
private final BlockRepository blockRepository;
private final UserService userService;
private final BoardUserRepository boardUserRepository;
private final TokenService tokenService;

private static final int PAGING_SIZE = 10;
private static final String PAGING_STANDARD = "date";
Expand All @@ -57,7 +59,7 @@ public class BoardServiceImpl implements BoardService{
@Override
public ApplicationResponse<GetBoardRes> createBoard(CreateBoardReq dto){

userService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken());

// host 조회
User host = userRepository.findByUserId(dto.getHostId())
Expand Down Expand Up @@ -124,7 +126,7 @@ public ApplicationResponse<GetBoardRes> createBoard(CreateBoardReq dto){
@Transactional(readOnly = false)
@Override
public ApplicationResponse<GetBoardRes> updateBoard(PatchBoardReq dto){
userService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getHostId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down Expand Up @@ -188,7 +190,7 @@ public ApplicationResponse<GetBoardRes> updateBoard(PatchBoardReq dto){
@Override
public ApplicationResponse<DeleteBoardRes> deleteBoard(DeleteBoardReq dto){

userService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken());

Board board = boardRepository.findBoardById(dto.getBoardId())
.orElseThrow(() -> new NotFoundBoardException());
Expand All @@ -210,7 +212,7 @@ public ApplicationResponse<DeleteBoardRes> deleteBoard(DeleteBoardReq dto){
@Override
public ApplicationResponse<List<List<GetAllBoardRes>>> findAllBoards(GetAllBoardsReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

int cursor = dto.getCursor();

Expand Down Expand Up @@ -245,7 +247,7 @@ public ApplicationResponse<List<List<GetAllBoardRes>>> findAllBoards(GetAllBoard
@Override
public ApplicationResponse<List<GetAllBoardRes>> findMyClubBoards(GetMyClubBoardsReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

int cursor = dto.getCursor();

Expand Down Expand Up @@ -297,7 +299,7 @@ else if(dto.getMyClubType().equals(MyClubType.APPLIED_CLUB.toString())){
@Override
public ApplicationResponse<GetAllBoardsByCategoryRes> findAllBoardsByCategory(GetAllBoardsByCategoryReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

int cursor = dto.getCursor();

Expand Down Expand Up @@ -340,7 +342,7 @@ public ApplicationResponse<GetAllBoardsByCategoryRes> findAllBoardsByCategory(Ge
@Override
public ApplicationResponse<List<List<GetAllBoardRes>>> findBoardsByCategories(GetBoardsByCategoriesReq dto) {

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

int cursor = dto.getCursor();
List<List<GetAllBoardRes>> boardsByCategories = new ArrayList<>();
Expand Down Expand Up @@ -382,7 +384,7 @@ public ApplicationResponse<List<List<GetAllBoardRes>>> findBoardsByCategories(Ge
@Override
public ApplicationResponse<GetBoardRes> findBoard(GetBoardReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down Expand Up @@ -411,7 +413,7 @@ public ApplicationResponse<GetBoardRes> findBoard(GetBoardReq dto){
@Override
public ApplicationResponse<DeleteBoardImageRes> deleteBoardImage(DeleteBoardImageReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.yogit.server.board.repository.BoardRepository;
import com.yogit.server.board.repository.BoardUserRepository;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.global.service.TokenService;
import com.yogit.server.s3.AwsS3Service;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.entity.UserStatus;
Expand All @@ -39,12 +40,13 @@ public class BoardUserServiceImpl implements BoardUserService{
private final UserService userService;
private final AwsS3Service awsS3Service;
private final APNService apnService;
private final TokenService tokenService;

@Transactional(readOnly = false)
@Override
public ApplicationResponse<BoardUserRes> joinBoardUser(CreateBoardUserReq dto) {

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down Expand Up @@ -99,7 +101,7 @@ public ApplicationResponse<BoardUserRes> joinBoardUser(CreateBoardUserReq dto) {
@Override
public ApplicationResponse<BoardUserRes> approveBoardUser(CreateBoardUserReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down Expand Up @@ -139,7 +141,7 @@ public ApplicationResponse<BoardUserRes> approveBoardUser(CreateBoardUserReq dto
@Override
public ApplicationResponse<Void> delBoardUser(CreateBoardUserReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.yogit.server.board.repository.CommentRepository;
import com.yogit.server.config.domain.BaseStatus;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.global.service.TokenService;
import com.yogit.server.s3.AwsS3Service;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.entity.UserStatus;
Expand Down Expand Up @@ -49,12 +50,13 @@ public class ClipBoardServiceImpl implements ClipBoardService{
private final BlockRepository blockRepository;
private final UserService userService;
private final APNService apnService;
private final TokenService tokenService;

@Transactional(readOnly = false)
@Override
public ApplicationResponse<ClipBoardRes> createClipBoard(CreateClipBoardReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down Expand Up @@ -97,7 +99,7 @@ public ApplicationResponse<ClipBoardRes> createClipBoard(CreateClipBoardReq dto)
@Override
public ApplicationResponse<GetClipBoardRes> findClipBoard(GetClipBoardReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down Expand Up @@ -154,7 +156,7 @@ public ApplicationResponse<GetClipBoardRes> findClipBoard(GetClipBoardReq dto){
@Override
public ApplicationResponse<GetClipBoardsRes> findAllClipBoards(GetAllClipBoardsReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

int cursor = dto.getCursor();
int totalPage=0;
Expand Down Expand Up @@ -197,7 +199,7 @@ public ApplicationResponse<GetClipBoardsRes> findAllClipBoards(GetAllClipBoardsR
@Override
public ApplicationResponse<ClipBoardRes> updateClipBoard(PatchClipBoardReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

//필요 객체 조회
User user = userRepository.findByUserId(dto.getUserId())
Expand Down Expand Up @@ -227,7 +229,7 @@ public ApplicationResponse<ClipBoardRes> updateClipBoard(PatchClipBoardReq dto){
@Override
public ApplicationResponse<String> deleteClipBoard(DeleteClipBoardReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.yogit.server.board.repository.ClipBoardRepository;
import com.yogit.server.board.repository.CommentRepository;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.global.service.TokenService;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.exception.InvalidTokenException;
import com.yogit.server.user.exception.NotFoundUserException;
Expand All @@ -37,13 +38,14 @@ public class CommentServiceImpl implements CommentService{
private final ClipBoardRepository clipBoardRepository;
private final BlockRepository blockRepository;
private final UserService userService;
private final TokenService tokenService;


@Transactional(readOnly = false)
@Override
public ApplicationResponse<CommentRes> createComment(CreateCommentReq dto){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand All @@ -61,7 +63,7 @@ public ApplicationResponse<CommentRes> createComment(CreateCommentReq dto){
@Override
public ApplicationResponse<List<CommentRes>> findAllComments(Long clipBoardId, Long userId, GetCommentsReq dto){

userService.validateRefreshToken(userId, dto.getRefreshToken());
tokenService.validateRefreshToken(userId, dto.getRefreshToken());

User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundUserException());
Expand All @@ -86,7 +88,7 @@ public ApplicationResponse<List<CommentRes>> findAllComments(Long clipBoardId, L
@Override
public ApplicationResponse<DeleteCommentRes> deleteComment(DeleteCommentReq dto, Long commentId){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand All @@ -109,7 +111,7 @@ public ApplicationResponse<DeleteCommentRes> deleteComment(DeleteCommentReq dto,
@Override
public ApplicationResponse<CommentRes> updateComment(PatchCommentReq dto, Long commentId){

userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken());

User user = userRepository.findByUserId(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.yogit.server.global.service;

import com.yogit.server.applelogin.exception.InvalidRefreshTokenException;
import com.yogit.server.applelogin.exception.NotFoundRefreshTokenException;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.exception.NotFoundUserException;
import com.yogit.server.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class TokenService {
private final UserRepository userRepository;

/**
* 리프레시 토큰 검증
*
* refresh_token은 만료되지 않기 때문에 권한이 필요한 요청일 경우
* 굳이 매번 애플 ID 서버로부터 refresh_token을 통해 access_token을 발급 받기보다는
* 유저의 refresh_token을 따로 DB나 기타 저장소에 저장해두고 캐싱해두고 조회해서 검증하는편이 성능면에서 낫다는 자료를 참고
* https://hwannny.tistory.com/71
*/
public Void validateRefreshToken(Long userId, String refreshToken){
User user = userRepository.findByUserId(userId).orElseThrow(() -> new NotFoundUserException());

if(user.getRefreshToken() == null) throw new NotFoundRefreshTokenException();

if(!user.getRefreshToken().equals(refreshToken)) throw new InvalidRefreshTokenException();

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.yogit.server.board.exception.clipboard.NotFoundClipBoardException;
import com.yogit.server.board.repository.ClipBoardRepository;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.global.service.TokenService;
import com.yogit.server.report.dto.req.CreateClipBoardReportReq;
import com.yogit.server.report.dto.res.ClipBoardReportRes;
import com.yogit.server.report.entity.ClipBoardReport;
Expand All @@ -28,13 +29,13 @@ public class ClipBoradReportServiceImpl implements ClipBoradReportService{
private final UserRepository userRepository;
private final ClipBoardRepository clipBoardRepository;
private final UserService userService;

private final TokenService tokenService;

@Override
@Transactional(readOnly = false)
public ApplicationResponse<ClipBoardReportRes> createClipBoardReport(CreateClipBoardReportReq dto) {

userService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken());
tokenService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken());

User reportingUser = userRepository.findByUserId(dto.getReportingUserId())
.orElseThrow(() -> new NotFoundUserException());
Expand Down
Loading

0 comments on commit fcbe262

Please sign in to comment.