-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] 식당 좋아요 관련 api #43
Changes from 23 commits
ffba207
78f8e44
d301c60
50d2b95
96e8a51
8ed7d37
ba4e651
0688dcd
0b69897
1d5de90
0f1f997
25d4b61
7b4b3af
011176c
c5053ec
ca7cd7a
91bcb49
ce82be9
728fe8b
3590cb2
c71081c
96dcdbd
d20ca42
fd3ff54
c9db421
94c3af0
0a5cad1
9c0a185
7664272
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.hankki.hankkiserver.api.store.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.api.auth.service.UserFinder; | ||
import org.hankki.hankkiserver.api.store.service.command.StoreDeleteCommand; | ||
import org.hankki.hankkiserver.api.store.service.command.StorePostCommand; | ||
import org.hankki.hankkiserver.api.store.service.response.HeartCreateResponse; | ||
import org.hankki.hankkiserver.api.store.service.response.HeartDeleteResponse; | ||
import org.hankki.hankkiserver.common.code.HeartErrorCode; | ||
import org.hankki.hankkiserver.common.exception.ConflictException; | ||
import org.hankki.hankkiserver.domain.heart.model.Heart; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class HeartCommandService { | ||
|
||
private final HeartUpdater heartUpdater; | ||
private final HeartFinder heartFinder; | ||
private final HeartDeleter heartDeleter; | ||
private final UserFinder userFinder; | ||
private final StoreFinder storeFinder; | ||
|
||
public HeartCreateResponse createHeart(final StorePostCommand storePostCommand) { | ||
Long userId = storePostCommand.userId(); | ||
Long storeId = storePostCommand.storeId(); | ||
validateCreateStoreHeart(userId, storeId); | ||
saveStoreHeart(userId, storeId); | ||
updateStoreHeartCount(storeId, false); | ||
return HeartCreateResponse.of(storeId, true); | ||
} | ||
|
||
public HeartDeleteResponse deleteHeart(final StoreDeleteCommand storeDeleteCommand) { | ||
Long userId = storeDeleteCommand.userId(); | ||
Long storeId = storeDeleteCommand.storeId(); | ||
validateDeleteStoreHeart(userId, storeId); | ||
heartDeleter.deleteHeart(userId,storeId); | ||
updateStoreHeartCount(storeId, true); | ||
return HeartDeleteResponse.of(storeId, false); | ||
} | ||
|
||
private void validateCreateStoreHeart(final Long userId, final Long storeId) { | ||
heartFinder.findByUserIdAndStoreId(userId, storeId) | ||
.ifPresent(heart -> { | ||
throw new ConflictException(HeartErrorCode.ALREADY_EXISTED_HEART); | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. existsby로 boolean 값만 받아오는 것이 더 나아 보입니다. |
||
|
||
private void validateDeleteStoreHeart(final Long userId, final Long storeId) { | ||
heartFinder.findByUserIdAndStoreId(userId, storeId) | ||
.orElseThrow(() -> new ConflictException(HeartErrorCode.ALREADY_DELETED_HEART)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 existby로 바꿔주시고 |
||
private void saveStoreHeart(final Long userId, final Long storeId) { | ||
Heart heart = Heart.createHeart(userFinder.getUserReference(userId), storeFinder.getStoreReference(storeId)); | ||
heartUpdater.saveHeart(heart); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 객체를 매개변수로 받고 한줄로 줄여주세요. heart변수 만들 필요 없어 보입니다. |
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굳이 heart 변수 안 써도 될 거 같습니다. 바로 saveHeart에 넣어주세요 |
||
private void updateStoreHeartCount(final Long storeId, final boolean isDeleted) { | ||
storeFinder.getStore(storeId).updateHearCount(isDeleted); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.hankki.hankkiserver.api.store.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.domain.heart.repository.HeartRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class HeartDeleter { | ||
|
||
private final HeartRepository heartRepository; | ||
|
||
public void deleteHeart(final Long userId, final Long storeId) { | ||
heartRepository.deleteByUserIdAndStoreId(userId, storeId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.hankki.hankkiserver.api.store.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.domain.heart.model.Heart; | ||
import org.hankki.hankkiserver.domain.heart.repository.HeartRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class HeartFinder { | ||
|
||
private final HeartRepository heartRepository; | ||
|
||
public Optional<Heart> findByUserIdAndStoreId(final Long userId, final Long storeId) { | ||
return heartRepository.findByUserIdAndStoreId(userId, storeId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hankki.hankkiserver.api.store.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.domain.heart.model.Heart; | ||
import org.hankki.hankkiserver.domain.heart.repository.HeartRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class HeartUpdater { | ||
|
||
private final HeartRepository heartRepository; | ||
|
||
public void saveHeart(final Heart heart) { | ||
heartRepository.save(heart); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,13 @@ protected Store findByIdWhereDeletedIsFalse(Long id) { | |
return storeRepository.findByIdAndIsDeletedIsFalse(id) | ||
.orElseThrow(() -> new NotFoundException(StoreErrorCode.STORE_NOT_FOUND)); | ||
} | ||
|
||
public Store getStore(final Long id) { | ||
return storeRepository.findById(id) | ||
.orElseThrow(()-> new NotFoundException(StoreErrorCode.STORE_NOT_FOUND)); | ||
} | ||
|
||
public Store getStoreReference(Long id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final를 달아주세용 |
||
return storeRepository.getReferenceById(id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.hankki.hankkiserver.api.store.service.command; | ||
|
||
public record StoreDeleteCommand( | ||
Long userId, | ||
Long storeId | ||
) { | ||
public static StoreDeleteCommand of(Long userId, Long storeId) { | ||
return new StoreDeleteCommand(userId, storeId); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 통일성을 위해 final 붙여주세요 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.hankki.hankkiserver.api.store.service.command; | ||
|
||
public record StorePostCommand( | ||
Long userId, | ||
Long storeId | ||
) { | ||
public static StorePostCommand of(Long userId, Long storeId) { | ||
return new StorePostCommand(userId, storeId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.hankki.hankkiserver.api.store.service.response; | ||
|
||
public record HeartCreateResponse( | ||
Long storeId, | ||
boolean isHearted | ||
) { | ||
public static HeartCreateResponse of(Long storeId, boolean isHearted) { | ||
return new HeartCreateResponse(storeId, isHearted); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.hankki.hankkiserver.api.store.service.response; | ||
|
||
public record HeartDeleteResponse( | ||
Long storeId, | ||
boolean isHearted | ||
) { | ||
public static HeartDeleteResponse of(Long storeId, boolean isHearted) { | ||
return new HeartDeleteResponse(storeId, isHearted); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.hankki.hankkiserver.common.code; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum HeartErrorCode implements ErrorCode { | ||
|
||
ALREADY_EXISTED_HEART(HttpStatus.CONFLICT, "이미 좋아요 한 가게입니다."), | ||
ALREADY_DELETED_HEART(HttpStatus.CONFLICT, "이미 좋아요를 취소한 가게입니다."); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.hankki.hankkiserver.common.exception; | ||
|
||
import lombok.Getter; | ||
import org.hankki.hankkiserver.common.code.ErrorCode; | ||
|
||
@Getter | ||
public class ConflictException extends RuntimeException { | ||
private final ErrorCode errorCode; | ||
|
||
public ConflictException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
매개변수에 final 추가해주세요