-
Notifications
You must be signed in to change notification settings - Fork 1
feat : feed 도메인 리팩토링 #238
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 : feed 도메인 리팩토링 #238
Changes from 17 commits
818345f
6d3d23d
1d10d59
8816947
4242872
041e684
7468e87
a28cdad
7708079
d75369d
404947d
42285d0
0acdec6
dcb9a8f
435e5c5
3d17791
b044453
7742787
acb11f3
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 |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| package com.kau.capstone.entity.food.repository; | ||
|
|
||
| import com.kau.capstone.entity.food.Food; | ||
| import com.kau.capstone.v2.food.exception.FoodNotFoundExceptionV2; | ||
| import lombok.NonNull; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface FoodRepository extends JpaRepository<Food, Long> { | ||
| default @NonNull Food getById(@NonNull Long id){ | ||
| return findById(id).orElseThrow(FoodNotFoundExceptionV2::new); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,6 @@ public static FoodsResponse toResponse(List<Food> foods) { | |
| .map(FoodResponse::toResponse) | ||
| .toList(); | ||
|
|
||
| return FoodsResponse.builder() | ||
| .foods(foodResponses) | ||
| .build(); | ||
| return new FoodsResponse(foodResponses); | ||
|
Collaborator
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. 팩토리 메서드(of)로 사용하는 데, new를 사용하는 것은 잘못된 방식인 것 같아요.(record 타입 기본 생성자 private으로 변환 필요해 보임. 모든 인원이 해당 되며, @NoArgsConstructor 활용하면 될 듯 합니다.)
Contributor
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. 이것도 v1인데 고쳐야 하나요..?
Collaborator
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. 쏘리 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| package com.kau.capstone.v1.food.exception; | ||
|
|
||
| import com.kau.capstone._core.exception.ApiException; | ||
| import com.kau.capstone.global.exception.ApplicationException; | ||
| import com.kau.capstone.global.exception.ErrorCode; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public class FoodNotFoundException extends ApplicationException { | ||
| public class FoodNotFoundException extends ApiException { | ||
| private static final String message = "해당 사료를 찾을 수 없습니다."; | ||
|
|
||
| public FoodNotFoundException(ErrorCode errorCode) { | ||
| super(errorCode); | ||
| public FoodNotFoundException() { | ||
| super(HttpStatus.NOT_FOUND, message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.kau.capstone.v2.food.controller; | ||
|
|
||
| import com.kau.capstone._core.dto.ApiResponse; | ||
| import com.kau.capstone.v1.auth.dto.LoginInfo; | ||
| import com.kau.capstone.v1.auth.util.LoginUser; | ||
| import com.kau.capstone.v1.point.dto.DeliveryFeeRequest; | ||
| import com.kau.capstone.v1.point.service.PointService; | ||
| import com.kau.capstone.v2.food.dto.response.FoodsResponseV2; | ||
| import com.kau.capstone.v2.food.service.FoodServiceV2; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class FoodControllerV2 { | ||
| private final FoodServiceV2 foodServiceV2; | ||
| private final PointService pointService; | ||
|
|
||
| @GetMapping("/api/v2/foods") | ||
| public ResponseEntity<ApiResponse<FoodsResponseV2>> getFoodsInfo() { | ||
| FoodsResponseV2 response = foodServiceV2.getFoodsInfo(); | ||
|
|
||
| return ApiResponse.ok(response); | ||
| } | ||
|
|
||
| @PostMapping("/api/v2/foods/{foodId}/points/payment") | ||
| public ResponseEntity<ApiResponse<Void>> payFoodWithPoints(@LoginUser LoginInfo loginInfo, | ||
| @PathVariable Long foodId, | ||
| @RequestBody DeliveryFeeRequest request) { | ||
| foodServiceV2.processPointPaymentForFood(loginInfo.memberId(), foodId, request.deliveryFee()); | ||
|
|
||
|
Comment on lines
+28
to
+32
Collaborator
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. controller랑 service랑 로직이 1대1로서 같은 의미인데, 메서드 명이 다른 것이 확인하기 힘든 것 같은데,,, 다들 어떻게 생각해요?
Contributor
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. 이건 저도 하나로 통일하는게 좋을 것 같아요 |
||
| return ApiResponse.ok(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.kau.capstone.v2.food.dto.response; | ||
|
|
||
| import com.kau.capstone.entity.food.Food; | ||
|
|
||
| public record FoodResponseV2( | ||
| Long id, | ||
| String imageUrl, | ||
| Long price | ||
| ) { | ||
|
|
||
| public static FoodResponseV2 of(Food food) { | ||
| return new FoodResponseV2( | ||
| food.getId(), | ||
| food.getImageUrl(), | ||
| food.getPrice() | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.kau.capstone.v2.food.dto.response; | ||
|
|
||
| import com.kau.capstone.entity.food.Food; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record FoodsResponseV2( | ||
| List<FoodResponseV2> foods | ||
| ) { | ||
|
|
||
| public static FoodsResponseV2 of(List<Food> foods) { | ||
| List<FoodResponseV2> foodResponses = foods.stream() | ||
| .map(FoodResponseV2::of) | ||
| .toList(); | ||
|
|
||
| return new FoodsResponseV2(foodResponses); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.kau.capstone.v2.food.exception; | ||
|
|
||
| import com.kau.capstone._core.exception.ApiException; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public class FoodNotFoundExceptionV2 extends ApiException { | ||
| private static final String message = "해당 사료를 찾을 수 없습니다."; | ||
|
|
||
| public FoodNotFoundExceptionV2() { | ||
| super(HttpStatus.NOT_FOUND, message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.kau.capstone.v2.food.service; | ||
|
|
||
| import com.kau.capstone.entity.alarm.Alarm; | ||
| import com.kau.capstone.entity.alarm.AlarmDetail; | ||
| import com.kau.capstone.entity.alarm.repository.AlarmRepository; | ||
| import com.kau.capstone.entity.food.Food; | ||
| import com.kau.capstone.entity.food.repository.FoodRepository; | ||
| import com.kau.capstone.entity.member.Member; | ||
| import com.kau.capstone.entity.member.repository.MemberRepository; | ||
| import com.kau.capstone.entity.point.History; | ||
| import com.kau.capstone.entity.point.Point; | ||
| import com.kau.capstone.entity.point.repository.HistoryRepository; | ||
| import com.kau.capstone.entity.reward.Reward; | ||
| import com.kau.capstone.entity.reward.RewardDetail; | ||
| import com.kau.capstone.entity.reward.repository.RewardRepository; | ||
| import com.kau.capstone.v2.food.dto.response.FoodsResponseV2; | ||
| import com.kau.capstone.v2.point.exception.PointNotEnoughExceptionV2; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| @Service | ||
| @Transactional | ||
| @RequiredArgsConstructor | ||
| public class FoodServiceV2 { | ||
|
|
||
| private final FoodRepository foodRepository; | ||
| private final MemberRepository memberRepository; | ||
| private final RewardRepository rewardRepository; | ||
| private final AlarmRepository alarmRepository; | ||
| private final HistoryRepository historyRepository; | ||
|
|
||
| public FoodsResponseV2 getFoodsInfo() { | ||
| List<Food> foods = foodRepository.findAll(); | ||
|
|
||
| return FoodsResponseV2.of(foods); | ||
| } | ||
|
|
||
| public void processPointPaymentForFood(Long memberId, Long foodId, Long deliveryFee) { | ||
| Member member = memberRepository.getById(memberId); | ||
| Point point = member.getPoint(); | ||
|
|
||
| Food food = foodRepository.getById(foodId); | ||
| long totalPrice = food.getPrice() + deliveryFee; | ||
|
|
||
| checkAmount(point.getAmount(), totalPrice); | ||
|
|
||
| point.payment(totalPrice); | ||
| History history = History.of(point, -totalPrice, food.getName()); | ||
| historyRepository.save(history); | ||
|
|
||
| Reward reward = rewardRepository.findRewardByMemberAndType(member, RewardDetail.THREE.type()); | ||
| if (!Objects.isNull(reward) && !reward.getIsAchieved()) { | ||
| reward.achievedSuccess(); | ||
| } | ||
|
|
||
| Alarm alarm = alarmRepository.findAlarmByMemberAndType(member, AlarmDetail.ONE.type()); | ||
| if (!Objects.isNull(alarm) && alarm.getIsVisible()) { | ||
| alarm.doNotVisible(); | ||
| } | ||
| } | ||
|
|
||
| private void checkAmount(long amount, long point){ | ||
| if (amount < point) { | ||
| throw new PointNotEnoughExceptionV2(); | ||
| } | ||
| } | ||
|
Comment on lines
+66
to
+70
Collaborator
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. 굳이 이 로직만 따로 함수화한 이유가 있나요? |
||
| } | ||
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.
of로 통일하기로 했어요.
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.
v1 코드는 안고치는거 아니었나요?
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.
쏘리