Skip to content

Commit 4c8ff73

Browse files
committed
refactor: ProductPriceController 내부 null체크 간소화
1 parent 9b50967 commit 4c8ff73

File tree

4 files changed

+21
-37
lines changed

4 files changed

+21
-37
lines changed

src/main/java/com/moaguide/refactor/article/service/ArticleOverviewService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.moaguide.refactor.article.dto.ArticleOverviewDto;
44
import com.moaguide.refactor.article.repository.ArticleContentRepository;
55
import com.moaguide.refactor.enums.ArticleCategory;
6-
import com.moaguide.refactor.util.TimeServie;
6+
import com.moaguide.refactor.util.TimeUtil;
77
import java.util.List;
88
import lombok.AllArgsConstructor;
99
import lombok.extern.slf4j.Slf4j;
@@ -22,7 +22,7 @@ public class ArticleOverviewService {
2222
// 인기 콘텐츠 (조회수 기준 정렬)
2323
public List<ArticleOverviewDto> getPopularContents(int popularLimit) {
2424
Pageable pageable = PageRequest.of(0, popularLimit, Sort.by(Sort.Direction.DESC, "views"));
25-
return articleContentRepository.findContentsByViews(pageable, TimeServie.getNowTimestamp())
25+
return articleContentRepository.findContentsByViews(pageable, TimeUtil.getNowTimestamp())
2626
.getContent();
2727
}
2828

@@ -31,7 +31,7 @@ public List<ArticleOverviewDto> getRecentContents(int recentLimit) {
3131
Pageable pageable = PageRequest.of(0, recentLimit,
3232
Sort.by(Sort.Direction.DESC, "createdAt"));
3333
return articleContentRepository.findContentsWithCategory(pageable,
34-
TimeServie.getNowTimestamp())
34+
TimeUtil.getNowTimestamp())
3535
.getContent();
3636
}
3737

@@ -41,7 +41,7 @@ public List<ArticleOverviewDto> getNewsContents(ArticleCategory articleCategory,
4141
Pageable pageable = PageRequest.of(0, newsLimit, Sort.by(Sort.Direction.DESC, "createdAt"));
4242

4343
return articleContentRepository.findByCategory(articleCategory.getId(), pageable,
44-
TimeServie.getNowTimestamp())
44+
TimeUtil.getNowTimestamp())
4545
.getContent();
4646
}
4747
}

src/main/java/com/moaguide/refactor/article/service/ArticleQueryService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.moaguide.refactor.article.repository.ArticleContentRepository;
66
import com.moaguide.refactor.article.repository.ArticleLikeRepository;
77
import com.moaguide.refactor.enums.ArticleCategory;
8-
import com.moaguide.refactor.util.TimeServie;
8+
import com.moaguide.refactor.util.TimeUtil;
99
import lombok.AllArgsConstructor;
1010
import lombok.extern.slf4j.Slf4j;
1111
import org.springframework.data.domain.Page;
@@ -27,19 +27,19 @@ public class ArticleQueryService {
2727
public Page<ArticleQueryDto> getContentsByCategory(int categoryId, int page) {
2828
Pageable pageable = PageRequest.of(page - 1, 5, Sort.by(Sort.Direction.DESC, "createdAt"));
2929
return articleContentRepository.findByCategoryId(categoryId, pageable,
30-
TimeServie.getNowTimestamp())
30+
TimeUtil.getNowTimestamp())
3131
.map(this::mapToContentDto);
3232
}
3333

3434
// 전체 콘텐츠 조회
3535
public Page<ArticleQueryDto> getContentsByAll(ArticleCategory articleCategory, int page) {
3636
Pageable pageable = PageRequest.of(page - 1, 5, Sort.by(Sort.Direction.DESC, "createdAt"));
3737
if (articleCategory == ArticleCategory.ALL) {
38-
return articleContentRepository.findAllContent(pageable, TimeServie.getNowTimestamp())
38+
return articleContentRepository.findAllContent(pageable, TimeUtil.getNowTimestamp())
3939
.map(this::mapToContentDto);
4040
} else {
4141
return articleContentRepository.findByCategoryId(articleCategory.getId(), pageable,
42-
TimeServie.getNowTimestamp())
42+
TimeUtil.getNowTimestamp())
4343
.map(this::mapToContentDto);
4444
}
4545
}
@@ -50,11 +50,11 @@ public Page<ArticleQueryDto> getContentsByType(String type, ArticleCategory arti
5050
Pageable pageable = PageRequest.of(page - 1, 5, Sort.by(Sort.Direction.DESC, "createdAt"));
5151
if (articleCategory == ArticleCategory.ALL) {
5252
return articleContentRepository.findByTypeContent(type, pageable,
53-
TimeServie.getNowTimestamp())
53+
TimeUtil.getNowTimestamp())
5454
.map(this::mapToContentDto);
5555
} else {
5656
return articleContentRepository.findByTypeAndCategoryId(type, articleCategory.getId(),
57-
pageable, TimeServie.getNowTimestamp())
57+
pageable, TimeUtil.getNowTimestamp())
5858
.map(this::mapToContentDto);
5959
}
6060
}

