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 적용) #532

Open
wants to merge 27 commits into
base: sm9171
Choose a base branch
from
Open
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
6546c05
docs: 2단계 요구사항
sm9171 Apr 12, 2024
0fee513
feat: 과정은 기수를 가지고 생성된다 test 및 기능 구현
sm9171 Apr 12, 2024
a2616d3
test: 과정은 여러 개의 강의를 가지고 생성된다. fail test
sm9171 Apr 12, 2024
9af8b2e
feat: 강의 도메인 생성
sm9171 Apr 12, 2024
d06b825
feat: image 도메인 생성
sm9171 Apr 12, 2024
e2096c1
refactor: ImageSize 래퍼클래스 생성 및 테스트 성공
sm9171 Apr 14, 2024
19e2c38
refactor: ImageHeight, ImageWidth 래퍼클래스 생성 및 테스트 성공
sm9171 Apr 14, 2024
0ad242c
refactor: image, session 도메인 분리
sm9171 Apr 17, 2024
d8a8613
Merge remote-tracking branch 'origin/step2' into step2
sm9171 Apr 17, 2024
c780198
feat: Seccions 일급 컬랙션 생성
sm9171 Apr 18, 2024
d192c42
feat: 무료강의, 유료강의 타입 생성, 테스트 2개 성공
sm9171 Apr 18, 2024
99445e9
feat: '유료 강의는 수강생이 결제한 금액과 수강료가 일치할 때 수강 신청이 가능' 테스트 성공
sm9171 Apr 18, 2024
6b42b1e
feat: '강의 수강신청은 강의 상태가 모집중일 때만 가능' 테스트 성공
sm9171 Apr 19, 2024
0896fa9
refactor: 사용안하는 테스트 제거
sm9171 Apr 19, 2024
29bd931
Merge remote-tracking branch 'refs/remotes/javajigi/sm9171' into step2
sm9171 Apr 19, 2024
25a6931
refactor: imageWidth, imageHeight 필드명 변경
sm9171 Apr 21, 2024
8ca928b
refactor: EndedAt, StartedAt 래핑클래스로 분리
sm9171 Apr 21, 2024
d62232c
refactor: sessionType description 필드 추가
sm9171 Apr 21, 2024
409d49c
refactor: Charge,Enrollment,SessionInfo 클래스 분리
sm9171 Apr 21, 2024
785c218
test: payment 완료 테스트 성공
sm9171 Apr 22, 2024
91cfaef
test: 결제금액과 강의금액이 다르면 결제가 되지 않는다
sm9171 Apr 22, 2024
fdf3e6d
refactor: Payment 생성자에 ChargeStatus 인수값 추가
sm9171 Apr 22, 2024
d58ad38
feat: sessionRepository 생성
sm9171 Apr 29, 2024
00ce7b2
test: imageRepositoryTest 완료
sm9171 May 3, 2024
5bd6404
test: sessionRepositoryTest 수정
sm9171 May 3, 2024
26946f9
Merge remote-tracking branch 'javajigy/sm9171' into step3
sm9171 May 3, 2024
7eef947
test: sessionRepositoryTest 완료
sm9171 May 3, 2024
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
32 changes: 31 additions & 1 deletion src/main/java/nextstep/image/domain/Image.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
package nextstep.image.domain;

