Skip to content

Commit 03942b2

Browse files
author
heewonham
committed
refactor : (review) 상수, 세션 제거/추가 기능, register 시 payment 유효성 session에서처리
1 parent 8ffdca6 commit 03942b2

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

src/main/java/nextstep/courses/domain/Course.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nextstep.courses.domain;
22

33
import java.time.LocalDateTime;
4+
import java.util.ArrayList;
45

56
public class Course {
67
private Long id;
@@ -42,6 +43,20 @@ public LocalDateTime getCreatedAt() {
4243
return createdAt;
4344
}
4445

46+
public void addSession(Session session) {
47+
if (sessions == null) {
48+
sessions = new Sessions(new ArrayList<>());
49+
}
50+
sessions.add(session);
51+
}
52+
53+
public void removeSession(Session session) {
54+
if (sessions == null) {
55+
return;
56+
}
57+
sessions.remove(session);
58+
}
59+
4560
@Override
4661
public String toString() {
4762
return "Course{" +

src/main/java/nextstep/courses/domain/Image.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,36 @@ public class Image {
66
private long sizeInBytes;
77
private int width;
88
private int height;
9+
private static final int IMAGE_SIZE_1MB = 1024 * 1024;
10+
private static final int WIDTH_LIMIT = 300;
11+
private static final int HEIGHT_LIMIT = 200;
12+
private static final double REQUIRED_ASPECT_RATIO = 1.5;
13+
private static final double ASPECT_RATIO_TOLERANCE = 0.01;
914

1015
public Image(String fileName, String contentType, long sizeInBytes, int width, int height) {
16+
validate(contentType, sizeInBytes, width, height);
1117
this.fileName = fileName;
1218
this.contentType = contentType;
1319
this.sizeInBytes = sizeInBytes;
1420
this.width = width;
1521
this.height = height;
16-
validate();
1722
}
1823

19-
private void validate() {
20-
if (sizeInBytes > 1024 * 1024) {
24+
private void validate(String contentType, long sizeInBytes, int width, int height) {
25+
if (sizeInBytes > IMAGE_SIZE_1MB) {
2126
throw new IllegalArgumentException("이미지 크기는 1MB 이하여야 합니다.");
2227
}
2328

2429
if (!contentType.matches("(gif|jpeg|jpg|png|svg)")) {
2530
throw new IllegalArgumentException("허용되지 않는 이미지 형식입니다.");
2631
}
2732

28-
if (width < 300 || height < 200) {
33+
if (width < WIDTH_LIMIT || height < HEIGHT_LIMIT) {
2934
throw new IllegalArgumentException("이미지 크기가 너무 작습니다.");
3035
}
3136

3237
double ratio = (double) width / height;
33-
if (Math.abs(ratio - 1.5) > 0.01) {
38+
if (Math.abs(ratio - REQUIRED_ASPECT_RATIO) > ASPECT_RATIO_TOLERANCE) {
3439
throw new IllegalArgumentException("이미지 비율은 3:2여야 합니다.");
3540
}
3641
}

src/main/java/nextstep/courses/domain/PaidSession.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ protected void validateRegistration(Long studentId, Payment payment) {
2121
if (payment == null) {
2222
throw new IllegalArgumentException("결제 정보가 없습니다.");
2323
}
24-
payment.isValidatePayment(id, studentId, tuitionFee);
24+
if (!this.id.equals(payment.getSessionId())) {
25+
throw new IllegalArgumentException("결제한 강의와 일치하지 않습니다.");
26+
}
27+
if (!studentId.equals(payment.getNsUserId())) {
28+
throw new IllegalArgumentException("결제한 사용자와 일치하지 않습니다.");
29+
}
30+
if (this.tuitionFee.getAmount() != payment.getAmount()) {
31+
throw new IllegalArgumentException("결제 금액과 일치하지 않습니다.");
32+
}
2533
}
2634

2735
@Override

src/main/java/nextstep/courses/domain/Sessions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44

55
public class Sessions {
66
private final List<Session> sessions;
7+
78
public Sessions(List<Session> sessions) {
89
this.sessions = sessions;
910
}
11+
12+
public void add(Session session) {
13+
sessions.add(session);
14+
}
15+
16+
public void remove(Session session) {
17+
sessions.remove(session);
18+
}
1019
}

src/main/java/nextstep/payments/domain/Payment.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ public class Payment {
1212
private Long nsUserId;
1313

1414
// 결제 금액
15-
private Long amount;
15+
private int amount;
1616

1717
private LocalDateTime createdAt;
1818

1919
public Payment() {
2020
}
2121

22-
public Payment(String id, Long sessionId, Long nsUserId, Long amount) {
22+
public Payment(String id, Long sessionId, Long nsUserId, int amount) {
2323
this.id = id;
2424
this.sessionId = sessionId;
2525
this.nsUserId = nsUserId;
2626
this.amount = amount;
2727
this.createdAt = LocalDateTime.now();
2828
}
2929

30-
public void isValidatePayment(Long sessionId, Long nsUserId, Long amount) {
31-
if (this.sessionId != sessionId) {
32-
throw new IllegalArgumentException("결제한 강의와 일치하지 않습니다.");
33-
}
34-
if (this.nsUserId != nsUserId) {
35-
throw new IllegalArgumentException("결제한 사용자와 일치하지 않습니다.");
36-
}
37-
if (this.amount != amount) {
38-
throw new IllegalArgumentException("결제 금액과 일치하지 않습니다.");
39-
}
30+
public Long getSessionId() {
31+
return sessionId;
32+
}
33+
34+
public Long getNsUserId() {
35+
return nsUserId;
36+
}
37+
38+
public int getAmount() {
39+
return amount;
4040
}
4141
}

0 commit comments

Comments
 (0)