-
Notifications
You must be signed in to change notification settings - Fork 299
3단계 - 수강신청(DB 적용) #769
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
base: heewonham
Are you sure you want to change the base?
3단계 - 수강신청(DB 적용) #769
Changes from 1 commit
845e2a8
fbca015
37e1180
623f840
c17e5ec
f8e00f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,5 @@ out/ | |
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
|
|
||
| /bin/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,72 +7,116 @@ | |
| import nextstep.courses.domain.PaidSession; | ||
| import nextstep.courses.domain.FreeSession; | ||
|
|
||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
| import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | ||
| import org.springframework.stereotype.Repository; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Repository | ||
| public class JdbcSessionRepository { | ||
| private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; | ||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| public JdbcSessionRepository(JdbcTemplate jdbcTemplate) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void save(Session session, Long courseId) { | ||
| Long imageId = null; | ||
| if (session.getCoverImage() != null) { | ||
| imageId = insertImage(session.getCoverImage()); | ||
| } | ||
|
|
||
| String sql = "insert into session (name, type, start_date, end_date, status, capacity, tuition_fee, image_id, course_id) " + | ||
| "values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; | ||
|
|
||
| jdbcTemplate.update(sql, | ||
| session.getName(), | ||
| session.getType().name(), | ||
| session.getPeriod().getStartDate(), | ||
| session.getPeriod().getEndDate(), | ||
| session.getStatus().name(), | ||
| session instanceof PaidSession ? ((PaidSession) session).getCapacity() : null, | ||
| session instanceof PaidSession ? ((PaidSession) session).getTuitionFee() : null, | ||
| imageId, | ||
| courseId | ||
| ); | ||
| SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate) | ||
| .withTableName("session") | ||
| .usingGeneratedKeyColumns("id"); | ||
|
|
||
| Map<String, Object> params = new HashMap<>(); | ||
| params.put("name", session.getName()); | ||
| params.put("type", session.getType().name()); | ||
| params.put("start_date", session.getPeriod().getStartDate()); | ||
| params.put("end_date", session.getPeriod().getEndDate()); | ||
| params.put("status", session.getStatus().name()); | ||
| params.put("capacity", session instanceof PaidSession ? ((PaidSession) session).getCapacity().getValue() : null); | ||
| params.put("tuition_fee", session instanceof PaidSession ? ((PaidSession) session).getTuitionFee().getValue() : null); | ||
| params.put("image_id", imageId); | ||
| params.put("course_id", courseId); | ||
|
|
||
| insert.execute(params); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void registerStudent(Long sessionId, Long studentId) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 동작이 틀리지는 않았는데요
Session 도메인 모델의 session.register(...);
sessionRepository.save(session); // session 도메인 모델 내 수강생들도 함께 저장 |
||
| String sql = "insert into session_student (session_id, student_id) values (:sessionId, :studentId)"; | ||
| MapSqlParameterSource params = new MapSqlParameterSource() | ||
| .addValue("sessionId", sessionId) | ||
| .addValue("studentId", studentId); | ||
| namedParameterJdbcTemplate.update(sql, params); | ||
| } | ||
|
|
||
| public List<Session> findAll() { | ||
| String sql = "select s.*, i.file_name, i.content_type, i.size_in_bytes, i.width, i.height " + | ||
| "from session s left join image i on s.image_id = i.id"; | ||
| return jdbcTemplate.query(sql, sessionRowMapper()); | ||
| List<Session> sessions = namedParameterJdbcTemplate.query(sql, sessionRowMapper()); | ||
|
|
||
| // 각 세션에 대한 수강생 정보를 함께 조회 | ||
| for (Session session : sessions) { | ||
| List<Long> studentIds = findStudentIdsBySessionId(session.getId()); | ||
| for (Long studentId : studentIds) { | ||
| session.register(studentId, null); | ||
| } | ||
| } | ||
| return sessions; | ||
| } | ||
|
|
||
| public Session findById(Long id) { | ||
| String sql = "select s.*, i.file_name, i.content_type, i.size_in_bytes, i.width, i.height " + | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
검색만 하니 |
||
| "from session s left join image i on s.image_id = i.id where s.id = ?"; | ||
| return jdbcTemplate.queryForObject(sql, sessionRowMapper(), id); | ||
| "from session s left join image i on s.image_id = i.id where s.id = :id"; | ||
| Session session = namedParameterJdbcTemplate.queryForObject(sql, | ||
| new MapSqlParameterSource("id", id), | ||
| sessionRowMapper()); | ||
|
|
||
| if (session != null) { | ||
| // 세션의 수강생 정보를 함께 조회 | ||
| List<Long> studentIds = findStudentIdsBySessionId(id); | ||
| for (Long studentId : studentIds) { | ||
| session.register(studentId, null); | ||
| } | ||
| } | ||
| return session; | ||
| } | ||
|
|
||
| @Transactional | ||
| public void update(Session session) { | ||
| String sql = "update session set name = ?, start_date = ?, end_date = ?, status = ?, capacity = ?, tuition_fee = ? where id = ?"; | ||
| jdbcTemplate.update(sql, | ||
| session.getName(), | ||
| session.getPeriod().getStartDate(), | ||
| session.getPeriod().getEndDate(), | ||
| session.getStatus().name(), | ||
| session instanceof PaidSession ? ((PaidSession) session).getCapacity() : null, | ||
| session instanceof PaidSession ? ((PaidSession) session).getTuitionFee() : null, | ||
| session.getId() | ||
| ); | ||
| String sql = "update session set name = :name, start_date = :startDate, end_date = :endDate, " + | ||
| "status = :status, capacity = :capacity, tuition_fee = :tuitionFee where id = :id"; | ||
|
|
||
| MapSqlParameterSource params = new MapSqlParameterSource() | ||
| .addValue("name", session.getName()) | ||
| .addValue("startDate", session.getPeriod().getStartDate()) | ||
| .addValue("endDate", session.getPeriod().getEndDate()) | ||
| .addValue("status", session.getStatus().name()) | ||
| .addValue("capacity", session instanceof PaidSession ? ((PaidSession) session).getCapacity().getValue() : null) | ||
| .addValue("tuitionFee", session instanceof PaidSession ? ((PaidSession) session).getTuitionFee().isSameAmount(0) ? 0 : null : null) | ||
| .addValue("id", session.getId()); | ||
|
|
||
| namedParameterJdbcTemplate.update(sql, params); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteById(Long id) { | ||
| jdbcTemplate.update("delete from session where id = ?", id); | ||
| deleteAllStudentsBySessionId(id); | ||
| String sql = "delete from session where id = :id"; | ||
| namedParameterJdbcTemplate.update(sql, new MapSqlParameterSource("id", id)); | ||
| } | ||
|
|
||
| private Long insertImage(Image image) { | ||
|
|
@@ -90,6 +134,18 @@ private Long insertImage(Image image) { | |
| return insert.executeAndReturnKey(params).longValue(); | ||
| } | ||
|
|
||
| private List<Long> findStudentIdsBySessionId(Long sessionId) { | ||
| String sql = "select student_id from session_student where session_id = :sessionId"; | ||
| return namedParameterJdbcTemplate.query(sql, | ||
| new MapSqlParameterSource("sessionId", sessionId), | ||
| (rs, rowNum) -> rs.getLong("student_id")); | ||
| } | ||
|
|
||
| private void deleteAllStudentsBySessionId(Long sessionId) { | ||
| String sql = "delete from session_student where session_id = :sessionId"; | ||
| namedParameterJdbcTemplate.update(sql, new MapSqlParameterSource("sessionId", sessionId)); | ||
| } | ||
|
|
||
| private RowMapper<Session> sessionRowMapper() { | ||
| return (rs, rowNum) -> { | ||
| SessionStatus status = SessionStatus.valueOf(rs.getString("status")); | ||
|
|
||
This file was deleted.
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이 courseId를 가지는 구조는 어떤가요?