Skip to content

Step2 #684

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

Open
wants to merge 27 commits into
base: jihyun-s
Choose a base branch
from
Open

Step2 #684

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
287591e
refactor step1
kakao-sophia-song Apr 8, 2025
25fa127
refactor step1
kakao-sophia-song Apr 8, 2025
d17c3ca
update readme
kakao-sophia-song Apr 13, 2025
ae553db
class 껍데기 생성
kakao-sophia-song Apr 13, 2025
4a6b84a
[feat] SessionStatus
kakao-sophia-song Apr 13, 2025
8229d60
[feat] Period
kakao-sophia-song Apr 13, 2025
6bb6d4a
[feat] NsUsers
kakao-sophia-song Apr 13, 2025
9c4ca04
[feat] Sessions
kakao-sophia-song Apr 13, 2025
2b438ed
[feat] DimensionTest
kakao-sophia-song Apr 13, 2025
3255c2f
[feat] Dimension
kakao-sophia-song Apr 13, 2025
92250b1
[feat] ImageType
kakao-sophia-song Apr 13, 2025
e569170
[feat] ImageMeta
kakao-sophia-song Apr 13, 2025
709c80d
[feat] Image test
kakao-sophia-song Apr 13, 2025
0fb8c8f
[feat] Image
kakao-sophia-song Apr 13, 2025
f1c5f49
[feat] PaidPolicyTest
kakao-sophia-song Apr 13, 2025
9a3ef7e
[feat] PaidPolicy
kakao-sophia-song Apr 13, 2025
5e789c8
[feat] SessionInformation
kakao-sophia-song Apr 13, 2025
da07e3b
updqte readme
kakao-sophia-song Apr 13, 2025
a89dc5d
[feat] EnrollmentManagerTest
kakao-sophia-song Apr 13, 2025
55d59e2
[feat] EnrollmentManager
kakao-sophia-song Apr 13, 2025
59265e9
[feat] SessionTest
kakao-sophia-song Apr 13, 2025
98e940b
[feat] Session
kakao-sophia-song Apr 13, 2025
40a9966
[feat] 기능구현 마무리
kakao-sophia-song Apr 13, 2025
f9d8f04
피드백 반영
kakao-sophia-song Apr 14, 2025
06e1441
package 위치 수정
kakao-sophia-song Apr 14, 2025
1d255df
테스트 수정
kakao-sophia-song Apr 14, 2025
fc4e374
테스트 수정
kakao-sophia-song Apr 14, 2025
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,47 @@
* QnaService의 비지니스 로직을 도메인 모델로 이동하는 리팩터링을 진행할 때 TDD로 구현한다.
* QnaService의 deleteQuestion() 메서드에 대한 단위 테스트는 src/test/java 폴더 nextstep.qna.service.QnaServiceTest이다.
* 도메인 모델로 로직을 이동한 후에도 QnaServiceTest의 모든 테스트는 통과해야 한다.

## Step2
### 수강 신청 기능 요구사항
* 과정(Course)은 기수 단위로 운영하며, 여러 개의 강의(Session)를 가질 수 있다.
* 강의는 시작일과 종료일을 가진다.
* 강의는 강의 커버 이미지 정보를 가진다.
* 이미지 크기는 1MB 이하여야 한다.
* 이미지 타입은 gif, jpg(jpeg 포함), png, svg만 허용한다.
* 이미지의 width는 300픽셀, height는 200픽셀 이상이어야 하며, width와 height의 비율은 3:2여야 한다.
* 강의는 무료 강의와 유료 강의로 나뉜다.
* 무료 강의는 최대 수강 인원 제한이 없다.
* 유료 강의는 강의 최대 수강 인원을 초과할 수 없다.
* 유료 강의는 수강생이 결제한 금액과 수강료가 일치할 때 수강 신청이 가능하다.
* 강의 상태는 준비중, 모집중, 종료 3가지 상태를 가진다.
* 강의 수강신청은 강의 상태가 모집중일 때만 가능하다.
* 유료 강의의 경우 결제는 이미 완료한 것으로 가정하고 이후 과정을 구현한다.
* 결제를 완료한 결제 정보는 payments 모듈을 통해 관리되며, 결제 정보는 Payment 객체에 담겨 반환된다.

### 프로그래밍 요구사항
* DB 테이블 설계 없이 도메인 모델부터 구현한다.
* 도메인 모델은 TDD로 구현한다.
* 단, Service 클래스는 단위 테스트가 없어도 된다.

### 기능 구현
* 과정 Course
* 기수를 저장하고 Sessions를 가지고 있다.
* 여러 개의 강의 Sessions
* 강의 Session
* 강의 정보 SessionInformation
* 강의 관리 (정책, 수강생, 상태) EnrollmentManager
* 강의 정책 Policy
* FreePolicy 무료
* PaidPolicy 유료
* 강의 상태 SessionStatus
* 강의 수강생들 NsUsers
* 강의 기간 Period
* 강의 커버 이미지 Image
* 강의 커버 이미지 사이즈 Dimension
* 강의 커버 이미지 확장자 ImageType
* 강의 커버 이미지 메타정보 ImageMeta
* fileName, ImageType, size



