Skip to content
Merged
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
30 changes: 27 additions & 3 deletions src/main/java/nextstep/courses/domain/image/SessionCoverImage.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
package nextstep.courses.domain.image;

public class SessionCoverImage {
private final Long id;
private final Long sessionId;
private final SessionImageDimension dimension;
private final SessionImageExtension extension;
private final SessionImageCapacity capacity;

public SessionCoverImage(int width, int height, String extension, long bytes) {
this(new SessionImageDimension(width, height), SessionImageExtension.from(extension), new SessionImageCapacity(bytes));
public SessionCoverImage(Long sessionId, int width, int height, String extension, long bytes) {
this(null, sessionId, new SessionImageDimension(width, height), SessionImageExtension.from(extension), new SessionImageCapacity(bytes));
}

public SessionCoverImage(SessionImageDimension dimension, SessionImageExtension extension, SessionImageCapacity capacity) {
public SessionCoverImage(Long id, Long sessionId, SessionImageDimension dimension, SessionImageExtension extension, SessionImageCapacity capacity) {
this.id = id;
this.sessionId = sessionId;
this.dimension = dimension;
this.extension = extension;
this.capacity = capacity;
}

public Long getId() {
return id;
}

public Long getSessionId() {
return sessionId;
}

public SessionImageDimension getDimension() {
return dimension;
}

public SessionImageExtension getExtension() {
return extension;
}

public SessionImageCapacity getCapacity() {
return capacity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ private void validate(long bytes) {
}
}

public long bytes() {
return bytes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public SessionImageDimension(int width, int height) {
this.width = width;
this.height = height;
}

private void validateMinLength(int width, int height){
if(!(width >= MIN_WIDTH && height >= MIN_HEIGHT)) {
throw new IllegalArgumentException("์ด๋ฏธ์ง€๋Š” ๊ฐ€๋กœ 300์ด์ƒ, ์„ธ๋กœ 200 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
Expand All @@ -34,5 +35,11 @@ private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

public int width() {
return width;
}

public int height() {
return height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package nextstep.courses.domain.image;

public interface SessionImageRepository {
int save(SessionCoverImage image);

SessionCoverImage findById(Long id);

SessionCoverImage findBySessionId(Long sessionId);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package nextstep.courses.domain.registration;

import java.time.LocalDateTime;
import java.util.Objects;

public class Registration {
private final Long id;
private final Long sessionId;
private final Long studentId;
private final LocalDateTime enrolledAt;

public Registration(Long sessionId, Long studentId) {
this(sessionId, studentId, LocalDateTime.now());
this(null, sessionId, studentId, LocalDateTime.now());
}

public Registration(Long sessionId, Long studentId, LocalDateTime enrolledAt) {
public Registration(Long id, Long sessionId, Long studentId, LocalDateTime enrolledAt) {
this.id = id;
this.sessionId = sessionId;
this.studentId = studentId;
this.enrolledAt = enrolledAt;
}

public boolean contains(Long studentId) {
return this.studentId == studentId;
return Objects.equals(this.studentId, studentId);
}

public Long getId() {
return id;
}

public Long getSessionId() {
return sessionId;
}

public Long getStudentId() {
return studentId;
}

public LocalDateTime getEnrolledAt() {
return enrolledAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nextstep.courses.domain.registration;

import java.util.List;

public interface RegistrationRepository {
int save(Registration registration);

Registration findById(Long id);

List<Registration> findBySessionId(Long sessionId);

int countBySessionId(Long sessionId);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
package nextstep.courses.domain.registration;

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

public class Registrations {
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 List<Registration> registrations;

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

public Registrations(List<Registration> registrations) {
this.registrations = registrations;
}

public Registrations add(Registration registration) {
List<Registration> newList = new ArrayList<>(registrations);
newList.add(registration);
return new Registrations(newList);
}

public int count() {
return registrations.size();
}

public boolean contains(Long studentId) {
return registrations.stream()
.anyMatch(r -> r.contains(studentId));
}

private final List<Registration> registrations;

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

public Registrations(List<Registration> registrations) {
this.registrations = new ArrayList<>(registrations);
}

public Registrations add(Registration registration) {
validateDuplicateRegistration(registration);
List<Registration> newList = new ArrayList<>(registrations);
newList.add(registration);
return new Registrations(newList);
}

private void validateDuplicateRegistration(Registration registration) {
if (isAlreadyRegistered(registration.getStudentId())) {
throw new IllegalArgumentException("์ด๋ฏธ ์ˆ˜๊ฐ•์‹ ์ฒญํ•œ ํ•™์ƒ์ž…๋‹ˆ๋‹ค.");
}
}

public boolean isAlreadyRegistered(long studentId) {
return registrations.stream()
.anyMatch(registration -> registration.getStudentId().equals(studentId));
}

public void validateCapacity(int maxCapacity) {
if (count() > maxCapacity) {
throw new IllegalStateException("์ตœ๋Œ€ ์ˆ˜๊ฐ• ์ธ์›์„ ์ดˆ๊ณผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
}

public int count() {
return registrations.size();
}
}
44 changes: 19 additions & 25 deletions src/main/java/nextstep/courses/domain/session/Enrollment.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
package nextstep.courses.domain.session;

import nextstep.courses.domain.session.type.FreeType;
import nextstep.courses.domain.session.type.SessionType;
import nextstep.courses.domain.registration.Registration;
import nextstep.courses.domain.registration.Registrations;

public class Enrollment {
private SessionState state;
private SessionType type;

public Enrollment() {
this(SessionState.PREPARING, new FreeType());
}

public Enrollment(SessionType type) {
this(SessionState.PREPARING, type);
}

public Enrollment(SessionState state, SessionType type) {
private final Session session;
Copy link
Contributor

Choose a reason for hiding this comment

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

Session์œผ๋กœ ์˜์กด ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋‹ค๋ณด๋‹ˆ Enrollment ์ƒ์„ฑ์—๋„ ์–ด๋ ค์›€์ด ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.
์˜์กด ๊ด€๊ณ„๋ฅผ ๋Š๊ณ , 'long sessionId'๋งŒ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ?
๊ฐ์ฒด์˜ ์˜์กด ๊ด€๊ณ„๋ฅผ ๋ฌด์กฐ๊ฑด์œผ๋กœ ๊ฐ€์ ธ๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹์€ ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒˆ ๊ธฐํšŒ์— ์–ด๋А ์‹œ์ ์— ์˜์กด ๊ด€๊ณ„๋ฅผ ๋Š๋Š” ๊ฒƒ์ด ์ข‹์„์ง€ ๊ณ ๋ฏผํ•ด ๋ณด๋Š” ์—ฐ์Šต๋„ ํ•ด๋ณด๋ฉด ์ข‹๊ฒ ๋‹ค.

private final SessionState state;
private final SessionPolicy policy;
private final Registrations registrations;

public Enrollment(Session session, SessionState state, SessionPolicy policy, Registrations registrations) {
validateState(state);
this.session = session;
this.state = state;
this.type = type;
this.policy = policy;
this.registrations = registrations;
}

public void enroll(long payAmount) {
validateState();
this.type = type.enroll(payAmount);
}
public Registration enroll(long payAmount, long userId) {
if (registrations.isAlreadyRegistered(userId)) {
throw new IllegalArgumentException("์ด๋ฏธ ์ˆ˜๊ฐ•์‹ ์ฒญํ•œ ํ•™์ƒ์ž…๋‹ˆ๋‹ค.");
}

public void open() {
this.state = state.open();
}
policy.validate(payAmount, registrations);

public void close() {
this.state = state.close();
return new Registration(session.getId(), userId);
}

private void validateState() {
private void validateState(SessionState state) {
if (!state.canEnroll()) {
throw new IllegalStateException("๋ชจ์ง‘์ค‘์ธ ๊ฐ•์˜๋งŒ ์ˆ˜๊ฐ•์‹ ์ฒญ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.");
}
Expand Down
98 changes: 67 additions & 31 deletions src/main/java/nextstep/courses/domain/session/Session.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,74 @@
package nextstep.courses.domain.session;

import java.util.List;
import nextstep.courses.domain.BaseEntity;
import nextstep.courses.domain.course.Course;
import nextstep.courses.domain.image.SessionCoverImage;
import nextstep.courses.domain.registration.Registration;
import nextstep.courses.domain.registration.Registrations;

public class Session extends BaseEntity {
private final Course course;
private final Term term;
private final SessionCoverImage cover;
private final SessionPeriod period;
private final Enrollment enrollment;

public Session(Course course, int term, SessionCoverImage cover, String startDay, String endDay) {
this(null, course, new Term(term), cover, new SessionPeriod(startDay, endDay), new Enrollment());
}

public Session(Long id, Course course, Term term, SessionCoverImage cover, SessionPeriod period, Enrollment enrollment) {
super(id);
this.course = course;
this.term = term;
this.cover = cover;
this.period = period;
this.enrollment = enrollment;
}

public void enroll(long payAmount) {
enrollment.enroll(payAmount);
}

public void open() {
enrollment.open();
}

public void close() {
enrollment.close();
}
private final Long courseId;
private final Term term;
private final SessionPeriod period;
private final SessionCoverImage coverImage;
private final SessionPolicy sessionPolicy;
private SessionState state;

public Session(Long courseId, int term, String startDay, String endDay, SessionCoverImage coverImage) {
this(null, courseId, new Term(term), new SessionPeriod(startDay, endDay), SessionState.PREPARING, new SessionPolicy(), coverImage);
}

public Session(Long courseId, int term, String startDay, String endDay, SessionPolicy sessionPolicy, SessionCoverImage coverImage) {
this(null, courseId, new Term(term), new SessionPeriod(startDay, endDay), SessionState.PREPARING, sessionPolicy, coverImage);
}

public Session(Long id, Long courseId, Term term, SessionPeriod period, SessionState state, SessionPolicy sessionPolicy, SessionCoverImage coverImage) {
super(id);
this.courseId = courseId;
this.term = term;
this.period = period;
this.state = state;
this.sessionPolicy = sessionPolicy;
this.coverImage = coverImage;
}

public Enrollment enrollment(Registrations registrations) {
return new Enrollment(this, state, sessionPolicy, registrations);
}

public Enrollment enrollment(List<Registration> registrations) {
return new Enrollment(this, state, sessionPolicy, new Registrations(registrations));
}

public void open() {
this.state = state.open();
}

public void close() {
this.state = state.close();
}

public Long getCourseId() {
return courseId;
}

public Term getTerm() {
return term;
}

public SessionPeriod getPeriod() {
return period;
}

public SessionState getState() {
return state;
}

public SessionPolicy getSessionPolicy() {
return sessionPolicy;
}

public SessionCoverImage getCoverImage() {
return coverImage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ private void validate(LocalDate startDay, LocalDate endDay) {
throw new IllegalArgumentException("์‹œ์ž‘์ผ์€ ์ข…๋ฃŒ์ผ๋ณด๋‹ค ์ด์ „์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
}
}

public LocalDate startDay() {
return startDay;
}

public LocalDate endDay() {
return endDay;
}
}
Loading