Skip to content

Commit

Permalink
[refactor] 엔티티 생성자 수정 (#348)
Browse files Browse the repository at this point in the history
* #333 refactor: Member 생성자 수정

- 빌더 private 변경
- 생성자 접근제한자 private 변경

* #347 refactor: Portfolio 생성자 수정

* #347 refactor: PortfolioHolding 생성자 수정

- 빌더 제거
- 생성자 private 설정

* #347 refactor: Member 생성자의 빌더 제거

* #347 refactor: Portfolio생성자에서 빌더 제거

* #347 refactor: StockDividend 생성자의 빌더 제거

* #347 refactor: PurchaseHistory 생성자 수정

* #347 refactor: fcmToken, Notification 생성자 수정

* #347 fix: 종목 지정가 알림 조건 버그 수정

* #347 refactor: NotificationPreference 생성자 수정

* #347 refactor: PortfolioGainHistory 생성자 수정

* #347 refactor: WatchList, WatchStock 생성자 수정\n -빌더 제거\n -생성자의 접근제한자를 private로 변경하여 캡슐화

* #347 refactor: StockTargetPrice, TargetPriceNoticiation 생성자 수정

* #347 fix: 정적 팩토리 메서드 수정
  • Loading branch information
yonghwankim-dev authored Jun 7, 2024
1 parent 9ead4f1 commit ccaff01
Show file tree
Hide file tree
Showing 69 changed files with 1,158 additions and 2,133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
Expand Down Expand Up @@ -59,8 +58,7 @@ public class StockDividend extends BaseEntity {
private static final ExDividendDateCalculator EX_DIVIDEND_DATE_CALCULATOR = new ExDividendDateCalculator(
new HolidayRepository(new HolidayFileReader()));

@Builder
protected StockDividend(LocalDateTime createAt, LocalDateTime modifiedAt, Long id, Money dividend,
private StockDividend(LocalDateTime createAt, LocalDateTime modifiedAt, Long id, Money dividend,
LocalDate recordDate, LocalDate exDividendDate, LocalDate paymentDate, Stock stock) {
super(createAt, modifiedAt);
this.id = id;
Expand All @@ -74,14 +72,13 @@ protected StockDividend(LocalDateTime createAt, LocalDateTime modifiedAt, Long i
public static StockDividend create(Money dividend, LocalDate recordDate,
LocalDate paymentDate, Stock stock) {
LocalDate exDividendDate = EX_DIVIDEND_DATE_CALCULATOR.calculate(recordDate);
return create(dividend, recordDate, exDividendDate, paymentDate, stock);
}

return StockDividend.builder()
.dividend(dividend)
.recordDate(recordDate)
.exDividendDate(exDividendDate)
.paymentDate(paymentDate)
.stock(stock)
.build();
public static StockDividend create(Money dividend, LocalDate recordDate, LocalDate exDividendDate,
LocalDate paymentDate, Stock stock) {
return new StockDividend(LocalDateTime.now(), null, null, dividend, recordDate, exDividendDate, paymentDate,
stock);
}

// 주식 개수에 따른 배당금 합계 계산
Expand Down Expand Up @@ -151,7 +148,7 @@ public void change(StockDividend stockDividend) {
* 배당 일정 정보들을 파싱하여 반환
* format : tickerSymbol:dividend:recordDate:exDividendDate:paymentDate
* - ex) 005930:361:2022-08-01:2022-08-01:2022-08-01, 005930:361:2022-08-01:2022-08-01:null
* @return
* @return 배당 일정 정보 요약
*/
public String parse() {
return String.format("%s:%s:%s:%s:%s", stock.getTickerSymbol(), dividend, recordDate, exDividendDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package codesquad.fineants.domain.fcm.domain.dto.request;

import java.time.LocalDateTime;

import codesquad.fineants.domain.fcm.domain.entity.FcmToken;
import codesquad.fineants.domain.member.domain.entity.Member;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -20,10 +18,6 @@ public class FcmRegisterRequest {
private String fcmToken;

public FcmToken toEntity(Member member) {
return FcmToken.builder()
.token(fcmToken)
.member(member)
.latestActivationTime(LocalDateTime.now())
.build();
return FcmToken.create(member, fcmToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ToString(exclude = "member")
@Table(name = "fcm_token", uniqueConstraints = {
@UniqueConstraint(name = "token_member_id_unique", columnNames = {"token", "member_id"})
Expand All @@ -40,12 +41,12 @@ public class FcmToken extends BaseEntity {

private LocalDateTime latestActivationTime;

@Builder
public FcmToken(Long id, Member member, String token, LocalDateTime latestActivationTime) {
this.id = id;
this.member = member;
this.token = token;
this.latestActivationTime = latestActivationTime;
public static FcmToken create(Member member, String token) {
return create(null, member, token);
}

public static FcmToken create(Long id, Member member, String token) {
return new FcmToken(id, member, token, LocalDateTime.now());
}

public void refreshLatestActivationTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Transient;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -66,8 +65,7 @@ protected PortfolioHolding() {
this.purchaseHistory = new ArrayList<>();
}

@Builder
public PortfolioHolding(LocalDateTime createAt, LocalDateTime modifiedAt, Long id,
private PortfolioHolding(LocalDateTime createAt, LocalDateTime modifiedAt, Long id,
Portfolio portfolio, Stock stock, Money currentPrice) {
super(createAt, modifiedAt);
this.id = id;
Expand All @@ -80,12 +78,16 @@ public static PortfolioHolding empty(Portfolio portfolio, Stock stock) {
return of(portfolio, stock, Money.zero());
}

public static PortfolioHolding of(Portfolio portfolio, Stock stock) {
return of(portfolio, stock, null);
}

public static PortfolioHolding of(Portfolio portfolio, Stock stock, Money currentPrice) {
return PortfolioHolding.builder()
.currentPrice(currentPrice)
.portfolio(portfolio)
.stock(stock)
.build();
return of(null, portfolio, stock, currentPrice);
}

public static PortfolioHolding of(Long id, Portfolio portfolio, Stock stock, Money currentPrice) {
return new PortfolioHolding(LocalDateTime.now(), null, id, portfolio, stock, currentPrice);
}

//== 연관관계 메소드 ==//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PortfolioDetailResponse createPortfolioDetailItem(Portfolio portfolio) {
portfolio.getId(), LocalDateTime.now())
.stream()
.findFirst()
.orElseGet(PortfolioGainHistory::empty);
.orElseGet(() -> PortfolioGainHistory.empty(portfolio));
return PortfolioDetailResponse.from(portfolio, history);
}

Expand All @@ -35,7 +35,7 @@ public PortfolioDetailRealTimeItem createPortfolioDetailRealTimeItem(Portfolio p
portfolio.getId(), LocalDateTime.now())
.stream()
.findFirst()
.orElseGet(PortfolioGainHistory::empty);
.orElseGet(() -> PortfolioGainHistory.empty(portfolio));
return PortfolioDetailRealTimeItem.of(portfolio, history);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codesquad.fineants.domain.member.domain.dto.request;

import codesquad.fineants.domain.member.domain.entity.Member;
import codesquad.fineants.domain.notificationpreference.domain.entity.NotificationPreference;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
Expand All @@ -23,12 +24,7 @@ public class MemberNotificationPreferenceRequest {
private Boolean targetPriceNotify;
private Long fcmTokenId;

public NotificationPreference toEntity() {
return NotificationPreference.builder()
.browserNotify(browserNotify)
.targetGainNotify(targetGainNotify)
.maxLossNotify(maxLossNotify)
.targetPriceNotify(targetPriceNotify)
.build();
public NotificationPreference toEntity(Member member) {
return NotificationPreference.create(browserNotify, targetGainNotify, maxLossNotify, targetPriceNotify, member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ public static SignUpServiceRequest of(SignUpRequest request, MultipartFile profi
}

public Member toEntity(String profileUrl, String encodedPassword) {
return Member.builder()
.email(email)
.nickname(nickname)
.profileUrl(profileUrl)
.password(encodedPassword)
.provider("local")
.build();
return Member.localMember(email, nickname, encodedPassword, profileUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -46,8 +45,7 @@ public class Member extends BaseEntity {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "member", orphanRemoval = true, cascade = CascadeType.ALL)
private Set<MemberRole> roles;

@Builder
public Member(LocalDateTime createAt, LocalDateTime modifiedAt, Long id, String email,
private Member(LocalDateTime createAt, LocalDateTime modifiedAt, Long id, String email,
String nickname, String provider, String password, String profileUrl,
NotificationPreference notificationPreference,
Set<MemberRole> roles) {
Expand All @@ -64,23 +62,21 @@ public Member(LocalDateTime createAt, LocalDateTime modifiedAt, Long id, String

public static Member oauthMember(String email, String nickname, String provider,
String profileUrl) {
return Member.builder()
.email(email)
.nickname(nickname)
.provider(provider)
.profileUrl(profileUrl)
.roles(new HashSet<>())
.build();
return new Member(LocalDateTime.now(), null, null, email, nickname, provider, null, profileUrl, null,
new HashSet<>());
}

public static Member localMember(String email, String nickname, String password) {
return Member.builder()
.email(email)
.nickname(nickname)
.provider("local")
.password(password)
.profileUrl(null)
.build();
return localMember(email, nickname, password, null);
}

public static Member localMember(String email, String nickname, String password, String profileUrl) {
return localMember(null, email, nickname, password, profileUrl);
}

public static Member localMember(Long id, String email, String nickname, String password, String profileUrl) {
return new Member(LocalDateTime.now(), null, id, email, nickname, "local", password, profileUrl, null,
new HashSet<>());
}

public void addMemberRole(MemberRole memberRole) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import codesquad.fineants.domain.notificationpreference.repository.NotificationPreferenceRepository;
import codesquad.fineants.global.errors.errorcode.MemberErrorCode;
import codesquad.fineants.global.errors.errorcode.NotificationPreferenceErrorCode;
import codesquad.fineants.global.errors.exception.FineAntsException;
import codesquad.fineants.global.errors.exception.NotFoundResourceException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -41,13 +42,12 @@ public MemberNotificationPreferenceResponse registerDefaultNotificationPreferenc
public MemberNotificationPreferenceResponse updateNotificationPreference(
Long memberId,
MemberNotificationPreferenceRequest request) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new FineAntsException(MemberErrorCode.NOT_FOUND_MEMBER));
notificationPreferenceRepository.findByMemberId(memberId)
.ifPresentOrElse(notificationPreference -> notificationPreference.changePreference(request.toEntity()),
() -> {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundResourceException(MemberErrorCode.NOT_FOUND_MEMBER));
notificationPreferenceRepository.save(NotificationPreference.defaultSetting(member));
});
.ifPresentOrElse(
notificationPreference -> notificationPreference.changePreference(request.toEntity(member)),
() -> notificationPreferenceRepository.save(NotificationPreference.defaultSetting(member)));
NotificationPreference preference = notificationPreferenceRepository.findByMemberId(memberId)
.orElseThrow(() ->
new NotFoundResourceException(NotificationPreferenceErrorCode.NOT_FOUND_NOTIFICATION_PREFERENCE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn
@Entity
Expand All @@ -48,8 +50,8 @@ public abstract class Notification extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

protected Notification(LocalDateTime createAt, LocalDateTime modifiedAt, Long id, String title,
Notification(LocalDateTime createAt, LocalDateTime modifiedAt, Long id, String title,
Boolean isRead, NotificationType type, String referenceId, String link, Member member) {
super(createAt, modifiedAt);
this.id = id;
Expand All @@ -65,14 +67,14 @@ public static Notification portfolio(String portfolioName, String title, Notific
String referenceId, String link, Member member) {
if (type == NotificationType.PORTFOLIO_TARGET_GAIN
|| type == NotificationType.PORTFOLIO_MAX_LOSS) {
return PortfolioNotification.create(portfolioName, title, type, referenceId, link, member);
return PortfolioNotification.newNotification(title, type, referenceId, link, portfolioName, member);
}
throw new IllegalArgumentException("잘못된 타입입니다. type=" + type);
}

public static Notification stock(String stockName, Money targetPrice, String title,
String referenceId, String link, Long targetPriceNotificationId, Member member) {
return StockTargetPriceNotification.create(stockName, targetPrice, title, referenceId,
return StockTargetPriceNotification.newNotification(stockName, targetPrice, title, referenceId,
link, targetPriceNotificationId, member);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Entity
Expand All @@ -16,25 +15,20 @@
public class PortfolioNotification extends Notification {
private String name;

@Builder
public PortfolioNotification(LocalDateTime createAt, LocalDateTime modifiedAt, Long id,
String title, Boolean isRead, NotificationType type, String referenceId, String link, Member member,
String name) {
super(createAt, modifiedAt, id, title, isRead, type, referenceId, link, member);
private PortfolioNotification(Long id, String title, Boolean isRead, NotificationType type, String referenceId,
String link, Member member, String name) {
super(LocalDateTime.now(), null, id, title, isRead, type, referenceId, link, member);
this.name = name;
}

public static Notification create(String portfolioName, String title, NotificationType type, String referenceId,
String link, Member member) {
return PortfolioNotification.builder()
.name(portfolioName)
.title(title)
.isRead(false)
.type(type)
.referenceId(referenceId)
.link(link)
.member(member)
.build();
public static PortfolioNotification newNotification(String title, NotificationType type, String referenceId,
String link, String portfolioName, Member member) {
return newNotification(null, title, type, referenceId, link, portfolioName, member);
}

public static PortfolioNotification newNotification(Long id, String title, NotificationType type,
String referenceId, String link, String portfolioName, Member member) {
return new PortfolioNotification(id, title, false, type, referenceId, link, member, portfolioName);
}

@Override
Expand Down
Loading

0 comments on commit ccaff01

Please sign in to comment.