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

πŸ”¨ Refactor/#34: User의 온기 총 횟수 계산 λ‘œμ§μ—μ„œ κ°€κ²Œλ³„ 쀑볡 제거 #35

Merged
merged 1 commit into from
Nov 18, 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 @@ -52,8 +52,8 @@ public ReadOnjungBriefResponseDto execute(UUID accountId) {
// μ˜¨μ • 생성
Onjung onjung = onjungService.createOnjung(donations, receipts, shares);

// μ˜¨μ • 총 개수 계산
Integer totalOnjungCount = onjungService.calculateTotalOnjungCount(onjung);
// μ˜¨μ • 총 개수 계산 (κ°€κ²Œλ³„ 쀑볡 μ œμ™Έ)
Integer totalOnjungCount = onjungService.calculateTotalUniqueOnjungStoreCount(onjung);

// μ˜¨μ • 총 κΈˆμ•‘ 계산
Integer totalOnjungAmount = onjungService.calculateTotalOnjungAmount(onjung);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public ReadOnjungCountResponseDto execute(
// Onjung 생성
Onjung onjung = onjungService.createOnjung(donations, receipts, shares);

// User의 총 μ˜¨μ • 횟수 쑰회
Integer totalOnjungCount = onjungService.calculateTotalOnjungCount(onjung);
// User의 총 μ˜¨μ • 횟수 쑰회 (κ°€κ²Œ 쀑볡 μ œμ™Έ)
Integer totalOnjungCount = onjungService.calculateTotalUniqueOnjungStoreCount(onjung);

return ReadOnjungCountResponseDto.builder()
.totalOnjungCount(totalOnjungCount)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.daon.onjung.onjung.domain.service;

import com.daon.onjung.account.domain.Store;
import com.daon.onjung.account.domain.User;
import com.daon.onjung.onjung.domain.Donation;
import com.daon.onjung.onjung.domain.Onjung;
Expand All @@ -15,6 +16,7 @@
@Service
public class OnjungService {

// κΈ°λΆ€, 영수증, 곡유 객체λ₯Ό λ°›μ•„μ„œ μ˜¨μ • 객체λ₯Ό 생성
public Onjung createOnjung(
List<Donation> donations,
List<Receipt> receipts,
Expand All @@ -27,10 +29,32 @@ public Onjung createOnjung(
.build();
}

// μ˜¨μ • 객체λ₯Ό λ°›μ•„μ„œ 총 μ˜¨μ • 횟수λ₯Ό 계산
public Integer calculateTotalOnjungCount(Onjung onjung) {
return onjung.getDonations().size() + onjung.getReceipts().size() + onjung.getShares().size();
}

// μ˜¨μ • 객체λ₯Ό λ°›μ•„μ„œ 총 μ˜¨μ • 횟수λ₯Ό 계산(κ°€κ²Œ 쀑볡 μ œμ™Έ)
public Integer calculateTotalUniqueOnjungStoreCount(Onjung onjung) {
Set<Store> uniqueStores = new HashSet<>();

// 각 λ¦¬μŠ€νŠΈμ—μ„œ Store 객체λ₯Ό μˆ˜μ§‘ν•˜μ—¬ Set에 μΆ”κ°€
uniqueStores.addAll(onjung.getDonations().stream()
.map(Donation::getStore)
.collect(Collectors.toSet()));

uniqueStores.addAll(onjung.getReceipts().stream()
.map(Receipt::getStore)
.collect(Collectors.toSet()));

uniqueStores.addAll(onjung.getShares().stream()
.map(Share::getStore)
.collect(Collectors.toSet()));

return uniqueStores.size();
}
Comment on lines +37 to +55
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Optimize stream operations and reduce duplication

While the implementation correctly handles store uniqueness, it can be optimized by combining the streams and reducing code duplication.

 public Integer calculateTotalUniqueOnjungStoreCount(Onjung onjung) {
-    Set<Store> uniqueStores = new HashSet<>();
-
-    // 각 λ¦¬μŠ€νŠΈμ—μ„œ Store 객체λ₯Ό μˆ˜μ§‘ν•˜μ—¬ Set에 μΆ”κ°€
-    uniqueStores.addAll(onjung.getDonations().stream()
-            .map(Donation::getStore)
-            .collect(Collectors.toSet()));
-
-    uniqueStores.addAll(onjung.getReceipts().stream()
-            .map(Receipt::getStore)
-            .collect(Collectors.toSet()));
-
-    uniqueStores.addAll(onjung.getShares().stream()
-            .map(Share::getStore)
-            .collect(Collectors.toSet()));
-
-    return uniqueStores.size();
+    return Stream.of(
+            onjung.getDonations().stream().map(Donation::getStore),
+            onjung.getReceipts().stream().map(Receipt::getStore),
+            onjung.getShares().stream().map(Share::getStore)
+        )
+        .flatMap(Function.identity())
+        .collect(Collectors.toSet())
+        .size();
 }
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// μ˜¨μ • 객체λ₯Ό λ°›μ•„μ„œ 총 μ˜¨μ • 횟수λ₯Ό 계산(κ°€κ²Œ 쀑볡 μ œμ™Έ)
public Integer calculateTotalUniqueOnjungStoreCount(Onjung onjung) {
Set<Store> uniqueStores = new HashSet<>();
// 각 λ¦¬μŠ€νŠΈμ—μ„œ Store 객체λ₯Ό μˆ˜μ§‘ν•˜μ—¬ Set에 μΆ”κ°€
uniqueStores.addAll(onjung.getDonations().stream()
.map(Donation::getStore)
.collect(Collectors.toSet()));
uniqueStores.addAll(onjung.getReceipts().stream()
.map(Receipt::getStore)
.collect(Collectors.toSet()));
uniqueStores.addAll(onjung.getShares().stream()
.map(Share::getStore)
.collect(Collectors.toSet()));
return uniqueStores.size();
}
// μ˜¨μ • 객체λ₯Ό λ°›μ•„μ„œ 총 μ˜¨μ • 횟수λ₯Ό 계산(κ°€κ²Œ 쀑볡 μ œμ™Έ)
public Integer calculateTotalUniqueOnjungStoreCount(Onjung onjung) {
return Stream.of(
onjung.getDonations().stream().map(Donation::getStore),
onjung.getReceipts().stream().map(Receipt::getStore),
onjung.getShares().stream().map(Share::getStore)
)
.flatMap(Function.identity())
.collect(Collectors.toSet())
.size();
}


// μ˜¨μ • 객체λ₯Ό λ°›μ•„μ„œ 총 ν•¨κ»˜ν•œ μ˜¨μ •μΈ 수λ₯Ό 계산(쀑볡 μ œμ™Έ)
public Integer calculateTotalUniqueOnjungUserCount(Onjung onjung) {
Set<User> uniqueUsers = new HashSet<>();

Expand Down
Loading