-
Notifications
You must be signed in to change notification settings - Fork 297
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
2단계 - 수강신청(도메인 모델) #667
2단계 - 수강신청(도메인 모델) #667
Changes from all commits
5f42d31
47df9da
ed41c7a
2166e31
85993e7
d248796
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package nextstep.courses.domain; | ||
|
||
public class CoverImage { | ||
|
||
private final CoverImageFileSize size; | ||
private final CoverImageType type; | ||
private final CoverImageResolution resolution; | ||
|
||
CoverImage(CoverImageType type, long size, int width, int height) { | ||
this.size = new CoverImageFileSize(size); | ||
this.type = type; | ||
this.resolution = new CoverImageResolution(width, height); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package nextstep.courses.domain; | ||
|
||
public class CoverImageFactory { | ||
|
||
public static CoverImage ofGif(long size, int width, int height) { | ||
return new CoverImage(CoverImageType.GIF, size, width, height); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package nextstep.courses.domain; | ||
|
||
public class CoverImageFileSize { | ||
private static final long MAX_SIZE = 1024 * 1024; // 1MB | ||
private final long size; | ||
|
||
public CoverImageFileSize(long size) { | ||
validate(size); | ||
this.size = size; | ||
} | ||
|
||
private static void validate(long size) { | ||
if (size <= 0 || size > MAX_SIZE) { | ||
throw new IllegalArgumentException("이미지 크기는 0보다 크고 1MB 이하여야 합니다."); | ||
} | ||
} | ||
|
||
public long getSize() { | ||
return size; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package nextstep.courses.domain; | ||
|
||
public class CoverImageResolution { | ||
private static final int MIN_WIDTH = 300; | ||
private static final int MIN_HEIGHT = 200; | ||
private static final int ASPECT_RATIO_WIDTH = 3; | ||
private static final int ASPECT_RATIO_HEIGHT = 2; | ||
Comment on lines
+3
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
private final int width; | ||
private final int height; | ||
|
||
public CoverImageResolution(int width, int height) { | ||
validate(width, height); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
private static void validate(int width, int height) { | ||
validateWidthAndHeight(width, height); | ||
validateAspectRatioValid(width, height); | ||
} | ||
|
||
private static void validateWidthAndHeight(int width, int height) { | ||
boolean isValid = width >= MIN_WIDTH && height >= MIN_HEIGHT; | ||
|
||
if (!isValid) { | ||
throw new IllegalArgumentException("이미지의 width는 300픽셀, height는 200픽셀 이상이어야 합니다."); | ||
} | ||
} | ||
|
||
private static void validateAspectRatioValid(int width, int height) { | ||
boolean isValid = width * ASPECT_RATIO_HEIGHT == height * ASPECT_RATIO_WIDTH; | ||
|
||
if (!isValid) { | ||
throw new IllegalArgumentException("width와 height의 비율은 3:2여야 합니다."); | ||
} | ||
} | ||
Comment on lines
+18
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package nextstep.courses.domain; | ||
|
||
public enum CoverImageType { | ||
GIF, | ||
JPG, | ||
PNG, | ||
SVG | ||
} | ||
Comment on lines
+3
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package nextstep.courses.domain; | ||
|
||
public class FreeRegistrationPolicy implements RegistrationPolicy { | ||
|
||
@Override | ||
public void validateRegistration(Session session, Money paymentAmount) { | ||
// 무조건 패스 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package nextstep.courses.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class Money { | ||
private final NaturalNumber value; | ||
Comment on lines
+5
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
원시값포장을 전부 하셨군요 👍 |
||
|
||
public Money(long amount) { | ||
this.value = new NaturalNumber(amount); | ||
} | ||
|
||
public long getAmount() { | ||
return value.getValue(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Money money = (Money) o; | ||
return Objects.equals(value, money.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package nextstep.courses.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class NaturalNumber implements Comparable<NaturalNumber> { | ||
|
||
private final long value; | ||
|
||
public NaturalNumber(long value) { | ||
validate(value); | ||
this.value = value; | ||
} | ||
|
||
private static void validate(long value) { | ||
if (value < 0) { | ||
throw new IllegalArgumentException("0을 포함한 자연수만 허용 가능합니다."); | ||
} | ||
} | ||
|
||
public long getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
NaturalNumber that = (NaturalNumber) o; | ||
return value == that.value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getValue() + ""; | ||
} | ||
|
||
@Override | ||
public int compareTo(NaturalNumber o) { | ||
return Long.compare(getValue(), o.getValue()); | ||
} | ||
|
||
public int compareTo(int o) { | ||
return compareTo(new NaturalNumber(o)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package nextstep.courses.domain; | ||
|
||
public class PaidRegistrationPolicy implements RegistrationPolicy { | ||
|
||
private final Money sessionFee; | ||
private final NaturalNumber maxStudentCount; | ||
|
||
public PaidRegistrationPolicy(int sessionFee, int maxStudentCount) { | ||
this.sessionFee = new Money(sessionFee); | ||
this.maxStudentCount = new NaturalNumber(maxStudentCount); | ||
} | ||
|
||
@Override | ||
public void validateRegistration(Session session, Money paymentAmount) { | ||
if (!session.isStudentCountLessThan((int) maxStudentCount.getValue())) { | ||
throw new IllegalArgumentException("강의 최대 수강 인원을 초과할 수 없습니다."); | ||
} | ||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
동의합니다 👍 |
||
|
||
if (!sessionFee.equals(paymentAmount)) { | ||
throw new IllegalArgumentException("수강생이 결제한 금액과 수강료가 일치할 때 수강 신청이 가능합니다."); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package nextstep.courses.domain; | ||
|
||
public interface RegistrationPolicy { | ||
void validateRegistration(Session session, Money paymentAmount); | ||
} | ||
Comment on lines
+3
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 유/무료 정책을 이렇게 구현해주셨군요 👍 👏 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package nextstep.courses.domain; | ||
|
||
import nextstep.payments.domain.Payment; | ||
import nextstep.users.domain.NsUser; | ||
import nextstep.users.domain.NsUsers; | ||
|
||
public class Session { | ||
private Long id; | ||
private Course course; | ||
private final NsUsers nsUsers = new NsUsers(); | ||
private CoverImage coverImage; | ||
private SessionStatus sessionStatus; | ||
private RegistrationPolicy registrationPolicy; | ||
private SessionPeriod sessionPeriod; | ||
|
||
Session(long id, CoverImage coverImage, SessionStatus sessionStatus, RegistrationPolicy registrationPolicy, SessionPeriod sessionPeriod) { | ||
this.id = id; | ||
this.coverImage = coverImage; | ||
this.sessionStatus = sessionStatus; | ||
this.registrationPolicy = registrationPolicy; | ||
this.sessionPeriod = sessionPeriod; | ||
} | ||
|
||
public void toCourse(Course course) { | ||
this.course = course; | ||
} | ||
|
||
public boolean isStudentCountLessThan(int count) { | ||
return nsUsers.getSize() < count; | ||
} | ||
|
||
public Payment register(NsUser nsUser, Money paymentAmount) { | ||
if (!sessionStatus.isRegistrable()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
throw new IllegalStateException("수강신청이 불가능한 상태입니다."); | ||
} | ||
|
||
registrationPolicy.validateRegistration(this, paymentAmount); | ||
|
||
nsUsers.add(nsUser); | ||
|
||
return new Payment("", this, nsUser, paymentAmount); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package nextstep.courses.domain; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class SessionPeriod { | ||
|
||
private final LocalDateTime startedAt; | ||
private final LocalDateTime endedAt; | ||
|
||
public SessionPeriod(LocalDateTime startedAt, LocalDateTime endedAt) { | ||
this.startedAt = startedAt; | ||
this.endedAt = endedAt; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package nextstep.courses.domain; | ||
|
||
public enum SessionStatus { | ||
PREPARING, | ||
RECRUITING, | ||
ENDED; | ||
|
||
public boolean isRegistrable() { | ||
return this == RECRUITING; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package nextstep.courses.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Sessions { | ||
private final List<Session> sessions; | ||
|
||
public Sessions(List<Session> sessions) { | ||
this.sessions = sessions; | ||
} | ||
|
||
public Sessions() { | ||
this(new ArrayList<>()); | ||
} | ||
|
||
public void add(Session session) { | ||
sessions.add(session); | ||
} | ||
|
||
public List<Session> getSessions() { | ||
return sessions; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,7 @@ public DeleteHistory delete(NsUser loginUser) { | |
validateDelete(loginUser); | ||
this.deleted = true; | ||
this.updatedDate = LocalDateTime.now(); | ||
return new DeleteHistory(ContentType.ANSWER, getId(), getWriter(), LocalDateTime.now()); | ||
return DeleteHistoryFactory.ofAnswer(getId(), loginUser, LocalDateTime.now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