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

step3 #386

Open
wants to merge 15 commits into
base: hyesooo
Choose a base branch
from
Open

step3 #386

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
22 changes: 22 additions & 0 deletions src/main/java/nextstep/courses/domain/Capacity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nextstep.courses.domain;

import nextstep.courses.exception.BusinessInvalidValueException;

public class Capacity {
Hyesooo marked this conversation as resolved.
Show resolved Hide resolved
private final int capacity;

public Capacity(int capacity) {
Copy link
Contributor

Choose a reason for hiding this comment

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

수강 인원이 1명 이상일 때 의미가 있기 때문에 1이상의 값인지에 대한 검증 로직도 필요하지 않을까?

this.capacity = capacity;
}

public void validateCapacity(int count) {
if (count >= capacity) {
throw new BusinessInvalidValueException("최대수강인원을 초과했습니다.");
}
}

public int capacity() {
return capacity;
}
}

20 changes: 15 additions & 5 deletions src/main/java/nextstep/courses/domain/Course.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package nextstep.courses.domain;

import nextstep.courses.utils.BaseEntity;

import java.time.LocalDateTime;

public class Course {
public class Course extends BaseEntity {
private Long id;

private String title;

private Long creatorId;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

public Course() {
}

public Course(Long id) {
this.id = id;
}

public Course(String title, Long creatorId) {
this(0L, title, creatorId, LocalDateTime.now(), null);
}

public Course(Long id, String title, Long creatorId) {
this(id, title, creatorId, LocalDateTime.now(), null);
}

public Course(Long id, String title, Long creatorId, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.title = title;
Expand All @@ -28,6 +34,10 @@ public Course(Long id, String title, Long creatorId, LocalDateTime createdAt, Lo
this.updatedAt = updatedAt;
}

public Long id() {
return id;
}

public String getTitle() {
return title;
}
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/nextstep/courses/domain/FreeSession.java

This file was deleted.

45 changes: 0 additions & 45 deletions src/main/java/nextstep/courses/domain/PaidSession.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/java/nextstep/courses/domain/Period.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ public Period(LocalDateTime beginDt, LocalDateTime endDt) {
this.beginDt = beginDt;
this.endDt = endDt;
}

public LocalDateTime getBeginDt() {
return beginDt;
}

public LocalDateTime getEndDt() {
return endDt;
}
}
21 changes: 21 additions & 0 deletions src/main/java/nextstep/courses/domain/Price.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nextstep.courses.domain;

import nextstep.courses.exception.BusinessInvalidValueException;

public class Price {
private final long price;

public Price(long price) {
Copy link
Contributor

Choose a reason for hiding this comment

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

강의료도 1000원 이상과 같이 본인만의 정책을 만들고 유효성 처리하도록 구현하는 것은 어떨까?

this.price = price;
}

public void validatePrice(long price) {
if (this.price != price) {
throw new BusinessInvalidValueException("강의 가격이 변동되었습니다.");
}
}

public long price() {
return price;
}
}
43 changes: 40 additions & 3 deletions src/main/java/nextstep/courses/domain/Registration.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
package nextstep.courses.domain;

import nextstep.courses.utils.BaseEntity;
import nextstep.users.domain.NsUser;

public class Registration {
import java.time.LocalDateTime;

public Registration() {
public class Registration extends BaseEntity {
private Long id = null;
private final NsUser user;
private final Session session;
private final Long amount;

public Registration(Long id, NsUser user, Session session, Long amount, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.user = user;
this.session = session;
this.amount = amount;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public Registration(NsUser user, Session session, Long amount) {
Copy link
Contributor

Choose a reason for hiding this comment

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

부생성자가 주생성자 앞에 오는 것이 관례임

this.user = user;
this.session = session;
this.amount = amount;
Copy link
Contributor

Choose a reason for hiding this comment

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

부생성자는 주생성자를 호출해 필드의 값을 초기화하는 것을 추천

}

public void register(NsUser user, Session session, Long amount) {
public static Registration register(NsUser user, Session session, Long amount) {
Registration registration = new Registration(user, session, amount);
session.enroll(user, amount);
return registration;
}

public Long id() {
return id;
}

public NsUser nsUser() {
return user;
}

public Session session() {
return session;
}

public Long amount() {
return amount;
}
}
11 changes: 11 additions & 0 deletions src/main/java/nextstep/courses/domain/Semester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nextstep.courses.domain;

import nextstep.courses.utils.BaseEntity;

public class Semester extends BaseEntity {
Copy link
Contributor

Choose a reason for hiding this comment

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

이 객체는 Course를 가지는 것 이외에 아무 역할도 담당하고 있지 않다.
이 객체가 필요한 이유는?

private final Course course;

public Semester(Course course) {
this.course = course;
}
}
99 changes: 88 additions & 11 deletions src/main/java/nextstep/courses/domain/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,117 @@

import nextstep.courses.enums.SessionStatus;
import nextstep.courses.exception.BusinessInvalidValueException;
import nextstep.courses.utils.BaseEntity;
import nextstep.users.domain.NsUser;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public abstract class Session {
final Long id;
final Course course;
final SessionCover sessionCover;
final Period period;
SessionStatus status = SessionStatus.PREPARE;
final List<NsUser> participants = new ArrayList<>();
public class Session extends BaseEntity {
public static long FREE_PRICE = 0L;
public static int MAX_CAPACITY = Integer.MAX_VALUE;
private final Long id;
private Course course;
private SessionCover sessionCover;
private Period period;
private Capacity capacity;
private Price price;

public Session(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course) {
private SessionStatus status = SessionStatus.PREPARE;
private List<NsUser> participants;

public Session(Long id) {
this.id = id;
}

private Session(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course, Capacity capacity, Price price, List<NsUser> participants) {
this.id = id;
this.period = new Period(beginDt, endDt);
this.sessionCover = sessionCover;
this.course = course;
this.capacity = capacity;
this.price = price;
this.participants = participants;
}

public Session(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionStatus status, Capacity capacity, Price price, Course course, SessionCover sessionCover, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.period = new Period(beginDt, endDt);
this.status = status;
this.sessionCover = sessionCover;
this.course = course;
this.capacity = capacity;
this.price = price;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public abstract void enroll(NsUser participant, Long amount);
public static Session fromSessionForFree(Session session, SessionCover sessionCover, Course course, List<NsUser> participants) {
return Session.ofFree(session.id, session.period.getBeginDt(), session.period.getEndDt(), sessionCover, course, participants);
}

public static Session fromSessionForPaid(Session session, SessionCover sessionCover, Course course, List<NsUser> participants) {
return Session.ofPaid(session.id, session.period.getBeginDt(), session.period.getEndDt(), sessionCover, course, session.price.price(), session.capacity.capacity(), participants);
}

public static Session ofFree(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course, List<NsUser> participants) {
return new Session(id, beginDt, endDt, sessionCover, course, new Capacity(MAX_CAPACITY), new Price(FREE_PRICE), participants);
}

public static Session ofPaid(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course, Long price, Integer capacity, List<NsUser> participants) {
return new Session(id, beginDt, endDt, sessionCover, course, new Capacity(capacity), new Price(price), participants);
}

public static Session fromSession(Session session, SessionCover sessionCover, Course course, List<NsUser> participants) {
if (session.price.equals(0L)) {
return fromSessionForFree(session, sessionCover, course, participants);
}
return fromSessionForPaid(session, sessionCover, course, participants);
}

public void startEnrollment() {
this.status = SessionStatus.ENROLL;
}

public void validateStatus() {
public void enroll(NsUser participant, Long amount) {
Hyesooo marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

이 메서드의 로직을 Enrollment와 같은 도메인 객체를 추가해 로직을 분리해 보는 것은 어떨까?
Enrollment는 수강신청 로직 구현을 위해 필요한 capacity와 수강생 목록(participants)을 인스턴스 변수로 가지면 어떨까?

capacity.validateCapacity(participants.size());
price.validatePrice(amount);
validateStatus();
this.participants.add(participant);
}


private void validateStatus() {
if (SessionStatus.ENROLL != this.status) {
throw new BusinessInvalidValueException("수강신청 가능한 상태가 아닙니다.");
}
}

public Price price() {
return price;
}

public Capacity capacity() {
return capacity;
}

public Long id() {
return id;
}

public Period period() {
return period;
}

public Course course() {
return course;
}

public SessionCover sessionCover() {
return sessionCover;
}

public String status() {
return this.status.name();
}
}
Loading