Skip to content
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

Feature/point details #49 회원 식별 로직 추가 #182

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.t3t.bookstoreapi.pointdetail.model.response.PointDetailResponse;
import com.t3t.bookstoreapi.pointdetail.service.UserPointDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -20,15 +22,21 @@ public class UserPointDetailController {

/**
* 회원의 포인트 타입에 따른 포인트 상세 내역 조회 API
* @param pointDetailType 조회할 포인트 타입(사용/적립)
* @param memberId 회원 ID
* @param pointDetailType 조회할 포인트 타입(사용/적립), null이면 전체 내역 조회
* @return 해당 포인트 상세 내역을 포함한 200(OK) 상태 반환
* @author hydrationn(박수화)
*/
@GetMapping("/mypage/point-details")
public BaseResponse<List<PointDetailResponse>> getPointDetailByPointDetailType(@RequestParam(name = "pointDetailType", required = false) String pointDetailType) {
List<PointDetailResponse> pointDetailResponse = userPointDetailService.getPointDetailByPointDetailType(pointDetailType);
@GetMapping("/member/{memberId}/point-details")
public ResponseEntity<BaseResponse<List<PointDetailResponse>>> getPointDetailByPointDetailType(@PathVariable("memberId") Long memberId,
@RequestParam(name = "pointDetailType", required = false) String pointDetailType) {
List<PointDetailResponse> pointDetailResponse = userPointDetailService.getPointDetailByPointDetailType(memberId, pointDetailType);

return new BaseResponse<List<PointDetailResponse>>().data(pointDetailResponse);
if(pointDetailType==null)
pointDetailResponse = userPointDetailService.getPointDetailList(memberId);


return ResponseEntity.status(HttpStatus.OK).body(new BaseResponse<List<PointDetailResponse>>().data(pointDetailResponse));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
public interface PointDetailRepository extends JpaRepository<PointDetail, Long> {
List<Optional<PointDetail>> findByMemberId(Long memberId);
boolean existsByMemberIdAndPointDetailType(Long memberId, String pointDetailType);
Optional<PointDetail> findByMemberIdAndPointDetailType(Long memberId, String pointDetailType);
List<Optional<PointDetail>> findByMemberIdAndPointDetailType(Long memberId, String pointDetailType);
List<PointDetail> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public List<PointDetailResponse> getPointDetailByPointDetailType(Long memberId,
if(pointDetailRepository.existsByMemberIdAndPointDetailType(memberId, pointDetailType))
throw new PointDetailNotFoundException(pointDetailType);

List<PointDetail> pointDetails = (List<PointDetail>) pointDetailRepository.findByMemberIdAndPointDetailType(memberId, pointDetailType)
.orElseThrow(() -> new PointDetailNotFoundException(pointDetailType));
List<Optional<PointDetail>> pointDetails = pointDetailRepository.findByMemberIdAndPointDetailType(memberId, pointDetailType);

return pointDetails.stream()
.map(optional -> optional.orElseThrow(() -> new PointDetailNotFoundException(pointDetailType)))
.map(PointDetailResponse::of)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.t3t.bookstoreapi.pointdetail.service;

import com.t3t.bookstoreapi.member.exception.MemberNotFoundException;
import com.t3t.bookstoreapi.member.repository.MemberRepository;
import com.t3t.bookstoreapi.pointdetail.exception.PointDetailNotFoundException;
import com.t3t.bookstoreapi.pointdetail.model.entity.PointDetail;
import com.t3t.bookstoreapi.pointdetail.model.response.PointDetailResponse;
Expand All @@ -10,6 +12,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand All @@ -20,28 +23,50 @@
@Transactional
@RequiredArgsConstructor
public class UserPointDetailService {
private final MemberRepository memberRepository;
private final PointDetailRepository pointDetailRepository;

/**
* 포인트 전체 내역 조회
* @param memberId 회원 ID
* @return 조회된 포인트 상세 정보를 {@link PointDetailResponse} 객체로 반환
* @throws PointDetailNotFoundException 포인트 상세 정보를 찾을 수 없는 경우 발생
* @author hydrationn(박수화)
*/
public List<PointDetailResponse> getPointDetailList(Long memberId) {
if(!memberRepository.existsById(memberId))
throw new MemberNotFoundException();

List<Optional<PointDetail>> pointDetailList = pointDetailRepository.findByMemberId(memberId);

return pointDetailList.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.map(PointDetailResponse::of)
.collect(Collectors.toList());
}

/**
* 포인트 타입에 따른 상세 정보 조회
* @param memberId 회원 ID
* @param pointDetailType 조회할 포인트 타입
* @return 조회된 포인트 상세 정보를 {@link PointDetailResponse} 객체로 반환
* @throws PointDetailNotFoundException 포인트 상세 정보를 찾을 수 없는 경우 발생
* @author hydrationn(박수화)
*/
@Transactional(readOnly = true)
public List<PointDetailResponse> getPointDetailByPointDetailType(String pointDetailType) {
public List<PointDetailResponse> getPointDetailByPointDetailType(Long memberId, String pointDetailType) {
if(!memberRepository.existsById(memberId))
throw new MemberNotFoundException();

List<PointDetail> pointDetails = new ArrayList<>();
List<PointDetail> addPointDetails = pointDetailRepository.findAll();
List<Optional<PointDetail>> addPointDetails = pointDetailRepository.findByMemberIdAndPointDetailType(memberId, pointDetailType);

for (int i = 0; i < addPointDetails.size(); i++) {
if(addPointDetails.get(i).getPointDetailType().equals(""))
pointDetails.add(addPointDetails.get(i));
else if (addPointDetails.get(i).getPointDetailType().equals("used")) {
pointDetails.add(addPointDetails.get(i));
if (addPointDetails.get(i).get().getPointDetailType().equals("used")) {
pointDetails.add(addPointDetails.get(i).get());
} else {
pointDetails.add(addPointDetails.get(i));
pointDetails.add(addPointDetails.get(i).get());
}
}

Expand Down
Loading