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

feat: add portfolio, portfolio stock entity #89

Merged
merged 11 commits into from
Apr 1, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package nexters.payout.domain.portfolio.domain;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import lombok.Getter;
import nexters.payout.domain.BaseEntity;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Entity
@Getter
public class Portfolio extends BaseEntity {

@ElementCollection
@CollectionTable(name = "portfolio_stock", joinColumns = @JoinColumn(name = "portfolio_id"))
private List<PortfolioStock> stocks = new ArrayList<>();
private Instant expireAt;

public Portfolio() {
super(null);
}

public Portfolio(final UUID id, final Instant expireAt) {
super(id);
this.expireAt = expireAt;
}

public Portfolio(final Instant expireAt, List<PortfolioStock> stocks) {
super(null);
this.stocks = stocks;
this.expireAt = expireAt;
}

public boolean isExpired() {
return expireAt.isAfter(Instant.now());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nexters.payout.domain.portfolio.domain;

import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.UUID;

@Embeddable
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PortfolioStock {

private UUID stockId;
private Integer shares;


public PortfolioStock(final UUID id, final UUID stockId, final Integer shares) {
this.stockId = stockId;
this.shares = shares;
}

private PortfolioStock(final UUID stockId, final Integer shares) {
this.stockId = stockId;
this.shares = shares;
}

public PortfolioStock create(final UUID portfolioId, final UUID stockId, final Integer shares) {
Copy link
Member

Choose a reason for hiding this comment

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

생성 메서드 따로 만들어주신 이유가 있을까요?? 생성자와 동일한 매개변수를 받는 것으로 보여서 생성자를 사용해도 될 것 같아서요!!

Copy link
Member Author

Choose a reason for hiding this comment

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

개인적으로 객체를 생성할 때 정적 팩토리 메서드를 이용하는 편인데, 다른 클래스와의 일관성을 유지하기 위해 생성했어요!! 그냥 생성자를 사용하는게 더 나을까요?! 🤔

Copy link
Member

Choose a reason for hiding this comment

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

저는 정적 팩터리 메서드는 생성을 위한 로직이 복잡한 경우에 주로 사용하고 아니라면 기본 생성자를 이용하는 편이긴 합니다!!
매개변수 개수가 많지 않고 생성자를 쓰는 것과 동일한 상황같아서 기본 생성자를 쓰는 것도 좋아보여요 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

오 팩토리 메서드를 경우에 따라 나눠쓰시는군요!! 그 방식이 오히려 더 직관적으로 보일 수도 있을것 같아요! 해당 방향으로 수정하겠습니다!!

return new PortfolioStock(portfolioId, stockId, shares);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package nexters.payout.domain.portfolio.domain.repository;

import nexters.payout.domain.portfolio.domain.Portfolio;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface PortfolioRepository extends JpaRepository<Portfolio, UUID> {

}
16 changes: 16 additions & 0 deletions domain/src/main/resources/db/migration/V5__add_portfolio.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
create table portfolio
(
id binary (16) not null
primary key,
expire_at datetime(6),
created_at datetime(6),
last_modified_at datetime(6)
) engine = innodb
default charset = utf8mb4;

create table portfolio_stock (
portfolio_id binary(16) not null,
stock_id binary(16),
shares integer
) engine=InnoDB
default charset = utf8mb4;
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class StockDividendAnalysisServiceTest {

Dividend pastDividend = DividendFixture.createDividendWithExDividendDate(
UUID.randomUUID(),
LocalDate.of(now.getYear(), now.getMonth().minus(1), now.getDayOfMonth())
LocalDate.of(now.getYear(), now.getMonth().minus(1), 1)
Copy link
Member

Choose a reason for hiding this comment

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

ㅋ ㅋ ㅋ 🤯🤯

.atStartOfDay(ZoneId.systemDefault()).toInstant()
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package nexters.payout.domain;

import nexters.payout.domain.portfolio.domain.Portfolio;
import nexters.payout.domain.portfolio.domain.PortfolioStock;

import java.time.Instant;
import java.util.UUID;

public class PortfolioFixture {

public static Portfolio createPortfolio(Instant expireAt) {
return new Portfolio(UUID.randomUUID(), expireAt);
}

public static PortfolioStock createPortfolioStock(UUID portfolioId, UUID stockId, Integer shares) {
return new PortfolioStock(UUID.randomUUID(), stockId, shares);
}
}
Loading