src/main/java/com/moaguide/refactor/product/controller/ProductPriceController.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.moaguide.refactor.product.controller;
22

3+
import static com.moaguide.refactor.util.EmptyCheckUtil.isEmpty;
4+
import static com.moaguide.refactor.util.EmptyCheckUtil.isListEmpty;
5+
import static com.moaguide.refactor.util.HashMapUtil.createEmptyHashMap;
6+
37
import com.moaguide.refactor.product.dto.DetailTransactionResponseDto;
48
import com.moaguide.refactor.product.dto.DivideGraphDto;
59
import com.moaguide.refactor.product.service.DivideService;
610
import com.moaguide.refactor.product.service.TransactionService;
7-
import java.util.ArrayList;
8-
import java.util.HashMap;
9-
import java.util.Map;
1011
import lombok.AllArgsConstructor;
1112
import org.springframework.http.ResponseEntity;
1213
import org.springframework.web.bind.annotation.GetMapping;
@@ -27,19 +28,9 @@ public ResponseEntity<Object> divide(@PathVariable String product_Id, @RequestPa
2728
DivideGraphDto divideCustomDtos = divideService.getGraphData(product_Id,
2829
month);
2930
// null 체크
30-
if (divideCustomDtos == null) {
31-
Map<String, Object> response = new HashMap<>();
32-
response.put("divide", new ArrayList<>());
33-
return ResponseEntity.ok(response);
34-
}
35-
36-
// 빈 리스트 체크
37-
if (divideCustomDtos.getDivide().isEmpty()) {
38-
Map<String, Object> response = new HashMap<>();
39-
response.put("divide", new ArrayList<>());
40-
return ResponseEntity.ok(response);
31+
if (isEmpty(divideCustomDtos) || isListEmpty(divideCustomDtos.getDivide())) {
32+
return ResponseEntity.ok(createEmptyHashMap("divide"));
4133
}
42-
4334
// 정상적인 경우 데이터 반환
4435
return ResponseEntity.ok().body(divideCustomDtos);
4536
}
@@ -49,18 +40,10 @@ public ResponseEntity<Object> transaction(@PathVariable String product_Id,
4940
@RequestParam int month) {
5041
DetailTransactionResponseDto transaction = transactionService.findbymonth(product_Id,
5142
month);
52-
if (transaction.getTransaction() == null) {
53-
Map<String, Object> response = new HashMap<>();
54-
response.put("transaction", new ArrayList<>());
55-
return ResponseEntity.ok(response);
43+
if (isEmpty(transaction) || isEmpty(transaction.getTransaction())) {
44+
return ResponseEntity.ok(createEmptyHashMap("transaction"));
5645
}
5746

58-
// 빈 리스트 체크
59-
if (transaction.getTransaction().isEmpty()) {
60-
Map<String, Object> response = new HashMap<>();
61-
response.put("transaction", new ArrayList<>());
62-
return ResponseEntity.ok(response);
63-
}
6447

6548
return ResponseEntity.ok().body(transaction);
6649
}

src/main/java/com/moaguide/refactor/util/EmptyCheckUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.moaguide.refactor.util;
22

33

4+
import java.util.List;
45

56
public class EmptyCheckUtil {
67

@@ -12,8 +13,8 @@ public static boolean isEmpty(Object obj) {
1213
}
1314
}
1415

15-
public static boolean isListEmpty(Object[] list) {
16-
if (list == null || list.length == 0) {
16+
public static boolean isListEmpty(List<?> list) {
17+
if (list == null) {
1718
return true;
1819
}else {
1920
return false;

0 commit comments

Comments
 (0)