Skip to content

Commit

Permalink
Merge pull request #39 from LikeLion-12th-SKHU/develop
Browse files Browse the repository at this point in the history
Develop -> main push
  • Loading branch information
ttttkii913 authored Jul 26, 2024
2 parents 19e1ea3 + 6ebc4c9 commit 68421f1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public static <T> T findByIdOrThrow(Optional<T> optionalEntity, ErrorCode errorC
return optionalEntity.orElseThrow(() ->
new NotFoundException(errorCode, errorCode.getMessage()));
}

public static <T> T findByEmailOrThrow(Optional<T> optionalEntity, ErrorCode errorCode) {
return optionalEntity.orElseThrow(() ->
new NotFoundException(errorCode, errorCode.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class CurationService {
// ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž - ํ๋ ˆ์ด์…˜ ์ƒ์„ฑ
@Transactional
public CurationInfoResDto curationSave(CurationSaveReqDto curationSaveReqDto, Principal principal) {
Long id = Long.parseLong(principal.getName()); // user repo์—์„œ userId๋กœ ๋ฐ”๊ฟ”์•ผ ํ•จ
User user = getUserById(id);
String email = principal.getName();
User user = getUserByEmail(email);

Curation curation = curationSaveReqDto.toEntity(user);
curationRepository.save(curation);
Expand All @@ -42,15 +42,14 @@ public CurationInfoResDto curationSave(CurationSaveReqDto curationSaveReqDto, Pr
// ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž - ํ๋ ˆ์ด์…˜ ์ˆ˜์ •
@Transactional
public CurationInfoResDto curationUpdate(Long curationId, CurationUpdateReqDto curationUpdateReqDto, Principal principal) {
Long id = Long.parseLong(principal.getName()); // user repo์—์„œ userId๋กœ ๋ฐ”๊ฟ”์•ผ ํ•จ
User user = getUserById(id);
String email = principal.getName();
User user = getUserByEmail(email);

Curation curation = getCurationById(curationId);

// ์ˆ˜์ • ๊ถŒํ•œ ํ™•์ธ -- ํ† ํฐ ๋ฐœ๊ธ‰์˜ ์ฃผ์ฒด๊ฐ€ email์ด๊ธฐ์— email๋กœ ์‚ฌ์šฉ์ž ํ™•์ธ์„ ํ•˜์˜€์œผ๋‚˜
// userid๋กœ ํŒ๋‹จ ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•จ. ์ผ๋‹จ crud์™€ ๋กœ๊ทธ์ธ ์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด email๋กœ ํ…Œ์ŠคํŠธ ํ•จ -> ํ…Œ์ŠคํŠธ ์„ฑ๊ณต -> userId๋กœ ๋ฐ”๊พธ๊ธฐ๋กœ ํ•จ
String LoginId = principal.getName();
if (!id.equals(LoginId)) {
// ํ๋ ˆ์ด์…˜ ์ž‘์„ฑ์ž ์ด๋ฉ”์ผ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ - ์ˆ˜์ • ๊ถŒํ•œ ํ™•์ธ
String CurationUserEmail = curation.getUser().getEmail();
if (!email.equals(CurationUserEmail)) {
throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION
, ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage());
}
Expand All @@ -65,11 +64,11 @@ public CurationInfoResDto curationUpdate(Long curationId, CurationUpdateReqDto c
public void curationDelete(Long curationId, Principal principal) {
Curation curation = getCurationById(curationId);

// ์‚ญ์ œ ๊ถŒํ•œ ํ™•์ธ
Long id = Long.parseLong(principal.getName());
Long LoginId = Long.parseLong(principal.getName());
// ํ๋ ˆ์ด์…˜ ์ž‘์„ฑ์ž ์ด๋ฉ”์ผ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ - ์‚ญ์ œ ๊ถŒํ•œ ํ™•์ธ
String email = principal.getName();
String CurationUserEmail = curation.getUser().getEmail();

if (!id.equals(LoginId)) {
if (!email.equals(CurationUserEmail)) {
throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION
, ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage());
}
Expand Down Expand Up @@ -123,8 +122,8 @@ private Curation getCurationById(Long curationId) {
, ErrorCode.CURATIONS_NOT_FOUND_EXCEPTION);
}

private User getUserById(Long userId) {
return EntityFinder.findByIdOrThrow(userRepository.findById(userId)
private User getUserByEmail(String email) {
return EntityFinder.findByEmailOrThrow(userRepository.findByEmail(email)
, ErrorCode.USER_NOT_FOUND_EXCEPTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class LikeService {
public void likeSave(@PathVariable("curationId") Long curationId, Principal principal) {
Curation curation = getCurationById(curationId);

Long id = Long.parseLong(principal.getName());
User user = getUserById(id);
String email = principal.getName();
User user = getUserByEmail(email);

Like like = Like.of(curation, user);
likeRepository.save(like);
Expand All @@ -45,8 +45,8 @@ public void likeSave(@PathVariable("curationId") Long curationId, Principal prin
public void likeDelete(@PathVariable("curationId") Long curationId, Principal principal) {
Curation curation = getCurationById(curationId);

Long id = Long.parseLong(principal.getName());
User user = getUserById(id);
String email = principal.getName();
User user = getUserByEmail(email);

// ํ๋ ˆ์ด์…˜์— ์ข‹์•„์š”๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
List<Like> existingLikes = likeRepository.findByCurationAndUser(curation, user);
Expand All @@ -68,8 +68,8 @@ private Curation getCurationById(Long curationId) {
, ErrorCode.CURATIONS_NOT_FOUND_EXCEPTION);
}

private User getUserById(Long userId) {
return EntityFinder.findByIdOrThrow(userRepository.findById(userId)
private User getUserByEmail(String email) {
return EntityFinder.findByEmailOrThrow(userRepository.findByEmail(email)
, ErrorCode.USER_NOT_FOUND_EXCEPTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

@Builder
public record LocationInfoResDto(
Long locationId,
Long curationId,
String name,
String description,
Expand All @@ -13,6 +14,7 @@ public record LocationInfoResDto(
) {
public static LocationInfoResDto from(Location location) {
return LocationInfoResDto.builder()
.locationId(location.getId())
.curationId(location.getCuration().getId())
.name(location.getName())
.description(location.getDescription())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ public class LocationService {
@Transactional
public LocationInfoResDto locationSave(LocationSaveReqDto locationSaveReqDto, MultipartFile multipartFile
, Long curationId, Principal principal) throws IOException {
Long id = Long.parseLong(principal.getName());
User user = getUserById(id);
String email = principal.getName();
User user = getUserByEmail(email);

Curation curation = getCurationById(curationId);

String CurationUserEmail = curation.getUser().getEmail();
if (!email.equals(CurationUserEmail)) {
throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION
, ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage());
}

String locationImage = s3Service.upload(multipartFile, "location");

Location location = locationSaveReqDto.toEntity(locationImage, curation, user);
Expand All @@ -52,9 +58,9 @@ public LocationInfoResDto locationSave(LocationSaveReqDto locationSaveReqDto, Mu
// ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž - ๊ณ ๋ฅธ ์œ„์น˜ ์กฐํšŒ
@Transactional
public LocationListResDto locationFindAll(Principal principal) {
Long id = Long.parseLong(principal.getName());
String email = principal.getName();

List<Location> locations = locationRepository.findByUserId(id);
List<Location> locations = locationRepository.findByUserEmail(email);

List<LocationInfoResDto> locationInfoResDtoList = locations.stream()
.map(LocationInfoResDto::from)
Expand All @@ -69,10 +75,10 @@ public LocationInfoResDto locationUpdate(Long locationId, LocationUpdateReqDto l
Location location = getLocationById(locationId);

// ์ˆ˜์ • ๊ถŒํ•œ ํ™•์ธ
Long id = location.getUser().getId();
Long LoginId = Long.parseLong(principal.getName());
String email = principal.getName();
String LocationUserEmail = location.getUser().getEmail();

if (!id.equals(LoginId)) {
if (!email.equals(LocationUserEmail)) {
throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION
, ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage());
}
Expand All @@ -93,10 +99,10 @@ public LocationInfoResDto locationUpdate(Long locationId, LocationUpdateReqDto l
public void locationDelete(Long locationId, Principal principal) {
Location location = getLocationById(locationId);
// ์‚ญ์ œ ๊ถŒํ•œ ํ™•์ธ
Long id = location.getUser().getId();
Long LoginId = Long.parseLong(principal.getName());
String email = principal.getName();
String LocationUserEmail = location.getUser().getEmail();

if (!id.equals(LoginId)) {
if (!email.equals(LocationUserEmail)) {
throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION
, ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage());
}
Expand All @@ -110,8 +116,8 @@ private Curation getCurationById(Long curationId) {
, ErrorCode.CURATIONS_NOT_FOUND_EXCEPTION);
}

private User getUserById(Long userId) {
return EntityFinder.findByIdOrThrow(userRepository.findById(userId)
private User getUserByEmail(String email) {
return EntityFinder.findByEmailOrThrow(userRepository.findByEmail(email)
, ErrorCode.USER_NOT_FOUND_EXCEPTION);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public interface LocationRepository extends JpaRepository<Location, Long> {

List<Location> findByCurationId(Long curationId);
List<Location> findByUserId(Long UserId);
List<Location> findByUserEmail(String email);
}

0 comments on commit 68421f1

Please sign in to comment.