diff --git a/src/main/java/aromanticcat/umcproject/converter/MyCollectionConverter.java b/src/main/java/aromanticcat/umcproject/converter/MyCollectionConverter.java index 91b14ad..ec9aa8b 100644 --- a/src/main/java/aromanticcat/umcproject/converter/MyCollectionConverter.java +++ b/src/main/java/aromanticcat/umcproject/converter/MyCollectionConverter.java @@ -1,6 +1,8 @@ package aromanticcat.umcproject.converter; import aromanticcat.umcproject.entity.AcquiredItem; +import aromanticcat.umcproject.entity.MyLetterPaper; +import aromanticcat.umcproject.entity.MyStamp; import aromanticcat.umcproject.web.dto.MyCollectionResponseDTO; public class MyCollectionConverter{ @@ -21,5 +23,22 @@ public static MyCollectionResponseDTO.AcquiredStampResultDTO toAcquiredStampResu .acquiredAt(stamp.getCreatedAt()) .build(); } + public static MyCollectionResponseDTO.AcquiredLetterPaperResultDTO toMyLetterPaperResultDTO(MyLetterPaper myLetterPaper){ + return MyCollectionResponseDTO.AcquiredLetterPaperResultDTO.builder() + .letterPaperId(myLetterPaper.getMPaper_id()) + .letterPaperName(myLetterPaper.getName()) + .letterPaperImageUrl(myLetterPaper.getImageUrl()) + .acquiredAt(myLetterPaper.getCreatedAt()) + .build(); + } + public static MyCollectionResponseDTO.AcquiredStampResultDTO toMyStampResultDTO(MyStamp stamp){ + return MyCollectionResponseDTO.AcquiredStampResultDTO.builder() + .stampId(stamp.getMStamp_id()) + .stampName(stamp.getName()) + .stampImageUrl(stamp.getImageUrl()) + .acquiredAt(stamp.getCreatedAt()) + .build(); + } + } diff --git a/src/main/java/aromanticcat/umcproject/entity/MyLetterPaper.java b/src/main/java/aromanticcat/umcproject/entity/MyLetterPaper.java index 9f9bda0..3859b95 100644 --- a/src/main/java/aromanticcat/umcproject/entity/MyLetterPaper.java +++ b/src/main/java/aromanticcat/umcproject/entity/MyLetterPaper.java @@ -10,7 +10,7 @@ @Setter @RequiredArgsConstructor @Table(name = "myLetterPaper") -public class MyLetterPaper { +public class MyLetterPaper extends BaseEntity{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long mPaper_id; diff --git a/src/main/java/aromanticcat/umcproject/entity/MyStamp.java b/src/main/java/aromanticcat/umcproject/entity/MyStamp.java index b78c74e..03be569 100644 --- a/src/main/java/aromanticcat/umcproject/entity/MyStamp.java +++ b/src/main/java/aromanticcat/umcproject/entity/MyStamp.java @@ -14,7 +14,7 @@ @Builder @RequiredArgsConstructor @Table(name = "myStamp") -public class MyStamp { +public class MyStamp extends BaseEntity{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long mStamp_id; diff --git a/src/main/java/aromanticcat/umcproject/repository/MyLetterPaperRepository.java b/src/main/java/aromanticcat/umcproject/repository/MyLetterPaperRepository.java index 5c5f761..3af1943 100644 --- a/src/main/java/aromanticcat/umcproject/repository/MyLetterPaperRepository.java +++ b/src/main/java/aromanticcat/umcproject/repository/MyLetterPaperRepository.java @@ -1,8 +1,11 @@ package aromanticcat.umcproject.repository; -import aromanticcat.umcproject.entity.Letterbox; import aromanticcat.umcproject.entity.MyLetterPaper; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; public interface MyLetterPaperRepository extends JpaRepository { + Page findByMemberId(Long memberId, Pageable pageable); + } diff --git a/src/main/java/aromanticcat/umcproject/repository/MyStampRepository.java b/src/main/java/aromanticcat/umcproject/repository/MyStampRepository.java index c779b51..2907306 100644 --- a/src/main/java/aromanticcat/umcproject/repository/MyStampRepository.java +++ b/src/main/java/aromanticcat/umcproject/repository/MyStampRepository.java @@ -1,8 +1,12 @@ package aromanticcat.umcproject.repository; -import aromanticcat.umcproject.entity.Letterbox; import aromanticcat.umcproject.entity.MyStamp; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; public interface MyStampRepository extends JpaRepository { + + Page findByMemberId(Long memberId, Pageable pageable); + } diff --git a/src/main/java/aromanticcat/umcproject/service/MyCollectionServiceImpl.java b/src/main/java/aromanticcat/umcproject/service/MyCollectionServiceImpl.java deleted file mode 100644 index e2dfd6d..0000000 --- a/src/main/java/aromanticcat/umcproject/service/MyCollectionServiceImpl.java +++ /dev/null @@ -1,59 +0,0 @@ -package aromanticcat.umcproject.service; - -import aromanticcat.umcproject.converter.MyCollectionConverter; -import aromanticcat.umcproject.entity.AcquiredItem; -import aromanticcat.umcproject.repository.AcquiredItemRepository; -import aromanticcat.umcproject.web.dto.MyCollectionResponseDTO; -import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -import javax.transaction.Transactional; -import java.util.List; -import java.util.stream.Collectors; - -@Service -@RequiredArgsConstructor -@Transactional -public class MyCollectionServiceImpl implements MyCollectionService { - - private final AcquiredItemRepository acquiredItemRepository; - - @Override - @Transactional - public List findLetterPaperList(Long memberId, int page, int pageSize) { - Pageable pageable = PageRequest.of(page, pageSize); - - // 사용자가 구매한 편지지 목록 조회 - Page acquiredLetterPaperPage = acquiredItemRepository.findByMemberIdAndLetterPaperIdIsNotNull(memberId, pageable); - - List acquiredLetterPaperList = acquiredLetterPaperPage.getContent(); - - List responseDTOs = acquiredLetterPaperList.stream() - .map(letterPaper -> MyCollectionConverter.toAcquiredLetterPaperResultDTO(letterPaper)) - .collect(Collectors.toList()); - return responseDTOs; - - } - - @Override - @Transactional - public List findStampList(Long memberId, int page, int pageSize) { - Pageable pageable = PageRequest.of(page, pageSize); - - // 사용자가 구매한 우표 목록 조회 - Page acquiredStampPage = acquiredItemRepository.findByMemberIdAndStampIdIsNotNull(memberId, pageable); - - List acquiredStampList = acquiredStampPage.getContent(); - - List responseDTOs = acquiredStampList.stream() - .map(stamp -> MyCollectionConverter.toAcquiredStampResultDTO(stamp)) - .collect(Collectors.toList()); - return responseDTOs; - - } - - -} diff --git a/src/main/java/aromanticcat/umcproject/service/MyCollectionService.java b/src/main/java/aromanticcat/umcproject/service/myCollectionService/MyCollectionService.java similarity index 54% rename from src/main/java/aromanticcat/umcproject/service/MyCollectionService.java rename to src/main/java/aromanticcat/umcproject/service/myCollectionService/MyCollectionService.java index d4a5b25..1ed94e6 100644 --- a/src/main/java/aromanticcat/umcproject/service/MyCollectionService.java +++ b/src/main/java/aromanticcat/umcproject/service/myCollectionService/MyCollectionService.java @@ -1,4 +1,4 @@ -package aromanticcat.umcproject.service; +package aromanticcat.umcproject.service.myCollectionService; import aromanticcat.umcproject.web.dto.MyCollectionResponseDTO; @@ -6,7 +6,6 @@ public interface MyCollectionService { - List findLetterPaperList(Long memberId, int page, int pageSize); - List findStampList(Long memberId, int page, int pageSize); - + List findLetterPaperList(Long memberId, int page, int pageSize, boolean onlyMyDesign); + List findStampList(Long memberId, int page, int pageSize, boolean onlyMyDesign); } diff --git a/src/main/java/aromanticcat/umcproject/service/myCollectionService/MyCollectionServiceImpl.java b/src/main/java/aromanticcat/umcproject/service/myCollectionService/MyCollectionServiceImpl.java new file mode 100644 index 0000000..e4133e8 --- /dev/null +++ b/src/main/java/aromanticcat/umcproject/service/myCollectionService/MyCollectionServiceImpl.java @@ -0,0 +1,114 @@ +package aromanticcat.umcproject.service.myCollectionService; + +import aromanticcat.umcproject.converter.MyCollectionConverter; +import aromanticcat.umcproject.entity.AcquiredItem; +import aromanticcat.umcproject.entity.MyLetterPaper; +import aromanticcat.umcproject.entity.MyStamp; +import aromanticcat.umcproject.repository.AcquiredItemRepository; +import aromanticcat.umcproject.repository.MyLetterPaperRepository; +import aromanticcat.umcproject.repository.MyStampRepository; +import aromanticcat.umcproject.web.dto.MyCollectionResponseDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +@Transactional +public class MyCollectionServiceImpl implements MyCollectionService { + + private final AcquiredItemRepository acquiredItemRepository; + private final MyLetterPaperRepository myLetterPaperRepository; + private final MyStampRepository myStampRepository; + + @Override + @Transactional + public List findLetterPaperList(Long memberId, int page, int pageSize, boolean onlyMyDesign) { + Pageable pageable = PageRequest.of(page, pageSize); + List responseDTOs; + + if(onlyMyDesign) { + Page myLetterPaperPage = myLetterPaperRepository.findByMemberId(memberId, pageable); + List myLetterPaperList = myLetterPaperPage.getContent(); + + responseDTOs = myLetterPaperList.stream() + .map(myLetterPaper -> MyCollectionConverter.toMyLetterPaperResultDTO(myLetterPaper)) + .collect(Collectors.toList()); + }else { + // 사용자가 구매한 편지지 목록 조회 + Page acquiredLetterPaperPage = acquiredItemRepository.findByMemberIdAndLetterPaperIdIsNotNull(memberId, pageable); + List acquiredLetterPaperList = acquiredLetterPaperPage.getContent(); + + // 마이 디자인 편지지 목록 조회 + Page myLetterPaperPage = myLetterPaperRepository.findByMemberId(memberId, pageable); + List myLetterPaperList = myLetterPaperPage.getContent(); + + // MyLetterPaper와 AcquredItem에서 가져온 편지지 목록 병합 + List myLetterPaperDTOs = myLetterPaperList.stream() + .map(myLetterPaper -> MyCollectionConverter.toMyLetterPaperResultDTO(myLetterPaper)) + .collect(Collectors.toList()); + + List acquiredLetterPaperDTOs = acquiredLetterPaperList.stream() + .map(letterPaper -> MyCollectionConverter.toAcquiredLetterPaperResultDTO(letterPaper)) + .collect(Collectors.toList()); + + responseDTOs = new ArrayList<>(); + responseDTOs.addAll(myLetterPaperDTOs); + responseDTOs.addAll(acquiredLetterPaperDTOs); + } + return responseDTOs; + + } + + @Override + @Transactional + public List findStampList(Long memberId, int page, int pageSize, boolean onlyMyDesign) { + Pageable pageable = PageRequest.of(page, pageSize); + List responseDTOs; + + if(onlyMyDesign){ + Page myStampPage = myStampRepository.findByMemberId(memberId, pageable); + + List myStampList = myStampPage.getContent(); + + responseDTOs = myStampList.stream() + .map(myStamp -> MyCollectionConverter.toMyStampResultDTO(myStamp)) + .collect(Collectors.toList()); + }else{ + // 사용자가 구매한 우표 목록 조회 + Page acquiredStampPage = acquiredItemRepository.findByMemberIdAndStampIdIsNotNull(memberId, pageable); + List acquiredStampList = acquiredStampPage.getContent(); + + // 마이 디자인 우표 목록 조회 + Page myStampPage = myStampRepository.findByMemberId(memberId, pageable); + List myStampList = myStampPage.getContent(); + + // MyStamp와 AcquredItem에서 가져온 우표 목록 병합 + List myStampDTOs = myStampList.stream() + .map(myStamp -> MyCollectionConverter.toMyStampResultDTO(myStamp)) + .collect(Collectors.toList()); + + List acquiredStampDTOs = acquiredStampList.stream() + .map(stamp -> MyCollectionConverter.toAcquiredStampResultDTO(stamp)) + .collect(Collectors.toList()); + + responseDTOs = new ArrayList<>(); + responseDTOs.addAll(myStampDTOs); + responseDTOs.addAll(acquiredStampDTOs); + } + + return responseDTOs; + + } + + + + +} diff --git a/src/main/java/aromanticcat/umcproject/service/MyDesignService.java b/src/main/java/aromanticcat/umcproject/service/myCollectionService/MyDesignService.java similarity index 95% rename from src/main/java/aromanticcat/umcproject/service/MyDesignService.java rename to src/main/java/aromanticcat/umcproject/service/myCollectionService/MyDesignService.java index 6c600ac..2b9df67 100644 --- a/src/main/java/aromanticcat/umcproject/service/MyDesignService.java +++ b/src/main/java/aromanticcat/umcproject/service/myCollectionService/MyDesignService.java @@ -1,4 +1,4 @@ -package aromanticcat.umcproject.service; +package aromanticcat.umcproject.service.myCollectionService; import aromanticcat.umcproject.S3.S3Service; import aromanticcat.umcproject.entity.Member; @@ -7,7 +7,6 @@ import aromanticcat.umcproject.repository.MemberRepository; import aromanticcat.umcproject.repository.MyLetterPaperRepository; import aromanticcat.umcproject.repository.MyStampRepository; -import aromanticcat.umcproject.repository.StampRepository; import aromanticcat.umcproject.web.dto.MyDesignRequest; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/aromanticcat/umcproject/service/StoreService.java b/src/main/java/aromanticcat/umcproject/service/storeService/StoreService.java similarity index 89% rename from src/main/java/aromanticcat/umcproject/service/StoreService.java rename to src/main/java/aromanticcat/umcproject/service/storeService/StoreService.java index f87c304..97bab0a 100644 --- a/src/main/java/aromanticcat/umcproject/service/StoreService.java +++ b/src/main/java/aromanticcat/umcproject/service/storeService/StoreService.java @@ -1,4 +1,4 @@ -package aromanticcat.umcproject.service; +package aromanticcat.umcproject.service.storeService; import aromanticcat.umcproject.web.dto.store.StoreResponseDTO; diff --git a/src/main/java/aromanticcat/umcproject/service/StoreServiceImpl.java b/src/main/java/aromanticcat/umcproject/service/storeService/StoreServiceImpl.java similarity index 99% rename from src/main/java/aromanticcat/umcproject/service/StoreServiceImpl.java rename to src/main/java/aromanticcat/umcproject/service/storeService/StoreServiceImpl.java index 2eefde4..5c2abd7 100644 --- a/src/main/java/aromanticcat/umcproject/service/StoreServiceImpl.java +++ b/src/main/java/aromanticcat/umcproject/service/storeService/StoreServiceImpl.java @@ -1,4 +1,4 @@ -package aromanticcat.umcproject.service; +package aromanticcat.umcproject.service.storeService; import aromanticcat.umcproject.converter.StoreConverter; import aromanticcat.umcproject.entity.AcquiredItem; diff --git a/src/main/java/aromanticcat/umcproject/web/controller/MyCollectionController.java b/src/main/java/aromanticcat/umcproject/web/controller/MyCollectionController.java index 3676e78..ac90f9c 100644 --- a/src/main/java/aromanticcat/umcproject/web/controller/MyCollectionController.java +++ b/src/main/java/aromanticcat/umcproject/web/controller/MyCollectionController.java @@ -1,7 +1,7 @@ package aromanticcat.umcproject.web.controller; import aromanticcat.umcproject.apiPayload.ApiResponse; -import aromanticcat.umcproject.service.MyCollectionService; +import aromanticcat.umcproject.service.myCollectionService.MyCollectionService; import aromanticcat.umcproject.web.dto.MyCollectionResponseDTO; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; @@ -25,12 +25,13 @@ public class MyCollectionController { description = "페이징을 포함합니다. query String으로 page(기본값 0)와 pageSize(기본값 12)를 주세요.") public ApiResponse> getMyLetterPaperList( @RequestParam(defaultValue = "0") int page, - @RequestParam(defaultValue = "12") int pageSize) { + @RequestParam(defaultValue = "12") int pageSize, + @RequestParam(defaultValue = "false")boolean onlyMyDesign) { try { // 로그인한 사용자의 아이디를 가져오는 메서드 Long userId = getCurrentUserId(); - List letterPaperList = myCollectionService.findLetterPaperList(userId, page, pageSize); + List letterPaperList = myCollectionService.findLetterPaperList(userId, page, pageSize, onlyMyDesign); return ApiResponse.onSuccess(letterPaperList); @@ -41,18 +42,19 @@ public ApiResponse> g @GetMapping("/stamp") @Operation(summary = "내 수집함 우표 조회 API", - description = "페이징을 포함합니다. query String으로 page(기본값 0)와 pageSize(기본값 12)를 주세요.") + description = "페이징을 포함합니다. query String으로 page(기본값 0)와 pageSize(기본값 12), onlyMyDesign(기본값 false)를 주세요.") public ApiResponse> getMyStampList( @RequestParam(defaultValue = "0") int page, - @RequestParam(defaultValue = "12") int pageSize) { + @RequestParam(defaultValue = "12") int pageSize, + @RequestParam(defaultValue = "false")boolean onlyMyDesign) { try { // 로그인한 사용자의 아이디를 가져오는 메서드 Long userId = getCurrentUserId(); - List stampList = myCollectionService.findStampList(userId, page, pageSize); - + List stampList = myCollectionService.findStampList(userId, page, pageSize, onlyMyDesign); return ApiResponse.onSuccess(stampList); + } catch (Exception e) { return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null); } diff --git a/src/main/java/aromanticcat/umcproject/web/controller/MyDesignController.java b/src/main/java/aromanticcat/umcproject/web/controller/MyDesignController.java index 9eba7f7..240b13d 100644 --- a/src/main/java/aromanticcat/umcproject/web/controller/MyDesignController.java +++ b/src/main/java/aromanticcat/umcproject/web/controller/MyDesignController.java @@ -1,7 +1,7 @@ package aromanticcat.umcproject.web.controller; import aromanticcat.umcproject.apiPayload.ApiResponse; -import aromanticcat.umcproject.service.MyDesignService; +import aromanticcat.umcproject.service.myCollectionService.MyDesignService; import aromanticcat.umcproject.web.dto.MyDesignRequest; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/aromanticcat/umcproject/web/controller/StoreController.java b/src/main/java/aromanticcat/umcproject/web/controller/StoreController.java index 617dbee..e284205 100644 --- a/src/main/java/aromanticcat/umcproject/web/controller/StoreController.java +++ b/src/main/java/aromanticcat/umcproject/web/controller/StoreController.java @@ -1,7 +1,7 @@ package aromanticcat.umcproject.web.controller; import aromanticcat.umcproject.apiPayload.ApiResponse; -import aromanticcat.umcproject.service.StoreService; +import aromanticcat.umcproject.service.storeService.StoreService; import aromanticcat.umcproject.web.dto.store.StoreResponseDTO; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor;