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

3단계 - 수강신청(DB 적용) #647

Open
wants to merge 5 commits into
base: yunji1201
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@

- 도메인 모델은 TDD로 구현
- Service 클래스는 단위 테스트가 없어도 된다

### 3단계 - 수강신청(DB 적용)

#### 프로그래밍 요구사항

1. [x] 이미지 생성 기능
2. [x] 이미지 조회 기능
3. [x] 이미지 수정 기능
4. [x] 이미지 삭제 기능
5. [x] 강의 생성 기능
6. [x] 강의 조회 기능
7. [x] 강의 수정 기능
8. [x] 강의 삭제 기능
55 changes: 49 additions & 6 deletions src/main/java/nextstep/courses/domain/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ public class Image {
private static final int MINIMUM_HEIGHT = 200;
private static final Set<String> ALLOWED_TYPES = Set.of("gif", "jpg", "jpeg", "png", "svg");

private final String fileName;
private final int fileSize;
private final String fileType;
private final int width;
private final int height;
private Long id;
private int sessionId;

Choose a reason for hiding this comment

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

이미지가 세션아이디를 알아야 하는 이유가 있을까요 ?

private String fileName;
private int fileSize;
private String fileType;
private int width;
private int height;

public Image(String fileName, int fileSize, String fileType, int width, int height) {
public Image(int sessionId, String fileName, int fileSize, String fileType, int width, int height) {
validateSize(fileSize);
validateFileType(fileType);
validateFileStandard(width, height);

this.sessionId = sessionId;
this.fileName = fileName;
this.fileSize = fileSize;
this.fileType = fileType;
Expand Down Expand Up @@ -49,4 +52,44 @@ private void validateFileStandard(int width, int height) {
}
}

public Long getId() {
return id;
}

public int getSessionId() {
return sessionId;
}

public String getFileName() {
return fileName;
}

public int getFileSize() {
return fileSize;
}

public String getFileType() {
return fileType;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public void setId(long id) {
this.id = id;
}

public void setSessionId(int sessionId) {
this.sessionId = sessionId;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

}
11 changes: 11 additions & 0 deletions src/main/java/nextstep/courses/domain/ImageRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nextstep.courses.domain;

public interface ImageRepository {
int save(Image image);

Image findById(Long id);

int update(Image image);

int deleteById(Long id);
}
83 changes: 0 additions & 83 deletions src/main/java/nextstep/courses/domain/Session.java

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nextstep.courses.domain;

import nextstep.courses.domain.session.Session;

public interface SessionRepository {
int save(Session session);

Session findById(Long id);

int update(Session session);

int deleteById(Long id);
}
20 changes: 20 additions & 0 deletions src/main/java/nextstep/courses/domain/session/FreeSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nextstep.courses.domain.session;

import nextstep.courses.constants.SessionStatus;
import nextstep.courses.domain.Image;

import java.time.LocalDate;

public class FreeSession extends Session {
public FreeSession(Long id, Long courseId, String title, LocalDate startDate, LocalDate endDate, Image sessionImage) {
super(id, courseId, title, startDate, endDate, sessionImage, SessionStatus.READY);
}

@Override
public void enroll(int payment) {
if (status != SessionStatus.OPEN) {
throw new IllegalStateException("수강 신청은 모집중인 상태에서만 가능합니다.");
}
enrollCount++;
}
}
43 changes: 43 additions & 0 deletions src/main/java/nextstep/courses/domain/session/PaidSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package nextstep.courses.domain.session;

import nextstep.courses.constants.SessionStatus;
import nextstep.courses.domain.Image;

import java.time.LocalDate;

public class PaidSession extends Session {
private int maxEnrollment;

Choose a reason for hiding this comment

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

필드를 굳이 구현체에 따라 나눌 필요가 있을까요 ? 디비상으로는 무료강의도 값을 가지고 있긴 할텐데요!

private int sessionFee;

public abstract class Session {
protected Long courseId;

public Session(Long id, Long courseId, String title, LocalDate startDate, LocalDate endDate, Image sessionImage, SessionStatus status) {
this.courseId = courseId;
}
}

public PaidSession(Long courseId, String title, LocalDate startDate, LocalDate endDate, Image sessionImage, int maxEnrollment, int sessionFee) {
super(null, courseId, title, startDate, endDate, sessionImage, SessionStatus.READY);
this.maxEnrollment = maxEnrollment;
this.sessionFee = sessionFee;
}

@Override
public void enroll(int payment) {

Choose a reason for hiding this comment

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

요거는 꼭 그래야되는 건 아닌데, 유효성검사의 책임을 다른 클래스로 위임하는건 어떨까요 ?
type에 따라 유효성검사를 결정하는 팩토리 클래스를 두면 굳이 세션 자체를 확장하지 않아도 구현이 가능할 것 같아서요!
DB랑 연동하게 되면 Session이 엔티티 클래스가 되는데 혼잡해지지 않을까 해서 의견드려봤습니다 ~

if (status != SessionStatus.OPEN) {
throw new IllegalStateException("수강 신청은 모집중인 상태에서만 가능합니다.");
}
if (enrollCount >= maxEnrollment) {
throw new IllegalStateException("수강 인원이 초과되었습니다.");
}
if (payment != sessionFee) {
throw new IllegalArgumentException("결제 금액이 수강료와 일치하지 않습니다.");
}
enrollCount++;
}

public int getSessionFee() {
return sessionFee;
}
}
87 changes: 87 additions & 0 deletions src/main/java/nextstep/courses/domain/session/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package nextstep.courses.domain.session;

import nextstep.courses.constants.SessionStatus;
import nextstep.courses.domain.Image;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public abstract class Session {
protected Long id;
protected Long courseId;
protected String title;
protected LocalDate startDate;
protected LocalDate endDate;
protected Image sessionImage;
protected int enrollCount;
protected SessionStatus status;

public Session(Long id, Long courseId, String title, LocalDate startDate, LocalDate endDate, Image sessionImage, SessionStatus status) {
validateDate(startDate, endDate);
this.id = id;
this.courseId = courseId;
this.title = title;
this.startDate = startDate;
this.endDate = endDate;
this.sessionImage = sessionImage;
this.status = status;
this.enrollCount = 0;
}


public Session(String title, LocalDate startDate, LocalDate endDate, Image sessionImage, SessionStatus status) {
this(null, null, title, startDate, endDate, sessionImage, status);
}

private void validateDate(LocalDate startDate, LocalDate endDate) {
if (startDate.isAfter(endDate)) {
throw new IllegalArgumentException("시작일은 종료일 이전이어야 합니다.");
}
}

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

public void closeEnrollment() {
this.status = SessionStatus.CLOSED;
}

public abstract void enroll(int payment);

public int getEnrolledCount() {
return enrollCount;
}

public Long getId() {
return id;
}

public Long getCourseId() {
return courseId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public long getDuration() {
return ChronoUnit.DAYS.between(startDate, endDate);
}

public LocalDate getStartTime() {
return this.startDate;
}

public LocalDate getEndTime() {
return this.endDate;
}

public void setId(long id) {
this.id = id;
}
}
Loading