-
Notifications
You must be signed in to change notification settings - Fork 307
๐ 3๋จ๊ณ - ์๊ฐ์ ์ฒญ(DB ์ ์ฉ) #802
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
Conversation
javajigi
left a comment
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.
๊ธฐ์กด์ ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ ์ ์งํ๋ฉด db ๋งคํ ๋ ์ ํ๋ค์. ๐ฏ
Entity/Mapper ํจํด์ ๋์
ํด ๋๋ฉ์ธ ๊ฐ์ฒด๊ฐ db ์ฐ๊ฒฐ๋ก ์ธํด ์ํฅ์ ๋ฐ์ง ์๋๋ก ์ต์ํ ๊ฒ๋ ์ธ์์ ์ด๋ค์.
Entity ๊ฐ์ฒด์ ์ฌ์ฉ์ ๋ํ ํผ๋๋ฐฑ์ด ์์ด ๋จ๊ฒจ๋ดค์ด์.
์์ ๋์ ์ด๋ป๊ฒ ์๊ฐํ๋์ง ๊ฒฐ๊ณผ๊ฐ ๊ถ๊ธํ๋ค์.
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Registrations { |
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.
๐
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| public class SessionEntity { |
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.
๋๋ฉ์ธ ๊ฐ์ฒด์ db ๋งคํ ๊ด๋ จ ์์ ์ ์ค์ผ ์ํค์ง ์๊ธฐ ์ํด Entity ์ถ๊ฐ ๐
| import nextstep.courses.domain.registration.Registrations; | ||
| import nextstep.courses.infrastructure.entity.RegistrationEntity; | ||
|
|
||
| public class RegistrationMapper { |
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.
๋งค๋ฒ Mapper๋ฅผ ์์ฑํ๋ ๊ฒ์ด ๋ฒ๊ฑฐ๋กญ๋ค๋ฉด https://mapstruct.org/ ์ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ์ ๊ณ ๋ คํด ๋ณด๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ผ ์ ์๊ฒ ๋ค.
| Session session = sessionRepository.findById(payment.getSessionId()); | ||
| session.validateEnroll(payment.getAmount()); | ||
|
|
||
| Registration registration = new Registration(payment.getSessionId(), payment.getNsUserId()); | ||
| registrationRepository.save(registration); |
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 ๊ฐ์ฒด ๋ด์ ๋๋ ๊ฒ์ด ์๋๋ผ ์๋์ ๊ฐ์ด Session์ ํตํด ์๊ฐ์ ์ฒญ์ ํ์ํ ๊ฐ์ฒด๋ฅผ ์๋์ Enrollment์ ๊ฐ์ ๊ฐ์ฒด๋ก ์์ฑํ ํ ์์ํจ์ผ๋ก์จ ๋ณต์ก๋๋ฅผ ๋ฎ์ถ๋ ๋ฐฉ์์ผ๋ก ์ ๊ทผํด ๋ณด๋ ๊ฒ์ ์ด๋จ๊น?
Session session = ...
List<Student> students = ...
Enrollment enrollment = session.enrollment(students);
Student student = enrollment.enroll(user);
enrollmentRepository.save(student.getUserId(), student.getSessionId());์ ์ฝ๋์์ ๋ฆฌํฉํฐ๋ง ํํธ๋ฅผ ์ป์ ์ ์๋ ๋ถ๋ถ์ด ์๋ค๋ฉด ๋ฆฌํฉํฐ๋งํด ๋ณธ๋ค.
์ ์ฝ๋์์ Student๋ ์์ ๋ ๊ตฌํํ ์ฝ๋์ Registration๊ณผ ๊ฐ์
| Session session = sessionRepository.findById(payment.getSessionId()); | ||
| session.validateEnroll(payment.getAmount()); | ||
|
|
||
| Registration registration = new Registration(payment.getSessionId(), payment.getNsUserId()); | ||
| registrationRepository.save(registration); |
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.
Entity ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์๋๋ฐ Repository์์ ์ฌ์ฉํ๋ ์ด์ ๋?
| Session session = sessionRepository.findById(payment.getSessionId()); | |
| session.validateEnroll(payment.getAmount()); | |
| Registration registration = new Registration(payment.getSessionId(), payment.getNsUserId()); | |
| registrationRepository.save(registration); | |
| SessionEntity session = sessionRepository.findById(payment.getSessionId()); | |
| List<Registration> registrations = registrationRepository.findBySessionId(payment.getSessionId()); | |
| Enrollment enrollment = session.enrollment(registrations); | |
| enrollment.enroll(payment); |
์์ ๊ฐ์ด Repository์ ๊ฐ์ ์ ๋ฌํ๊ณ ์กฐํํ ๋ Entity๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ์ด๋ป๊ฒ ์๊ฐํ๋?
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.
(๋ฐ๊ฟ์ผ์ง ํ๊ณ ์์ด๋ฒ๋ ธ์ต๋๋ค)
SessionRepository๋ ๋๋ฉ์ธ ๋ ์ด์ด์ ์์ผ๋ฏ๋ก Session์ผ๋ก ๋ฐ๋๊ฒ ๋ง๋ ๊ฑฐ ๊ฐ๊ณ
๊ตฌํ์ฒด๋ ์ธํ๋ผ ๋ ์ด์ด์ ์์ผ๋ฏ๋ก SessionEntity๋ฅผ ์ฌ์ฉํ๊ฒ ํ๋ ค๊ณ ํฉ๋๋ค
javajigi
left a comment
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.
ํผ๋๋ฐฑ ๋ฐ์ ๋ ์ ํ๋ค์. ๐ฏ
์ถ๊ฐ๋ก ํ๋ฒ ๊ณ ๋ฏผํด ๋ดค์ผ๋ฉด ํ๋ ๋ถ๋ถ์ด ์์ด ํผ๋๋ฐฑ ๋จ๊ฒผ์ผ๋ ์ด๋ฒ ๊ธฐํ์ ํ๋ฒ ๊ณ ๋ฏผํด ๋ณด๋ฉด ์ข๊ฒ ์ต๋๋ค.
| } | ||
|
|
||
| public Enrollment(SessionState state, SessionType type) { | ||
| private final Session session; |
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์ผ๋ก ์์กด ๊ด๊ณ๋ฅผ ๊ฐ์ง๋ค๋ณด๋ Enrollment ์์ฑ์๋ ์ด๋ ค์์ด ์์ ๊ฒ ๊ฐ๋ค.
์์กด ๊ด๊ณ๋ฅผ ๋๊ณ , 'long sessionId'๋ง ์ ๋ฌํ๋ ๊ฒ์ ์ด๋จ๊น?
๊ฐ์ฒด์ ์์กด ๊ด๊ณ๋ฅผ ๋ฌด์กฐ๊ฑด์ผ๋ก ๊ฐ์ ธ๊ฐ๋ ๊ฒ์ด ์ข์ ๊ฒ์ ์๋๊ธฐ ๋๋ฌธ์ ์ด๋ฒ ๊ธฐํ์ ์ด๋ ์์ ์ ์์กด ๊ด๊ณ๋ฅผ ๋๋ ๊ฒ์ด ์ข์์ง ๊ณ ๋ฏผํด ๋ณด๋ ์ฐ์ต๋ ํด๋ณด๋ฉด ์ข๊ฒ ๋ค.
๋๋ฉ์ธ ๋ชจ๋ธ:
Session (1) โโโโ (N) Registration
ํ ์ด๋ธ ์ค๊ณ:
session (id, course_id, term, ...)
registration (id, session_id, student_id, enrolled_at) -- FK๋ก session_id ์ฐธ์กฐ
๊ณ ๋ฏผํ ๋ถ๋ถ:
Registrations registrations = findRegistrationsBySessionId(id, entity.getMaxCapacity());
๋๋ฉ์ธ ๋ชจ๋ธ:
Session (1) โโโโ (1) SessionCoverImage
ํ ์ด๋ธ ์ค๊ณ:
session (id, ...) -- ์ด๋ฏธ์ง ์ ๋ณด๋ฅผ ํฌํจํ์ง ์์
session_cover_image (id, session_id, width, height, extension, capacity) -- FK๋ก session_id ์ฐธ์กฐ
๊ณ ๋ฏผํ ๋ถ๋ถ:
SessionCoverImage coverImage = findCoverImageBySessionId(id);
return SessionMapper.toDomain(entity, registrations, coverImage);
SessionType (FreeType/PaidType) ์ฒ๋ฆฌ:
session (
type varchar(10), -- 'FREE' or 'PAID'
max_capacity int, -- PaidType์ maxCapacity
tuition_fee bigint -- PaidType์ tuitionFee
)
Mapper์์ ๋ณต์:
if ("FREE".equals(entity.getType())) {
type = new FreeType(registrations);
} else {
type = new PaidType(entity.getTuitionFee(), registrations);
}
๊ตฌ์กฐ:
Domain Object โโ Entity โโ DB
โ โ
โโโ Mapper โโโ
ํต์ฌ ์ค๊ณ ๊ฒฐ์ ์์ฝ