diff --git a/src/main/java/ceos/phototoground/domain/follow/service/FollowService.java b/src/main/java/ceos/phototoground/domain/follow/service/FollowService.java index 3bcff1c..7c252c4 100644 --- a/src/main/java/ceos/phototoground/domain/follow/service/FollowService.java +++ b/src/main/java/ceos/phototoground/domain/follow/service/FollowService.java @@ -71,7 +71,7 @@ public List getAllFollowedPhotographers(Customer customer) { } public Follow findByCustomerAndPhotographer_Id(Long customerId, Long photographerId) { - + return followRepository.findByCustomer_IdAndPhotographer_Id(customerId, photographerId); } } diff --git a/src/main/java/ceos/phototoground/domain/photographer/controller/PhotographerController.java b/src/main/java/ceos/phototoground/domain/photographer/controller/PhotographerController.java index 70b6b25..7240a47 100644 --- a/src/main/java/ceos/phototoground/domain/photographer/controller/PhotographerController.java +++ b/src/main/java/ceos/phototoground/domain/photographer/controller/PhotographerController.java @@ -3,6 +3,7 @@ import ceos.phototoground.domain.customer.dto.CustomUserDetails; import ceos.phototoground.domain.photographer.dto.PhotographerBottomDTO; +import ceos.phototoground.domain.photographer.dto.PhotographerIdDTO; import ceos.phototoground.domain.photographer.dto.PhotographerIntroDTO; import ceos.phototoground.domain.photographer.dto.PhotographerListDTO; import ceos.phototoground.domain.photographer.dto.PhotographerResponseDTO; @@ -52,16 +53,6 @@ public ResponseEntity searchPhotographer( } - /* - // 특정 작가 상단부 조회 - @GetMapping("/{photographerId}/intro") - public ResponseEntity getPhotographerIntro(@PathVariable Long photographerId) { - - PhotographerIntroDTO dto = photographerService.getPhotographerIntro(photographerId); - - return ResponseEntity.ok(dto); - } - */ // 특정 작가 상단부 조회 @GetMapping("/{photographerId}/intro") @@ -69,11 +60,10 @@ public ResponseEntity getPhotographerIntro(@PathVariable L @AuthenticationPrincipal CustomUserDetails customUserDetails) { PhotographerIntroDTO dto = photographerService.getPhotographerIntro(photographerId, customUserDetails); - + return ResponseEntity.ok(dto); } - // 특정 작가 하단부 조회 @GetMapping("/{photographerId}/bottom") public ResponseEntity getPhotographerBottom(@PathVariable Long photographerId, @@ -95,4 +85,12 @@ public ResponseEntity getActivePhotographer() { return ResponseEntity.ok(photographerListDTO); } + + // 내 id 찾기 + @GetMapping("/myId") + public ResponseEntity getMyId(@AuthenticationPrincipal CustomUserDetails customUserDetails) { + + PhotographerIdDTO dto = photographerService.getMyId(customUserDetails); + return ResponseEntity.ok(dto); + } } diff --git a/src/main/java/ceos/phototoground/domain/photographer/dto/PhotographerIdDTO.java b/src/main/java/ceos/phototoground/domain/photographer/dto/PhotographerIdDTO.java new file mode 100644 index 0000000..e0d1cff --- /dev/null +++ b/src/main/java/ceos/phototoground/domain/photographer/dto/PhotographerIdDTO.java @@ -0,0 +1,15 @@ +package ceos.phototoground.domain.photographer.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class PhotographerIdDTO { + + private Long photographerId; + + public static PhotographerIdDTO from(Long photographerId) { + return new PhotographerIdDTO(photographerId); + } +} diff --git a/src/main/java/ceos/phototoground/domain/photographer/service/PhotographerService.java b/src/main/java/ceos/phototoground/domain/photographer/service/PhotographerService.java index 3dc975f..6a098c8 100644 --- a/src/main/java/ceos/phototoground/domain/photographer/service/PhotographerService.java +++ b/src/main/java/ceos/phototoground/domain/photographer/service/PhotographerService.java @@ -1,6 +1,7 @@ package ceos.phototoground.domain.photographer.service; import ceos.phototoground.domain.customer.dto.CustomUserDetails; +import ceos.phototoground.domain.customer.entity.UserRole; import ceos.phototoground.domain.follow.entity.Follow; import ceos.phototoground.domain.follow.service.FollowService; import ceos.phototoground.domain.photoProfile.entity.PhotoProfile; @@ -8,6 +9,7 @@ import ceos.phototoground.domain.photoProfile.service.PhotoProfileService; import ceos.phototoground.domain.photoProfile.service.PhotoStyleService; import ceos.phototoground.domain.photographer.dto.PhotographerBottomDTO; +import ceos.phototoground.domain.photographer.dto.PhotographerIdDTO; import ceos.phototoground.domain.photographer.dto.PhotographerIntroDTO; import ceos.phototoground.domain.photographer.dto.PhotographerListDTO; import ceos.phototoground.domain.photographer.dto.PhotographerResponseDTO; @@ -115,9 +117,17 @@ public PhotographerIntroDTO getPhotographerIntro(Long photographerId, CustomUser // 로그인 한 사용자 if (customUserDetails != null) { - Long customerId = customUserDetails.getCustomer().getId(); - Follow follow = followService.findByCustomerAndPhotographer_Id(customerId, photographerId); - isFollowing = follow != null; + // 고객인지 작가인지 + String authority = customUserDetails.getAuthorities().iterator().next().getAuthority(); + UserRole role = UserRole.fromAuthority(authority); + Long customerId = (role == UserRole.CUSTOMER) ? + customUserDetails.getCustomer().getId() : + null; + // 고객이라면 following 상태 반영 + if (customerId != null) { + Follow follow = followService.findByCustomerAndPhotographer_Id(customerId, photographerId); + isFollowing = follow != null; + } } Photographer photographer = photographerRepository.findById(photographerId) @@ -135,7 +145,17 @@ public PhotographerIntroDTO getPhotographerIntro(Long photographerId, CustomUser return PhotographerIntroDTO.of(photographer, photoProfile, univNameList, styleList, isFollowing); } - +/* + // 로그인 한 사람이 페이지 주인인지 + private boolean myPage(Long photographerId, CustomUserDetails customUserDetails) { + String authority = customUserDetails.getAuthorities().iterator().next().getAuthority(); + UserRole role = UserRole.fromAuthority(authority); + if (role == UserRole.PHOTOGRAPHER) { + return photographerId.equals(customUserDetails.getPhotographer().getId()); + } + return false; + } +*/ public PhotographerBottomDTO getPhotographerBottom(Long photographerId, Long cursor, int size) { @@ -185,4 +205,9 @@ public Photographer findById(Long photographerId) { return photographerRepository.findById(photographerId) .orElseThrow(() -> new CustomException(ErrorCode.PHOTOGRAPHER_NOT_FOUND)); } + + public PhotographerIdDTO getMyId(CustomUserDetails customUserDetails) { + Long myId = customUserDetails.getPhotographer().getId(); + return PhotographerIdDTO.from(myId); + } } diff --git a/src/main/java/ceos/phototoground/global/jwt/JWTUtil.java b/src/main/java/ceos/phototoground/global/jwt/JWTUtil.java index aeecadf..ce8857c 100644 --- a/src/main/java/ceos/phototoground/global/jwt/JWTUtil.java +++ b/src/main/java/ceos/phototoground/global/jwt/JWTUtil.java @@ -13,37 +13,42 @@ public class JWTUtil { // JWTUtil : 0.12.3 ver private SecretKey secretKey; - public JWTUtil(@Value("${spring.jwt.secret}")String secret) { + public JWTUtil(@Value("${spring.jwt.secret}") String secret) { // application.yml의 jwt를 가져와 객체 변수로 암호화 (HS256) - secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), Jwts.SIG.HS256.key().build().getAlgorithm()); + secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), + Jwts.SIG.HS256.key().build().getAlgorithm()); } public String getUsername(String token) { // 토큰이 서버에서 생성된 게 맞는지 확인 후 이메일을 받아옴 - return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().get("username", String.class); + return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload() + .get("username", String.class); } public String getRole(String token) { - return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().get("role", String.class); + return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload() + .get("role", String.class); } public String getCategory(String token) { - return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().get("category", String.class); + return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload() + .get("category", String.class); } public Boolean isExpired(String token) { - return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().getExpiration().before(new Date()); + return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().getExpiration() + .before(new Date()); } public String createJwt(String category, String email, String role, Long expiredMs) { // JWT 발급 return Jwts.builder() - .claim("category", category) - .claim("username", email) - .claim("role", role) - .issuedAt(new Date(System.currentTimeMillis())) - .expiration(new Date(System.currentTimeMillis() + expiredMs)) - .signWith(secretKey) - .compact(); + .claim("category", category) + .claim("username", email) + .claim("role", role) + .issuedAt(new Date(System.currentTimeMillis())) + .expiration(new Date(System.currentTimeMillis() + expiredMs)) + .signWith(secretKey) + .compact(); } }