39 changes: 30 additions & 9 deletions src/main/java/nextstep/courses/domain/Course.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nextstep.courses.domain;

import java.time.LocalDateTime;
import nextstep.sessions.domain.Session;
import nextstep.sessions.domain.Sessions;

public class Course {
private Long id;
Expand All @@ -9,20 +11,25 @@ public class Course {

private Long creatorId;

private int generation;
Copy link
Member

Choose a reason for hiding this comment

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

기수였군요 😃 기수는 Session으로 이미 표현이 된게 아닐까요?
강의 소개 페이지에서 스크롤을 마지막까지 내려보시면 여러 기수가 진행되었음을 확인할 수 있는데요.
그러니 Course는 'TDD, 클린 코드 with Java', Session을 '20기'로 볼 수 있을 것 같은데 어떻게 생각하시나요?


private Sessions sessions = new Sessions();

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

public Course() {
}

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

public Course(Long id, String title, Long creatorId, LocalDateTime createdAt, LocalDateTime updatedAt) {
public Course(Long id, String title, Long creatorId, int generation, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.title = title;
this.generation = generation;
this.creatorId = creatorId;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
Expand All @@ -36,18 +43,32 @@ public Long getCreatorId() {
return creatorId;
}

public int getGeneration() {
return generation;
}

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

public Sessions getSessions() {
return sessions;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

@Override
public String toString() {
return "Course{" +
"id=" + id +
", title='" + title + '\'' +
", creatorId=" + creatorId +
", createdAt=" + createdAt +
", updatedAt=" + updatedAt +
'}';
"id=" + id +
", title='" + title + '\'' +
", creatorId=" + creatorId +
", batchNumber=" + generation +
", sessions=" + sessions +
", createdAt=" + createdAt +
", updatedAt=" + updatedAt +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,33 @@

@Repository("courseRepository")
public class JdbcCourseRepository implements CourseRepository {
private JdbcOperations jdbcTemplate;
private final JdbcOperations jdbcTemplate;

public JdbcCourseRepository(JdbcOperations jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public int save(Course course) {
String sql = "insert into course (title, creator_id, created_at) values(?, ?, ?)";
return jdbcTemplate.update(sql, course.getTitle(), course.getCreatorId(), course.getCreatedAt());
String sql = "insert into course (title, creator_id, batch_number, created_at) values(?, ?, ?, ?)";
return jdbcTemplate.update(sql, course.getTitle(), course.getCreatorId(), course.getGeneration(), course.getCreatedAt());
}

@Override
public Course findById(Long id) {
String sql = "select id, title, creator_id, created_at, updated_at from course where id = ?";
String sql = "select id, title, creator_id, batch_number, created_at, updated_at from course where id = ?";
RowMapper<Course> rowMapper = (rs, rowNum) -> new Course(
rs.getLong(1),
rs.getString(2),
rs.getLong(3),
toLocalDateTime(rs.getTimestamp(4)),
toLocalDateTime(rs.getTimestamp(5)));
rs.getLong("id"),
rs.getString("title"),
rs.getLong("creator_id"),
rs.getInt("batch_number"),
toLocalDateTime(rs.getTimestamp("created_at")),
toLocalDateTime(rs.getTimestamp("updated_at"))
);
return jdbcTemplate.queryForObject(sql, rowMapper, id);
}

private LocalDateTime toLocalDateTime(Timestamp timestamp) {
if (timestamp == null) {
return null;
}
return timestamp.toLocalDateTime();
return timestamp != null ? timestamp.toLocalDateTime() : null;
}
}
38 changes: 38 additions & 0 deletions src/main/java/nextstep/image/domain/Dimension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nextstep.image.domain;

public class Dimension {
Comment on lines +1 to +3
Copy link
Member

Choose a reason for hiding this comment

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

패키지를 분리해 주셨네요 👍
image, policy, sessions 패키지들은 모두 course와 같은 위치가 아닌 하위로 이동하면 어떨까요?

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

private final int width;
private final int height;

public Dimension(int width, int height) {
validate(width, height);

this.width = width;
this.height = height;
}

private void validate(int width, int height) {
if (width < MIN_WIDTH) {
throw new IllegalArgumentException("너비는 " + MIN_WIDTH + " 이상이어야 합니다.");
}
if (height < MIN_HEIGHT) {
throw new IllegalArgumentException("높이는 " + MIN_HEIGHT + " 이상이어야 합니다.");
}
if (width * HEIGHT_RATIO != height * WIDTH_RATIO) {
throw new IllegalArgumentException("비율이 맞지 않습니다. " + WIDTH_RATIO + ":" + HEIGHT_RATIO);
}
}

public int width() {
return width;
}

public int height() {
return height;
}
}
28 changes: 28 additions & 0 deletions src/main/java/nextstep/image/domain/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package nextstep.image.domain;

public class Image {
private static final long MAX_SIZE_IN_BYTES = 1_048_576; // 1MB
Copy link
Member

Choose a reason for hiding this comment

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

이젠 피드백 중에 확인을 못하신 내용이 있는 것 같아요.
PR 페이지 중간에 이렇게 생긴 버튼이 있습니다.
image
클릭하시면 추가 코멘트 들이 있으니 확인 부탁드려요.


private final ImageMeta meta;
private final Dimension dimension;

public Image(ImageMeta meta, Dimension dimension) {
validate(meta, dimension);
this.meta = meta;
this.dimension = dimension;
}

private void validate(ImageMeta meta, Dimension dimension) {
if (meta.size() > MAX_SIZE_IN_BYTES) {
throw new IllegalArgumentException("이미지 크기는 1MB 이하여야 합니다.");
}
}

public ImageMeta meta() {
return meta;
}

public Dimension dimension() {
return dimension;
}
}
25 changes: 25 additions & 0 deletions src/main/java/nextstep/image/domain/ImageMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nextstep.image.domain;

public class ImageMeta {
private final String fileName;
private final ImageType contentType;
private final long size;

public ImageMeta(String fileName, ImageType contentType, long size) {
this.fileName = fileName;
this.contentType = contentType;
this.size = size;
}

public String fileName() {
return fileName;
}

public ImageType contentType() {
return contentType;
}

public long size() {
return size;
}
}
28 changes: 28 additions & 0 deletions src/main/java/nextstep/image/domain/ImageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package nextstep.image.domain;

import java.util.Arrays;
import java.util.Optional;

public enum ImageType {
GIF("image/gif"),
JPEG("image/jpeg"),
JPG("image/jpg"),
PNG("image/png"),
SVG("image/svg+xml");

private final String contentType;

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

public String contentType() {
return contentType;
}

public static Optional<ImageType> from(String type) {
return Arrays.stream(values())
.filter(t -> t.contentType.equalsIgnoreCase(type))
.findFirst();
}
}
8 changes: 8 additions & 0 deletions src/main/java/nextstep/payments/domain/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public Payment(String id, Long sessionId, Long nsUserId, Long amount) {
this.amount = amount;
this.createdAt = LocalDateTime.now();
}

public Long amount() {
return amount;
}

public Long nsUserId() {
return nsUserId;
}
}
11 changes: 11 additions & 0 deletions src/main/java/nextstep/policy/domain/FreePolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nextstep.policy.domain;

import nextstep.payments.domain.Payment;

public class FreePolicy implements Policy {

@Override
public void validate(Payment payment, int currentParticipants) {
// nothing to validate
}
}
31 changes: 31 additions & 0 deletions src/main/java/nextstep/policy/domain/PaidPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nextstep.policy.domain;

import nextstep.payments.domain.Payment;

public class PaidPolicy implements Policy {
private final int max;
private final long fee;

public PaidPolicy(int max, long fee) {
this.max = max;
this.fee = fee;
}

@Override
public void validate(Payment payment, int currentParticipants) {
if (currentParticipants >= max) {
throw new IllegalStateException("정원 초과입니다.");
}
if (!payment.amount().equals(fee)) {
throw new IllegalArgumentException("결제 금액이 일치하지 않습니다.");
}
}

public int max() {
return max;
}

public long fee() {
return fee;
}
}
7 changes: 7 additions & 0 deletions src/main/java/nextstep/policy/domain/Policy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nextstep.policy.domain;

import nextstep.payments.domain.Payment;

public interface Policy {
void validate(Payment payment, int currentParticipants);
}
4 changes: 3 additions & 1 deletion src/main/java/nextstep/qna/domain/Answers.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public void add(Answer answer) {
answers.add(answer);
}

public void validateAllOwnedBy(NsUser user) throws CannotDeleteException {
private void validateAllOwnedBy(NsUser user) throws CannotDeleteException {
if (answers.stream().anyMatch(answer -> !answer.isOwner(user))) {
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
}
}

public List<DeleteHistory> deleteBy(NsUser user) throws CannotDeleteException {
validateAllOwnedBy(user);

List<DeleteHistory> histories = new ArrayList<>();
for (Answer answer : answers) {
histories.add(answer.deleteBy(user));
Expand Down
1 change: 0 additions & 1 deletion src/main/java/nextstep/qna/domain/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public String toString() {

public List<DeleteHistory> deleteBy(NsUser user) throws CannotDeleteException {
validateOwner(user);
answers.validateAllOwnedBy(user);

this.deleted = true;

Expand Down
Loading