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

public class Capacity {

private final int max;
private int current;

public Capacity(int max, int current) {
this.max = max;
this.current = current;
}

public void validateAvailable() {
if (current > max) {
throw new IllegalStateException();
}
}
}
14 changes: 9 additions & 5 deletions src/main/java/nextstep/courses/domain/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@ public class Course {

private Long creatorId;

private final Sessions sessions;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

public Course() {
}

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

public Course(Long id, String title, Long creatorId, LocalDateTime createdAt, LocalDateTime updatedAt) {
public Course(Long id, String title, Long creatorId, Sessions sessions, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.title = title;
this.creatorId = creatorId;
this.sessions = sessions;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public void addSession(Session session) {
sessions.add(session);
}

public String getTitle() {
return title;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/nextstep/courses/domain/CoverImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package nextstep.courses.domain;

public class CoverImage {
private ImageSize imageSize;
private ImageType imageType;
private ImageDimension imageDimension;

public CoverImage(int size, String fileName, int width, int height) {
this(new ImageSize(size), ImageType.fromFileName(fileName), new ImageDimension(width, height));
}

private CoverImage(ImageSize imageSize, ImageType imageType, ImageDimension imageDimension) {
this.imageSize = imageSize;
this.imageType = imageType;
this.imageDimension = imageDimension;
}
}
15 changes: 15 additions & 0 deletions src/main/java/nextstep/courses/domain/Enrollment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nextstep.courses.domain;

import java.time.LocalDateTime;

public class Enrollment {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

private final Long sessionId;
private final Long userId;
private final LocalDateTime enrollmentDate;

public Enrollment(Long sessionId, Long userId) {
this.sessionId = sessionId;
this.userId = userId;
this.enrollmentDate = LocalDateTime.now();
}
}
7 changes: 7 additions & 0 deletions src/main/java/nextstep/courses/domain/EnrollmentPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nextstep.courses.domain;

import nextstep.payments.domain.Payment;

public interface EnrollmentPolicy {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

void validateEnrollment(Payment payment);
}
10 changes: 10 additions & 0 deletions src/main/java/nextstep/courses/domain/FreeEnrollmentPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package nextstep.courses.domain;

import nextstep.payments.domain.Payment;

public class FreeEnrollmentPolicy implements EnrollmentPolicy{
@Override
public void validateEnrollment(Payment payment) {
// 무료강의는 검증하지 않는다
}
}
30 changes: 30 additions & 0 deletions src/main/java/nextstep/courses/domain/ImageDimension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nextstep.courses.domain;

public class ImageDimension {

public static final int HEIGHT_RATIO = 2;
public static final int WIDTH_RATIO = 3;
public static final int MIN_WIDTH = 300;
public static final int MIN_HEIGHT = 200;
private int width;
private int height;

public ImageDimension(int width, int height) {
validateMinimumSize(width, height);
validateRatio(width, height);
this.width = width;
this.height = height;
}

private static void validateMinimumSize(int width, int height) {
if (width < MIN_WIDTH || height < MIN_HEIGHT) {
throw new IllegalArgumentException();
}
}

private static void validateRatio(int width, int height) {
if (!(HEIGHT_RATIO * width == WIDTH_RATIO * height)) {
throw new IllegalArgumentException();
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/nextstep/courses/domain/ImageSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nextstep.courses.domain;

import java.util.Objects;

public class ImageSize {

public static final int MAX_SIZE = 1_048_576;
private int imageSize;

public ImageSize(int imageSize) {
if (imageSize > MAX_SIZE) {
throw new IllegalArgumentException();
}
this.imageSize = imageSize;
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
ImageSize imageSize1 = (ImageSize) object;
return imageSize == imageSize1.imageSize;
}

@Override
public int hashCode() {
return Objects.hashCode(imageSize);
}
}
43 changes: 43 additions & 0 deletions src/main/java/nextstep/courses/domain/ImageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package nextstep.courses.domain;

import java.util.Arrays;

public enum ImageType {

GIF("gif"),
JPG("jpg", "jpeg"),
PNG("png"),
SVG("svg");

private final String[] extensions;

ImageType(String... extensions) {
this.extensions = extensions;
}

private boolean matches(String extension) {
return Arrays.stream(extensions)
.anyMatch(ext -> ext.equalsIgnoreCase(extension));
}

private static ImageType from(String extension) {
return Arrays.stream(values())
.filter(type -> type.matches(extension))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException());
}

public static ImageType fromFileName(String fileName) {
String extension = extractExtension(fileName);
return from(extension);
}

private static String extractExtension(String fileName) {
int index = fileName.lastIndexOf(".");
if (index == -1 || index == fileName.length() - 1) {
throw new IllegalArgumentException();
}
return fileName.substring(index + 1);
}

}
28 changes: 28 additions & 0 deletions src/main/java/nextstep/courses/domain/Money.java
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 long amount;

public Money(long amount) {
this.amount = amount;
}

public boolean isEqualTo(Money other) {
return this.equals(other);
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
Money money = (Money) object;
return amount == money.amount;
}

@Override
public int hashCode() {
return Objects.hashCode(amount);
}
}
26 changes: 26 additions & 0 deletions src/main/java/nextstep/courses/domain/PaidEnrollmentPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nextstep.courses.domain;

import nextstep.payments.domain.Payment;

public class PaidEnrollmentPolicy implements EnrollmentPolicy {

private final Money money;
private final Capacity capacity;

public PaidEnrollmentPolicy(Money money, Capacity capacity) {
this.money = money;
this.capacity = capacity;
}

@Override
public void validateEnrollment(Payment payment) {
capacity.validateAvailable();
validatePayment(payment);
}

private void validatePayment(Payment payment) {
if (!payment.isSameAmount(money)) {
throw new IllegalArgumentException();
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/nextstep/courses/domain/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nextstep.courses.domain;

import nextstep.payments.domain.Payment;

import java.time.LocalDateTime;

public class Session {
private final long id;
private final SessionDuration sessionDuration;
private final CoverImage coverImage;
private final EnrollmentPolicy enrollmentPolicy;
private final SessionState sessionState;

public Session(long id
, LocalDateTime startDate
, LocalDateTime endDate
, int size
, String fileName
, int width
, int height
, EnrollmentPolicy enrollmentPolicy
, SessionState sessionState) {
this(id, new SessionDuration(startDate, endDate), new CoverImage(size, fileName, width, height)
, enrollmentPolicy, sessionState);
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

주 생성자 호출하는 방식으로 구현 👍

}

public Session(long id, SessionDuration sessionDuration, CoverImage coverImage
, EnrollmentPolicy enrollmentPolicy, SessionState sessionState) {
this.id = id;
this.sessionDuration = sessionDuration;
this.coverImage = coverImage;
this.enrollmentPolicy = enrollmentPolicy;
this.sessionState = sessionState;
}

public Enrollment enroll(Long userId, Payment payment) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍
단, 수강 신청이 가능한 경우 수강생 목록에 추가해야하지 않을까?
즉, Session이 수강생 목록을 가지고 있어야 하지 않을까?
수강생 목록에 수강생을 추가할 때 이미 수강 신청한 수강생인지, 수강생 최대 인원을 초과하는 등의 판단을 해야하지 않을까?

sessionState.validateEnroll();
enrollmentPolicy.validateEnrollment(payment);
return new Enrollment(this.id, userId);
}
}
20 changes: 20 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionDuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nextstep.courses.domain;

import java.time.LocalDateTime;

public class SessionDuration {
private LocalDateTime startDate;
private LocalDateTime endDate;

public SessionDuration(LocalDateTime startDate, LocalDateTime endDate) {
validateDate(startDate, endDate);
this.startDate = startDate;
this.endDate = endDate;
}

private void validateDate(LocalDateTime startDate, LocalDateTime endDate) {
if (!startDate.isBefore(endDate)) {
throw new IllegalArgumentException();
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nextstep.courses.domain;

public enum SessionState {
READY,
OPEN,
CLOSED;

public void validateEnroll() {
if (this != OPEN) {
throw new IllegalStateException();
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/nextstep/courses/domain/Sessions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nextstep.courses.domain;

import java.util.ArrayList;
import java.util.List;

public class Sessions {
private final List<Session> sessions;

public Sessions() {
this(new ArrayList<>());
}

public Sessions(List<Session> sessions) {
this.sessions = new ArrayList<>(sessions);
}

public void add(Session session) {
this.sessions.add(session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Course findById(Long id) {
rs.getLong(1),
rs.getString(2),
rs.getLong(3),
null,
toLocalDateTime(rs.getTimestamp(4)),
toLocalDateTime(rs.getTimestamp(5)));
return jdbcTemplate.queryForObject(sql, rowMapper, id);
Expand Down
Loading