3737import java .io .OutputStream ;
3838import java .net .URL ;
3939import java .time .LocalDateTime ;
40+ import java .util .Comparator ;
4041import java .util .List ;
4142import java .util .UUID ;
4243import java .util .zip .ZipEntry ;
@@ -58,37 +59,37 @@ public class ProductService {
5859
5960 public List <ScriptListResponseDTO .ProductListDTO > getPlayList (int page , UserEntity userInfo , PlayType playType , int pageSize , ProductSortType sortType ) {
6061 try {
61- Sort sort = createProductSort (sortType );
62- final Pageable pageable = PageRequest .of (page , pageSize , sort );
63- final List <ProductEntity > plays = productRepo .findAllValidPlays (playType , ProductStatus .PASS , pageable );
64-
65- return plays .stream ()
66- .map (play -> {
67- final ScriptListResponseDTO .ProductListDTO productListDTO = new ScriptListResponseDTO .ProductListDTO ();
68-
69- final String scriptImage = generateScriptImgURL (play );
70-
71- productListDTO .setId (play .getId ());
72- productListDTO .setTitle (play .getTitle ());
73- productListDTO .setWriter (play .getWriter ());
74- productListDTO .setImagePath (scriptImage );
75- productListDTO .setScript (play .getScript ());
76- productListDTO .setScriptPrice (play .getScriptPrice ());
77- productListDTO .setPerformance (play .getPerformance ());
78- productListDTO .setPerformancePrice (play .getPerformancePrice ());
79- productListDTO .setDate (play .getCreatedAt ());
80- productListDTO .setChecked (play .getChecked ());
81- productListDTO .setLike (getProductLikeStatus (userInfo , play .getId ()));
82- productListDTO .setLikeCount (play .getLikeCount ());
83- productListDTO .setViewCount (viewCountService .getProductViewCount (play .getId ()));
84-
85- return productListDTO ;
86- }).toList ();
62+ // POPULAR(조회수 기준 정렬)는 Java단에서 처리
63+ if (sortType == ProductSortType .POPULAR ) {
64+ List <ProductEntity > plays = productRepo .findAllValidPlays (
65+ playType ,
66+ ProductStatus .PASS ,
67+ PageRequest .of (page , pageSize , Sort .unsorted ()) // 정렬 직접 처리
68+ );
69+
70+ return plays .stream ()
71+ .map (play -> getListDto (userInfo , play ))
72+ .sorted (Comparator .comparingLong (ScriptListResponseDTO .ProductListDTO ::getViewCount ).reversed ())
73+ .limit (pageSize )
74+ .toList ();
75+ } else {
76+ Sort sort = createProductSort (sortType );
77+ List <ProductEntity > plays = productRepo .findAllValidPlays (
78+ playType ,
79+ ProductStatus .PASS ,
80+ PageRequest .of (page , pageSize , sort )
81+ );
82+
83+ return plays .stream ()
84+ .map (play -> getListDto (userInfo , play ))
85+ .toList ();
86+ }
8787 } catch (Exception e ) {
88- throw e ;
88+ throw new RuntimeException ( "작품 목록 조회 실패" , e ) ;
8989 }
9090 }
9191
92+
9293 public ProductEntity getProduct (UUID id ) {
9394 try {
9495 return productRepo .findById (id );
@@ -612,4 +613,26 @@ protected void createReviewLike(final ReviewLikeEntity like, final UUID reviewId
612613 throw new RuntimeException ("좋아요 생성 실패" , e );
613614 }
614615 }
616+
617+ private ScriptListResponseDTO .ProductListDTO getListDto (final UserEntity userInfo , final ProductEntity play ) {
618+ ScriptListResponseDTO .ProductListDTO productListDTO = new ScriptListResponseDTO .ProductListDTO ();
619+
620+ final String scriptImage = generateScriptImgURL (play );
621+
622+ productListDTO .setId (play .getId ());
623+ productListDTO .setTitle (play .getTitle ());
624+ productListDTO .setWriter (play .getWriter ());
625+ productListDTO .setImagePath (scriptImage );
626+ productListDTO .setScript (play .getScript ());
627+ productListDTO .setScriptPrice (play .getScriptPrice ());
628+ productListDTO .setPerformance (play .getPerformance ());
629+ productListDTO .setPerformancePrice (play .getPerformancePrice ());
630+ productListDTO .setDate (play .getCreatedAt ());
631+ productListDTO .setChecked (play .getChecked ());
632+ productListDTO .setLike (getProductLikeStatus (userInfo , play .getId ()));
633+ productListDTO .setLikeCount (play .getLikeCount ());
634+ productListDTO .setViewCount (viewCountService .getProductViewCount (play .getId ()));
635+
636+ return productListDTO ;
637+ }
615638}
0 commit comments