Skip to content

Commit

Permalink
fix: add voucher-read access-right-error
Browse files Browse the repository at this point in the history
  • Loading branch information
정인희 authored and 정인희 committed Jul 27, 2023
1 parent 0e52ba5 commit 8dfa075
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package org.swmaestro.repl.gifthub.vouchers.service;

import lombok.RequiredArgsConstructor;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.swmaestro.repl.gifthub.auth.service.MemberService;
import org.swmaestro.repl.gifthub.exception.BusinessException;
import org.swmaestro.repl.gifthub.exception.ErrorCode;
import org.swmaestro.repl.gifthub.util.DateConverter;
import org.swmaestro.repl.gifthub.vouchers.dto.*;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherReadResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseResponseDto;
import org.swmaestro.repl.gifthub.vouchers.entity.Voucher;
import org.swmaestro.repl.gifthub.vouchers.entity.VoucherUsageHistory;
import org.swmaestro.repl.gifthub.vouchers.repository.VoucherRepository;
import org.swmaestro.repl.gifthub.vouchers.repository.VoucherUsageHistoryRepository;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -54,12 +60,17 @@ public VoucherSaveResponseDto save(String username, VoucherSaveRequestDto vouche
/*
기프티콘 상세 조회 메서드
*/
public VoucherReadResponseDto read(Long id) {
public VoucherReadResponseDto read(Long id, String username) {
Optional<Voucher> voucher = voucherRepository.findById(id);
List<Voucher> vouchers = voucherRepository.findAllByMemberUsername(username);

if (voucher == null) {
if (voucher.isEmpty()) {
throw new BusinessException("존재하지 않는 상품권 입니다.", ErrorCode.NOT_FOUND_RESOURCE);
}
if (!vouchers.contains(voucher.get())) {
throw new BusinessException("상품권을 조회할 권한이 없습니다.", ErrorCode.ACCESS_DENIED);
}

VoucherReadResponseDto voucherReadResponseDto = mapToDto(voucher.get());
return voucherReadResponseDto;
}
Expand Down Expand Up @@ -87,7 +98,8 @@ public VoucherSaveResponseDto update(Long voucherId, VoucherUpdateRequestDto vou
.orElseThrow(() -> new BusinessException("존재하지 않는 상품권 입니다.", ErrorCode.NOT_FOUND_RESOURCE));

voucher.setBarcode(
voucherUpdateRequestDto.getBarcode() == null ? voucher.getBarcode() : voucherUpdateRequestDto.getBarcode());
voucherUpdateRequestDto.getBarcode() == null ? voucher.getBarcode() :
voucherUpdateRequestDto.getBarcode());
voucher.setBrand(voucherUpdateRequestDto.getBrandName() == null ? voucher.getBrand() :
brandService.read(voucherUpdateRequestDto.getBrandName()));
voucher.setProduct(voucherUpdateRequestDto.getProductName() == null ? voucher.getProduct() :
Expand Down Expand Up @@ -135,7 +147,6 @@ public VoucherUseResponseDto use(String username, Long voucherId, VoucherUseRequ
throw new BusinessException("유효기간이 만료된 상품권 입니다.", ErrorCode.EXIST_RESOURCE);
}


VoucherUsageHistory voucherUsageHistory = VoucherUsageHistory.builder()
.member(memberService.read(username))
.voucher(voucher.get())
Expand Down

0 comments on commit 8dfa075

Please sign in to comment.