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 #355

Open
wants to merge 14 commits into
base: yumble
Choose a base branch
from
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@
* [X] 강의 수강신청은 강의 상태가 모집중일 때만 가능하다.
* [X] 유료 강의의 경우 결제는 이미 완료한 것으로 가정하고 이후 과정을 구현한다.
* [X] 결제를 완료한 결제 정보는 payments 모듈을 통해 관리되며, 결제 정보는 Payment 객체에 담겨 반한된다.

# STEP 3 기능분석
* [X] schema sql 작성
* [X] Session Schema sql 작성
* [X] SessionType Schema sql 작성
* [X] Thumbnail Schema sql 작성
* [X] Students Schema sql 작성
* [X] data sql 작성
* [X] Session 예시 데이터 data.sql 작성
* [X] SessionType Schema sql 작성
* [X] Thumbnail 예시 데이터 data.sql 작성
* [X] Students 예시 데이터 data.sql 작성
* [X] repository query 작성
* [X] Session Read 쿼리 작성
* [X] Thumbnail Read 쿼리 작성
* [X] Students Read, Save 쿼리 작성
* [X] service enroll 코드 작성
8 changes: 5 additions & 3 deletions src/main/java/nextstep/courses/domain/FreeSession.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package nextstep.courses.domain;

public class FreeSession extends SessionType{
public class FreeSession implements SessionType{

public FreeSession(boolean isPaid) {
super(isPaid);
private final Long sessionTypeId;

public FreeSession(Long sessionTypeId) {
this.sessionTypeId = sessionTypeId;
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/nextstep/courses/domain/PaidSession.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package nextstep.courses.domain;

public class PaidSession extends SessionType{
public class PaidSession implements SessionType{

Choose a reason for hiding this comment

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

아 세션타입을 구현한게 세션이었군요 ?

Choose a reason for hiding this comment

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

DB 맵핑을 더 직관적으로 표현한다면, SessionType을 enum으로 만들고 DB에 업데이트 하는 정보를 enum을 넣으면 어떨까 싶네요! @Enumerated 를 String 타입으로 하는 것 처럼요!


private final Long sessionTypeId;
private final Integer maxStudents;
private final Integer sessionFee;

public PaidSession(boolean isPaid, Integer maxStudents, Integer sessionFee) {
super(isPaid);
public PaidSession(Long sessionTypeId, Integer maxStudents, Integer sessionFee) {
validatePaidValue(maxStudents, sessionFee);
this.sessionTypeId = sessionTypeId;
this.maxStudents = maxStudents;
this.sessionFee = sessionFee;
}
Expand Down
37 changes: 18 additions & 19 deletions src/main/java/nextstep/courses/domain/Session.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package nextstep.courses.domain;

import java.util.List;
import nextstep.courses.exception.SessionException;
import nextstep.courses.exception.SessionException.SessionFeeNotEqualException;
import nextstep.courses.exception.SessionException.SessionFullException;
import nextstep.courses.exception.SessionException.SessionNotOpenException;
import nextstep.users.domain.NsUser;

public class Session {
Expand All @@ -10,55 +14,51 @@ public class Session {
private final Thumbnail thumbnail;
private final SessionType sessionType;
private final SessionStatus sessionStatus;
private final List<NsUser> students;

public Session(Long sessionId, String sessionName, Period sessionPeriod, Thumbnail thumbnail,
SessionType sessionType, SessionStatus sessionStatus, List<NsUser> students) {
SessionType sessionType, SessionStatus sessionStatus) {
this.sessionId = sessionId;
this.sessionName = sessionName;
this.sessionPeriod = sessionPeriod;
this.thumbnail = thumbnail;
this.sessionType = sessionType;
this.sessionStatus = sessionStatus;
this.students = students;
}

public boolean isEnrollmentPossible(Integer sessionFee) {
public boolean isEnrollmentPossible(SessionStudents students, Integer sessionFee) {

Choose a reason for hiding this comment

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

저는 요 부분이 추상화가 되는게 맞지 않나 생각합니다..!

Choose a reason for hiding this comment

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

그리고 요거 enroll할때 내부에서 유효성검사를 하는식으로 해야되지 않을까요 ? 지금은 외부에서 판단하도록 되어있는 것 같아서요!

if (!isRecruiting()) {
return false;
throw new SessionNotOpenException("강의가 모집중인 상태가 아닙니다.");
}
if (!isWithinCapacity()) {
return false;
if (!isWithinCapacity(students)) {
throw new SessionFullException("강의의 수용인원이 다 찼습니다.");
}
if (!checkSessionFeeEquality(sessionFee)) {
return false;
throw new SessionFeeNotEqualException("접수하신 수강료가 강의 수강료와 일치하지 않습니다.");
}
return true;
}
boolean isRecruiting() {
protected boolean isRecruiting() {
return sessionStatus == SessionStatus.RECRUITING;
}

boolean isWithinCapacity() {
return this.sessionType.isWithinCapacity(this.students.size());
protected boolean isWithinCapacity(SessionStudents students) {
return students.isWithinCapacity(this.sessionType);
}

boolean checkSessionFeeEquality(Integer sessionFee) {
protected boolean checkSessionFeeEquality(Integer sessionFee) {
return this.sessionType.checkSessionFeeEquality(sessionFee);
}

public void enroll(NsUser student) {
this.students.add(student);
public Student enroll(SessionStudents students, NsUser user) {
Student student = new Student(this.sessionId, user.getId());
students.enroll(student);
return student;
}

public Long getSessionId() {
return sessionId;
}

public List<NsUser> getStudents() {
return students;
}

@Override
public String toString() {
return "Session{" +
Expand All @@ -68,7 +68,6 @@ public String toString() {
", thumbnail=" + thumbnail +
", sessionType=" + sessionType +
", sessionStatus=" + sessionStatus +
", students=" + students +
'}';
}
}
5 changes: 5 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nextstep.courses.domain;

public interface SessionRepository {
Session findById(Long id);
}
24 changes: 24 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionStudents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nextstep.courses.domain;

import java.util.List;

public class SessionStudents {

private final List<Student> students;

public SessionStudents(List<Student> students) {
this.students = students;
}

public void enroll(Student student) {
students.add(student);
}

public boolean isWithinCapacity(SessionType sessionType) {
return sessionType.isWithinCapacity(students.size());

Choose a reason for hiding this comment

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

sessionType을 가져와서 검증하는게 아니라 sessionStudents가 현재 수강인원을 제공해주는게 더 자연스럽지 않을까요 ?

}

public List<Student> getStudents() {
return students;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nextstep.courses.domain;

public interface SessionStudentsRepository {
int save(Student student);

SessionStudents findBySessionId(Long sessionId);
}
23 changes: 12 additions & 11 deletions src/main/java/nextstep/courses/domain/SessionType.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package nextstep.courses.domain;

public abstract class SessionType {
public interface SessionType {

private final boolean isPaid;

public SessionType(boolean isPaid) {
this.isPaid = isPaid;
static SessionType determineSessionType(boolean isPaid, Long sessionTypeId, Integer maxStudents, Integer sessionFee) {
if (isPaid) {
return new PaidSession(sessionTypeId, maxStudents, sessionFee);
}
return new FreeSession(sessionTypeId);
}

public static SessionType determineSessionType(boolean isPaid, Integer maxStudents, Integer sessionFee) {
if (isPaid) {
return new PaidSession(isPaid, maxStudents, sessionFee);
static SessionType determineSessionTypeByDB(Long freeSessionTypeId, Long paidSessionTypeId, Integer maxStudents, Integer sessionFee) {
if (freeSessionTypeId == null) {
return new PaidSession(paidSessionTypeId, maxStudents, sessionFee);

Choose a reason for hiding this comment

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

SessionType의 구현체들이라 Type을 명시해주던가 다른 이름을 주는게 좋을 것 같아요
개인적으로는 EnrollmentStrategy 같은걸로 만들면 되지 않을까 싶네요..!

그리고 얘는 validateEnrollment 정도만 추상화를 하고, 내부에서 뭘 하는지는 각각의 구현체에 맡기고, 이 데이터들을 필드로 가지고 있는게 아니라 검증 할 때 파라미터로 받으면 되지 않을까요 ?

지금 밑에 있는 두개 메서드는 Free일때는 사실 아예 없어도 될 것 처럼 보여서요!

}
return new FreeSession(isPaid);
return new FreeSession(freeSessionTypeId);
}

public abstract boolean isWithinCapacity(Integer size);
public abstract boolean checkSessionFeeEquality(Integer sessionFee);
boolean isWithinCapacity(Integer size);
boolean checkSessionFeeEquality(Integer sessionFee);
}
42 changes: 42 additions & 0 deletions src/main/java/nextstep/courses/domain/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nextstep.courses.domain;

import java.util.Objects;

public class Student {

private Long id;
private final Long sessionId;
private final Long studentId;

public Student(Long sessionId, Long studentId) {
this(null, sessionId, studentId);
}

public Student(Long id, Long sessionId, Long studentId) {
this.id = id;
this.sessionId = sessionId;
this.studentId = studentId;
}

public Long getSessionId() {
return sessionId;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Student student = (Student) o;
return Objects.equals(sessionId, student.sessionId)
&& Objects.equals(studentId, student.studentId);
}

@Override
public int hashCode() {
return Objects.hash(sessionId, studentId);
}
}
10 changes: 5 additions & 5 deletions src/main/java/nextstep/courses/domain/Thumbnail.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ public class Thumbnail {
private static final Pattern IMAGE_EXTENSION_PATTERN = Pattern.compile(".*\\.(gif|jpe?g|png|svg)$", Pattern.CASE_INSENSITIVE);
private final Integer thumbnailId;
private final String thumbnailName;
private final FileSize thumbnailSize;
private final FileDimensions thumbnailDimensions;
private final ThumbnailSize thumbnailSize;
private final ThumbnailDimensions thumbnailDimensions;

public Thumbnail(Integer thumbnailId, String thumbnailName,
long thumbnailSize, int thumbnailWidth, int thumbnailHeight) {
this(thumbnailId, thumbnailName, new FileSize(thumbnailSize),
new FileDimensions(thumbnailWidth, thumbnailHeight));
this(thumbnailId, thumbnailName, new ThumbnailSize(thumbnailSize),
new ThumbnailDimensions(thumbnailWidth, thumbnailHeight));
}

public Thumbnail(Integer thumbnailId, String thumbnailName,
FileSize thumbnailSize, FileDimensions thumbnailDimensions) {
ThumbnailSize thumbnailSize, ThumbnailDimensions thumbnailDimensions) {
validateThumbnailExt(thumbnailName);
this.thumbnailId = thumbnailId;
this.thumbnailName = thumbnailName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import nextstep.courses.exception.FileException.FileDimensionsException;

public class FileDimensions {
public class ThumbnailDimensions {

private static final int MIN_THUMBNAIL_FILE_LENGTH = 0;
private static final int MAX_THUMBNAIL_FILE_WIDTH = 300;
Expand All @@ -13,7 +13,7 @@ public class FileDimensions {
private final int width;
private final int height;

public FileDimensions(int width, int height) {
public ThumbnailDimensions(int width, int height) {
validateFileDimensions(width, height);
this.width = width;
this.height = height;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nextstep.courses.domain;

public interface ThumbnailRepository {
Thumbnail findById(Integer id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import nextstep.courses.exception.FileException.FileSizeException;

public class FileSize {
public class ThumbnailSize {

private static final long MAX_THUMBNAIL_FILE_SIZE = 1024L * 1024L;
private static final long MIN_THUMBNAIL_FILE_SIZE = 0L;

private final long size;

public FileSize(long size) {
public ThumbnailSize(long size) {
validateFileSize(size);
this.size = size;
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/nextstep/courses/exception/SessionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nextstep.courses.exception;

public class SessionException extends IllegalArgumentException{
public SessionException() {
}

public SessionException(String message) {
super(message);
}
public static class SessionNotOpenException extends SessionException {
public SessionNotOpenException(String message) {
super(message);
}
}
public static class SessionFullException extends SessionException {
public SessionFullException(String message) {
super(message);
}
}

public static class SessionFeeNotEqualException extends SessionException {
public SessionFeeNotEqualException(String message) {
super(message);
}
}
}

Loading