-
Notifications
You must be signed in to change notification settings - Fork 293
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
Closed
Step3 (DB 적용) #399
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/nextstep/courses/infrastructure/JdbcSessionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/main/java/nextstep/courses/service/SessionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
NsUser loginUser = this.userRepository.findByUserId(userId); | ||
Session session = this.sessionRepository.findById(sessionId); | ||
|
||
session.enroll(loginUser); | ||
sessionRepository.save(session); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 도메인 객체를 최대한 활용할 수 있는 방법을 찾아보는 것이 이 단계의 핵심 미션이다.
이와 관련해 어떻게 해결하는 것이 좋을지 방법을 찾아보면 좋겠다.