public class Image {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

private Long id;
private Long sessionId;
private String url;
private ImageType type;
private ImageShape shape;
private Integer size;

public Image(final ImageType type, final ImageShape shape, final Integer size) {
public Image(final long id, final long sessionId, final String url, final ImageType type, final ImageShape shape, final Integer size) {
this.id = id;
this.sessionId = sessionId;
this.url = url;
this.type = type;
this.shape = shape;
this.size = size;
}

public Long getSessionId() {
return sessionId;
}

public String getUrl() {
return url;
}

public String getType() {
return type.name();
}

public int getImageWidth() {
return shape.getWidth();
}

public int getImageHeight() {
return shape.getHeight();
}

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

public interface ImageRepository {
int save(final Image image);
Image findById(final Long id);
}
12 changes: 12 additions & 0 deletions src/main/java/nextstep/image/domain/ImageShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public class ImageShape {
private ImageWidth width;
private ImageHeight height;

public ImageShape(final int width, final int height) {
this(new ImageWidth(width), new ImageHeight(height));
}

public ImageShape(ImageWidth width, ImageHeight height) {
validateShape(width, height);
this.width = width;
Expand All @@ -15,4 +19,12 @@ private static void validateShape(final ImageWidth width, final ImageHeight heig
throw new IllegalArgumentException("가로 세로 비율은 3:2이어야 합니다.");
}
}

public int getWidth() {
return width.getWidth();
}

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

import nextstep.courses.domain.Course;
import nextstep.image.domain.*;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;

import java.sql.Timestamp;
import java.time.LocalDateTime;

public class JdbcImageRepository implements ImageRepository {
private JdbcOperations jdbcTemplate;

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

@Override
public int save(Image image) {
String sql = "insert into image (session_id, url, image_type, image_height, image_width, size) values(?, ?, ?,?, ?, ?)";
return jdbcTemplate.update(sql, image.getSessionId(), image.getUrl(), image.getType(), image.getImageHeight(), image.getImageWidth(), image.getSize());
}

@Override
public Image findById(Long id) {
String sql = "select id, session_id, url, image_type, image_width, image_height, size from image where id = ?";
RowMapper<Image> rowMapper = (rs, rowNum) -> new Image(
rs.getLong(1),
rs.getLong(2),
rs.getString(3),
ImageType.valueOf(rs.getString(4)),
new ImageShape(rs.getInt(5), rs.getInt(6)),
rs.getInt(7));
return jdbcTemplate.queryForObject(sql, rowMapper, id);
}

private LocalDateTime toLocalDateTime(Timestamp timestamp) {
if (timestamp == null) {
return null;
}
return timestamp.toLocalDateTime();
}
}
8 changes: 8 additions & 0 deletions src/main/java/nextstep/sessions/domain/Charge.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ public Charge(ChargeStatus status, int price) {
this.status = status;
this.price = price;
}

public ChargeStatus getStatus() {
return status;
}

public int getPrice() {
return price;
}
}
11 changes: 11 additions & 0 deletions src/main/java/nextstep/sessions/domain/Enrollment.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ private boolean isFullCapacity() {
return students.size() == capacity;
}

public SessionStatus getSessionStatus() {
return sessionStatus;
}

public int getCapacity() {
return capacity;
}

public List<NsUser> getStudents() {
return students;
}
}
52 changes: 43 additions & 9 deletions src/main/java/nextstep/sessions/domain/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,59 @@

public class Session {

private final SessionInfo sessionInfo;
private final Charge charge;
private final Long id;
private final SessionInfo sessionInfo;
private final Charge charge;
private final Enrollment enrollment;
private final SessionDate sessionDate;
private final SessionDate sessionDate;

public Session(String title, Long creatorId,
ChargeStatus chargeStatus, int price,
int capacity, SessionStatus sessionStatus,
LocalDate startDate, LocalDate endDate) {
this(new SessionInfo(title, creatorId, null),
public Session(final Long id, String title, Long creatorId,
ChargeStatus chargeStatus, int price,
int capacity, SessionStatus sessionStatus,
LocalDate startDate, LocalDate endDate) {
this(id, new SessionInfo(title, creatorId, null),
new Charge(chargeStatus, price),
new Enrollment(sessionStatus, capacity),
new SessionDate(new StartedAt(startDate), new EndedAt(endDate)));
}

public Session(SessionInfo sessionInfo, Charge charge, Enrollment enrollment, SessionDate sessionDate) {
public Session(final Long id, SessionInfo sessionInfo, Charge charge, Enrollment enrollment, SessionDate sessionDate) {
this.id = id;
this.sessionInfo = sessionInfo;
this.charge = charge;
this.enrollment = enrollment;
this.sessionDate = sessionDate;
}

public String getTitle() {
return sessionInfo.getTitle();
}

public Long getCreatorId() {
return sessionInfo.getCreatorId();
}

public String getChargeStatus() {
return charge.getStatus().name();
}

public int getPrice() {
return charge.getPrice();
}

public LocalDate getStartedAt() {
return sessionDate.getStartedAt();
}

public LocalDate getEndedAt() {
return sessionDate.getEndedAt();
}

public String getSessionStatus() {
return enrollment.getSessionStatus().name();
}

public int getCapacity() {
return enrollment.getCapacity();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Session과 NsUser의 관계는 어떻게 될까?

  • 한 Session은 n명이 수강할 수 있다.
  • 한명의 NsUser는 n개의 Session을 수강신청할 수 있다.

위와 같은 관계이므로 Session : NsUser는 m : n 관계이다.
보통 DB 설계할 때 m : n는 어떻게 설계하는지를 고려해 테이블을 추가하고 매핑해 보면 좋겠다.

8 changes: 8 additions & 0 deletions src/main/java/nextstep/sessions/domain/SessionDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ private static void validateSessionDate(final LocalDate startedAt, final LocalDa
throw new IllegalArgumentException("강의 종료일보다 강의 시작일이 늦을 수 없습니다.");
}
}

public LocalDate getStartedAt() {
return startedAt.getStartedAt();
}

public LocalDate getEndedAt() {
return endedAt.getEndedAt();
}
}
12 changes: 12 additions & 0 deletions src/main/java/nextstep/sessions/domain/SessionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ public SessionInfo(String title, Long creatorId, Image image) {
this.creatorId = creatorId;
this.image = image;
}

public String getTitle() {
return title;
}

public Long getCreatorId() {
return creatorId;
}

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

public interface SessionRepository {
int save(final Session session);
Session findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package nextstep.sessions.infrastructure;

import nextstep.sessions.domain.*;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;

import java.sql.Timestamp;
import java.time.LocalDate;

public class JdbcSessionRepository implements SessionRepository {
private JdbcOperations jdbcTemplate;

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

@Override
public int save(Session session) {
String sql = "insert into session (title, creator_id, charge_status, price, session_status, capacity, started_at, ended_at) values(?, ?, ?,?, ?, ?, ?, ?)";
return jdbcTemplate.update(sql, session.getTitle(), session.getCreatorId(),session.getChargeStatus(), session.getPrice(), session.getSessionStatus(), session.getCapacity(), session.getStartedAt(), session.getEndedAt());
}

@Override
public Session findById(Long id) {
String sql = "select session.id, session.title, session.creator_id, session.charge_status, session.price, session.capacity, session.session_status,session.started_at, session.ended_at " +
"from session where id = ?";

RowMapper<Session> rowMapper = (rs, rowNum) -> new Session(
rs.getLong(1),
rs.getString(2),
rs.getLong(3),
ChargeStatus.valueOf(rs.getString(4)),
rs.getInt(5),
rs.getInt(6),
SessionStatus.valueOf(rs.getString(7)),
toLocalDate(rs.getTimestamp(8)),
toLocalDate(rs.getTimestamp(9))
);

return jdbcTemplate.queryForObject(sql, rowMapper, id);
}

private LocalDate toLocalDate(Timestamp timestamp) {
if (timestamp == null) {
return null;
}
return timestamp.toLocalDateTime().toLocalDate();
}
}
24 changes: 24 additions & 0 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,27 @@ create table delete_history (
deleted_by_id bigint,
primary key (id)
);

create table image (
id bigint generated by default as identity,
session_id bigint not null,
url varchar(255),
image_type varchar(255),
image_width bigint,
image_height bigint,
size bigint,
primary key (id)
);

create table session (
id bigint generated by default as identity,
title varchar(255) not null,
creator_id bigint not null,
charge_status varchar(255) not null,
price bigint not null,
session_status varchar(255) not null,
capacity bigint not null,
started_at timestamp,
ended_at timestamp,
primary key (id)
);
4 changes: 2 additions & 2 deletions src/test/java/fixture/sessions/domain/SessionFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

public class SessionFixture {
public static Session createSessionWithEnrollment(int capacity, SessionStatus status) {
return new Session(
return new Session(1L,
"TDD, 클린 코드 with Java 16기", 2L,
ChargeStatus.PAID, 800_000,
capacity, status,
LocalDate.now(), LocalDate.now().plusDays(30));
}
public static Session createSessionWithSessionDate(LocalDate startDate, LocalDate endDate) {
return new Session(
return new Session(1L,
"TDD, 클린 코드 with Java 16기", 2L,
ChargeStatus.PAID, 800_000,
10, SessionStatus.OPENED,
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/nextstep/image/domain/ImageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package nextstep.image.domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ImageTest {
@DisplayName("이미지 크기는 1MB 이하여야 한다.")
@Test
public void imageSizeLessThan1MB() {
//given & when & then
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> {
new ImageSize(1000000);
}).withMessageMatching("이미지 크기는 1메가 바이트를 넘을 수 없습니다.");
}

@DisplayName("이미지의 width는 300픽셀 이상이어야 한다.")
@Test
public void imageWidthOver300Pixel() {
//given
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> {
new ImageWidth(200);
}).withMessageMatching("이미지 너비는 300px보다 커야 합니다.");
}

@DisplayName("이미지의 height는 200픽셀 이상이어야 한다.")
@Test
public void imageHeightOver200Pixel() {
//given
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> {
new ImageHeight(100);
}).withMessageMatching("이미지 높이는 200px보다 커야 합니다.");
}

@DisplayName("이미지의 width와 height의 비율은 3:2여야 한다.")
@Test
public void widthAndHeightRatioEqual3To2() {
//given
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> {
new ImageShape(new ImageWidth(400), new ImageHeight(400));
}).withMessageMatching("가로 세로 비율은 3:2이어야 합니다.");
}
}
Loading