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 @@ -71,7 +71,7 @@ public List<FollowResponseDto> getAllFollowedPhotographers(Customer customer) {
}

public Follow findByCustomerAndPhotographer_Id(Long customerId, Long photographerId) {

return followRepository.findByCustomer_IdAndPhotographer_Id(customerId, photographerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,28 +53,17 @@ public ResponseEntity<PhotographerSearchListDTO> searchPhotographer(

}

/*
// 특정 작가 상단부 조회
@GetMapping("/{photographerId}/intro")
public ResponseEntity<PhotographerIntroDTO> getPhotographerIntro(@PathVariable Long photographerId) {

PhotographerIntroDTO dto = photographerService.getPhotographerIntro(photographerId);

return ResponseEntity.ok(dto);
}
*/

// 특정 작가 상단부 조회
@GetMapping("/{photographerId}/intro")
public ResponseEntity<PhotographerIntroDTO> getPhotographerIntro(@PathVariable Long photographerId,
@AuthenticationPrincipal CustomUserDetails customUserDetails) {

PhotographerIntroDTO dto = photographerService.getPhotographerIntro(photographerId, customUserDetails);

return ResponseEntity.ok(dto);
}


// 특정 작가 하단부 조회
@GetMapping("/{photographerId}/bottom")
public ResponseEntity<PhotographerBottomDTO> getPhotographerBottom(@PathVariable Long photographerId,
Expand All @@ -95,4 +85,12 @@ public ResponseEntity<PhotographerListDTO> getActivePhotographer() {

return ResponseEntity.ok(photographerListDTO);
}

// 내 id 찾기
@GetMapping("/myId")
public ResponseEntity<PhotographerIdDTO> getMyId(@AuthenticationPrincipal CustomUserDetails customUserDetails) {

PhotographerIdDTO dto = photographerService.getMyId(customUserDetails);
return ResponseEntity.ok(dto);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import ceos.phototoground.domain.photoProfile.entity.QPhotoProfile;
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;
Expand Down Expand Up @@ -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)
Expand All @@ -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) {

Expand Down Expand Up @@ -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);
}
}
31 changes: 18 additions & 13 deletions src/main/java/ceos/phototoground/global/jwt/JWTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Loading