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 (DB 적용) #399

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions src/main/java/nextstep/courses/domain/Enrollment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public Enrollment(SessionState sessionState, Students students) {
this.students = students;
}

public SessionState getSessionState() {
return sessionState;
}

public Students getStudents() {
return students;
}

public void enroll(NsUser loginUser) {
if (!this.sessionState.isRecruiting()) {
throw new IllegalArgumentException("수강신청은 모집중에만 가능합니다");
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/nextstep/courses/domain/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ public Image(int size, String type, int width, int height) {
this(new ImageSize(size, width, height), ImageType.of(type));
}

public ImageSize getImageSize() {
return imageSize;
}

public ImageType getImageType() {
return imageType;
}

public Image(ImageSize imageSize, ImageType imageType) {
this.imageSize = imageSize;
this.imageType = imageType;
}

}
12 changes: 12 additions & 0 deletions src/main/java/nextstep/courses/domain/ImageSize.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ private void validateSize(int size) {
throw new IllegalArgumentException("이미지 크기는 1MB 이하여야 한다");
}
}

public int getSize() {
return size;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}
}
4 changes: 4 additions & 0 deletions src/main/java/nextstep/courses/domain/ImageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public enum ImageType {

private String type;

public String getType() {
return type;
}

ImageType(String type) {
this.type = type;
}
Expand Down
36 changes: 33 additions & 3 deletions src/main/java/nextstep/courses/domain/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
import java.time.LocalDate;

public class Session {
private long id;
private final String title;
private final Image image;
private final SessionPeriod sessionPeriod;
private final Enrollment enrollment;
private final SessionType sessionType;
private final int price;

public Session(String title, int size, String imageType,
public Session(long id, String title, int size, String imageType,
int width, int height,
LocalDate startedAt, LocalDate endedAt,
String state, String sessionType,
int capacity, int price) {
this(title, new Image(size, imageType, width, height),
this(id, title, new Image(size, imageType, width, height),
new SessionPeriod(startedAt, endedAt), new Enrollment(SessionState.of(state), capacity),
SessionType.of(sessionType), price);
}

public Session(String title, Image image,
public Session(long id, String title, Image image,
SessionPeriod sessionPeriod, Enrollment enrollment,
SessionType sessionType, int price ) {
this.id = id;
this.title = title;
this.image = image;
this.sessionPeriod = sessionPeriod;
Expand All @@ -33,6 +35,34 @@ public Session(String title, Image image,
this.price = price;
}

public long getId() {
return id;
}

public String getTitle() {
return title;
}

public Image getImage() {
return image;
}

public SessionPeriod getSessionPeriod() {
return sessionPeriod;
}

public Enrollment getEnrollment() {
return enrollment;
}

public SessionType getSessionType() {
return sessionType;
}

public int getPrice() {
return price;
}

public void enroll(NsUser loginUser) {
enrollment.enroll(loginUser);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionPeriod.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public SessionPeriod(LocalDate startedAt, LocalDate endedAt) {
this.endedAt = endedAt;
}

public LocalDate getStartedAt() {
return startedAt;
}

public LocalDate getEndedAt() {
return endedAt;
}

private void validateDate(LocalDate startedAt, LocalDate endedAt) {
if (startedAt.isAfter(endedAt)) {
throw new IllegalArgumentException("강의 시작인은 종료일 이후여야 합니다");
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nextstep.courses.domain;

public interface SessionRepository {
int save(Session session);

Session findById(Long id);
}
4 changes: 4 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionState.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static SessionState of(String state) {
.orElseThrow(() -> new IllegalArgumentException("지원하지 않는 세션 상태입니다."));
}

public String getState() {
return state;
}

public boolean isRecruiting() {
return this == RECRUITING;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/nextstep/courses/domain/SessionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public enum SessionType {
this.type = type;
}

public String getType() {
return type;
}

public static SessionType of(String type) {
return Arrays.stream(values())
.filter(it -> it.type.equals(type))
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/nextstep/courses/domain/Students.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public void add(NsUser loginUser) {
}
this.students.add(loginUser);
}

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

public int getCapacity() {
return capacity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package nextstep.courses.infrastructure;

import nextstep.courses.domain.Course;
import nextstep.courses.domain.CourseRepository;
import nextstep.courses.domain.Session;
import nextstep.courses.domain.SessionRepository;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

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

@Repository("sessionRepository")
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 (id, title, image_size, image_type, image_width, image_height, " +
"started_at, ended_at, state, session_type, capacity, price) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

return jdbcTemplate.update(sql,
session.getId(),
session.getTitle(),
session.getImage().getImageSize().getSize(),
session.getImage().getImageType().getType(),
session.getImage().getImageSize().getWidth(),
session.getImage().getImageSize().getHeight(),
java.sql.Date.valueOf(session.getSessionPeriod().getStartedAt()),
java.sql.Date.valueOf(session.getSessionPeriod().getEndedAt()),
session.getEnrollment().getSessionState().getState(),
session.getSessionType().toString(),
session.getEnrollment().getStudents().getCapacity(),
session.getPrice());
}

@Override
public Session findById(Long id) {
String sql = "select id, title, image_size, image_type, image_width, image_height, " +
"started_at, ended_at, state, session_type, capacity, price " +
"from session where id = ?";

RowMapper<Session> rowMapper = (rs, rowNum) -> new Session(
rs.getLong("id"),
rs.getString("title"),
rs.getInt("image_size"),
rs.getString("image_type"),
rs.getInt("image_width"),
rs.getInt("image_height"),
toLocalDate(rs.getDate("started_at")),
toLocalDate(rs.getDate("ended_at")),
rs.getString("state"),
rs.getString("session_type"),
rs.getInt("capacity"),
rs.getInt("price"));

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

private LocalDate toLocalDate(Date timestamp) {
if (timestamp == null) {
return null;
}
return timestamp.toLocalDate();
}
}
23 changes: 23 additions & 0 deletions src/main/java/nextstep/courses/service/SessionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nextstep.courses.service;

import nextstep.courses.domain.Session;
import nextstep.courses.domain.SessionRepository;
import nextstep.users.domain.NsUser;
import nextstep.users.domain.UserRepository;

public class SessionService {
private final SessionRepository sessionRepository;
private final UserRepository userRepository;
public SessionService(SessionRepository sessionRepository, UserRepository userRepository) {
this.sessionRepository = sessionRepository;
this.userRepository = userRepository;
}

public void enroll(long sessionId, String userId) {
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의 관계가 m:n 관계이다.
이 관계를 담당할 테이블과 관련 로직이 필요하지 않을까?

수강 신청한 학생의 목록을 관리하면서 기존의 Enrollment 도메인 객체를 최대한 활용할 수 있는 방법을 찾아보는 것이 이 단계의 핵심 미션이다.
이와 관련해 어떻게 해결하는 것이 좋을지 방법을 찾아보면 좋겠다.

NsUser loginUser = this.userRepository.findByUserId(userId);
Session session = this.sessionRepository.findById(sessionId);

session.enroll(loginUser);
sessionRepository.save(session);
}
}
2 changes: 1 addition & 1 deletion src/main/java/nextstep/users/domain/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.util.Optional;

public interface UserRepository {
Optional<NsUser> findByUserId(String userId);
NsUser findByUserId(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public JdbcUserRepository(JdbcOperations jdbcTemplate) {
}

@Override
public Optional<NsUser> findByUserId(String userId) {
public NsUser findByUserId(String userId) {
String sql = "select id, user_id, password, name, email, created_at, updated_at from ns_user where user_id = ?";
RowMapper<NsUser> rowMapper = (rs, rowNum) -> new NsUser(
rs.getLong(1),
Expand All @@ -29,7 +29,7 @@ public Optional<NsUser> findByUserId(String userId) {
rs.getString(5),
toLocalDateTime(rs.getTimestamp(6)),
toLocalDateTime(rs.getTimestamp(7)));
return Optional.of(jdbcTemplate.queryForObject(sql, rowMapper, userId));
return jdbcTemplate.queryForObject(sql, rowMapper, userId);
}

private LocalDateTime toLocalDateTime(Timestamp timestamp) {
Expand Down