Skip to content

Commit

Permalink
feat: 단일 기프티콘 조회 구현
Browse files Browse the repository at this point in the history
월렛 화면에서 단일 기프티콘 조회하기(walletDetail())를 구현하였습니다.
- walletDetail()을 생성했습니다(Controller, Service).
- HomeReceiverService의 일부 코드를 모듈화하여 함수로 빼냈습니다.
- 사용자 Role 검증을 Service에서 제거하였습니다(HomeReceiverService.receiverGetOneGift. 추후 Security Config로 책임 전가).
  • Loading branch information
Ganghee-Lee-0522 committed Apr 25, 2024
1 parent 0a72d99 commit b0675e9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/main/java/zero/eight/donut/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests((auth) -> {
auth
.requestMatchers(permitList).permitAll()
////////////////////////////////////////
/* 역할 검증이 필요한 uri 추가하기 .permit */
////////////////////////////////////////
.anyRequest().authenticated();
})
// jwtFilter를 UsernamePasswordAuthenticationFilter 앞에 추가
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zero.eight.donut.common.response.ApiResponse;
Expand All @@ -18,4 +19,9 @@ public class WalletController {
public ApiResponse<?> walletMain() {
return walletService.walletMain();
}

@GetMapping("/{giftId}")
public ApiResponse<?> walletDetail(@PathVariable Long giftId) {
return walletService.walletDetail(giftId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.time.LocalDateTime;

@Getter
public class ReceiverGetGiftResponseDto {
public class GetGiftResponseDto {
private String product;
private Integer price;
private LocalDateTime dueDate;
Expand All @@ -18,7 +18,7 @@ public class ReceiverGetGiftResponseDto {
private Long boxId;

@Builder
public ReceiverGetGiftResponseDto(String product, Integer price, LocalDateTime dueDate, String imgUrl, Store store, Status status, Long boxId){
public GetGiftResponseDto(String product, Integer price, LocalDateTime dueDate, String imgUrl, Store store, Status status, Long boxId){
this.product = product;
this.price = price;
this.dueDate = dueDate;
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/zero/eight/donut/service/HomeReceiverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,39 @@ public ApiResponse receiverGetOneBox(Long boxId){
}
@Transactional
public ApiResponse receiverGetOneGift(Long giftId){
// 수혜자 여부 검증
if (!authUtils.getCurrentUserRole().equals(Role.ROLE_RECEIVER)) {
return ApiResponse.failure(Error.NOT_AUTHENTICATED_EXCEPTION);
}

// Security Config로 책임 전가
// // 수혜자 여부 검증
// if (!authUtils.getCurrentUserRole().equals(Role.ROLE_RECEIVER)) {
// return ApiResponse.failure(Error.NOT_AUTHENTICATED_EXCEPTION);
// }

//Gift 있는지 확인
Optional<Gift> giftOptional = giftRepository.findById(giftId);
if(giftOptional.isEmpty())
return ApiResponse.failure(Error.GIFT_NOT_FOUND_EXCEPTION);

Gift gift = giftOptional.get();

ReceiverGetGiftResponseDto responseDto = ReceiverGetGiftResponseDto.builder()
// 재사용을 위해 함수로 변경
/*
GetGiftResponseDto responseDto = GetGiftResponseDto.builder()
.product(gift.getProduct())
.price(gift.getPrice())
.dueDate(gift.getDueDate())
.imgUrl(gift.getImageUrl())
.store(gift.getStore())
.status(gift.getStatus())
.boxId(gift.getGiftbox().getId())
.build();
*/

return ApiResponse.success(Success.HOME_RECEIVER_GIFT_SUCCESS, getGiftInfo(giftId, gift));
}

public GetGiftResponseDto getGiftInfo(Long giftId, Gift gift){

return GetGiftResponseDto.builder()
.product(gift.getProduct())
.price(gift.getPrice())
.dueDate(gift.getDueDate())
Expand All @@ -149,6 +171,5 @@ public ApiResponse receiverGetOneGift(Long giftId){
.status(gift.getStatus())
.boxId(gift.getGiftbox().getId())
.build();
return ApiResponse.success(Success.HOME_RECEIVER_GIFT_SUCCESS, responseDto);
}
}
29 changes: 25 additions & 4 deletions src/main/java/zero/eight/donut/service/WalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
import lombok.RequiredArgsConstructor;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zero.eight.donut.common.response.ApiResponse;
import zero.eight.donut.config.jwt.AuthUtils;
import zero.eight.donut.domain.Gift;
import zero.eight.donut.domain.Giver;
import zero.eight.donut.domain.enums.Store;
import zero.eight.donut.dto.auth.Role;
import zero.eight.donut.dto.wallet.WalletGiftInfoDto;
import zero.eight.donut.dto.wallet.WalletResponseDto;
import zero.eight.donut.exception.Error;
import zero.eight.donut.exception.Success;
import zero.eight.donut.repository.GiftRepository;
import zero.eight.donut.repository.ReceiverRepository;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

@RequiredArgsConstructor
Expand All @@ -29,7 +29,9 @@ public class WalletService {
private final AuthUtils authUtils;
private final ReceiverRepository receiverRepository;
private final GiftRepository giftRepository;
private final HomeReceiverService homeReceiverService;

@Transactional
// 월렛 화면 조회
public ApiResponse<?> walletMain() {
// 변수 선언
Expand Down Expand Up @@ -106,4 +108,23 @@ public Map<Store, Long> countGiftsByStore(List<Gift> giftList) {
return giftList.stream()
.collect(Collectors.groupingBy(Gift::getStore, Collectors.counting()));
}

@Transactional
public ApiResponse<?> walletDetail(Long giftId) {

// Security Config로 책임 전가
// // 기부자 여부 검증
// if (!authUtils.getCurrentUserRole().equals(Role.ROLE_GIVER)) {
// return ApiResponse.failure(Error.NOT_AUTHENTICATED_EXCEPTION);
// }

//Gift 있는지 확인
Optional<Gift> giftOptional = giftRepository.findById(giftId);
if(giftOptional.isEmpty())
return ApiResponse.failure(Error.GIFT_NOT_FOUND_EXCEPTION);

Gift gift = giftOptional.get();

return ApiResponse.success(Success.HOME_RECEIVER_GIFT_SUCCESS, homeReceiverService.getGiftInfo(giftId, gift));
}
}

0 comments on commit b0675e9

Please sign in to comment.