From b0a3fd1a120afb5c19f2c83b98725ec63acecde6 Mon Sep 17 00:00:00 2001 From: asteriskzie Date: Wed, 24 Apr 2024 16:27:32 +0700 Subject: [PATCH] [REFACTOR] Add Spring Boot JPA and PostgreSQL dependencies, and update Review model and ReviewState classes --- build.gradle.kts | 2 ++ .../snackscription/review/model/Review.java | 26 ++++++++++++++++--- .../review/model/ReviewState.java | 21 ++++++++++++++- .../review/model/ReviewStateApproved.java | 6 +---- .../review/model/ReviewStatePending.java | 6 +---- .../review/model/ReviewStateRejected.java | 7 ++--- .../review/model/ReviewTest.java | 9 ++----- 7 files changed, 51 insertions(+), 26 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2a83950..c18a996 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,10 +23,12 @@ repositories { } dependencies { + implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("org.springframework.boot:spring-boot-starter-web") compileOnly("org.projectlombok:lombok") developmentOnly("org.springframework.boot:spring-boot-devtools") + runtimeOnly("org.postgresql:postgresql") annotationProcessor("org.springframework.boot:spring-boot-configuration-processor") annotationProcessor("org.projectlombok:lombok") testImplementation("org.springframework.boot:spring-boot-starter-test") diff --git a/src/main/java/snackscription/review/model/Review.java b/src/main/java/snackscription/review/model/Review.java index c05c8b9..f8b2dd4 100644 --- a/src/main/java/snackscription/review/model/Review.java +++ b/src/main/java/snackscription/review/model/Review.java @@ -4,17 +4,36 @@ import lombok.Setter; import java.util.UUID; +import jakarta.persistence.*; + @Getter +@Entity +@Table(name = "review") public class Review { - private final String id; + @Id + private String id; + + @Column(name = "rating", nullable = false) private int rating; + @Setter + @Column(name = "content", nullable = false) private String content; + @Setter + @ManyToOne + @JoinColumn(name = "state_id", nullable = false) private ReviewState state; - private final String userId; - private final String subscriptionBoxId; + + @Column(name="user_id", nullable = false) + private String userId; + + @Column(name="subsbox_id", nullable = false) + private String subscriptionBoxId; + + public Review() { + } public Review(int rating, String content, String userId, String subscriptionBoxId) { this.id = UUID.randomUUID().toString(); @@ -25,6 +44,7 @@ public Review(int rating, String content, String userId, String subscriptionBoxI this.subscriptionBoxId = subscriptionBoxId; } + public void editReview(int rating, String content) { this.setRating(rating); this.setContent(content); diff --git a/src/main/java/snackscription/review/model/ReviewState.java b/src/main/java/snackscription/review/model/ReviewState.java index b6d5daa..554491d 100644 --- a/src/main/java/snackscription/review/model/ReviewState.java +++ b/src/main/java/snackscription/review/model/ReviewState.java @@ -1,8 +1,22 @@ package snackscription.review.model; +import jakarta.persistence.*; + + +@Entity +@Table(name = "review_state") public abstract class ReviewState { + + @Transient Review review; - String state; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + Long id; + + @Column(nullable = false) + String name; + ReviewState(Review review) { this.review = review; } @@ -10,4 +24,9 @@ public abstract class ReviewState { public abstract void approve(); public abstract void reject(); + + @Override + public String toString() { + return this.name; + } } \ No newline at end of file diff --git a/src/main/java/snackscription/review/model/ReviewStateApproved.java b/src/main/java/snackscription/review/model/ReviewStateApproved.java index 3875665..43d67ed 100644 --- a/src/main/java/snackscription/review/model/ReviewStateApproved.java +++ b/src/main/java/snackscription/review/model/ReviewStateApproved.java @@ -4,6 +4,7 @@ public class ReviewStateApproved extends ReviewState { ReviewStateApproved(Review review) { super(review); + this.name = "Approved"; } @Override @@ -15,9 +16,4 @@ public void approve() { public void reject() { this.review.setState(new ReviewStateRejected(this.review)); } - - @Override - public String toString() { - return "Approved"; - } } \ No newline at end of file diff --git a/src/main/java/snackscription/review/model/ReviewStatePending.java b/src/main/java/snackscription/review/model/ReviewStatePending.java index 0064775..efcb1f5 100644 --- a/src/main/java/snackscription/review/model/ReviewStatePending.java +++ b/src/main/java/snackscription/review/model/ReviewStatePending.java @@ -4,6 +4,7 @@ public class ReviewStatePending extends ReviewState { ReviewStatePending(Review review) { super(review); + this.name = "Pending"; } @Override @@ -15,9 +16,4 @@ public void approve() { public void reject() { this.review.setState(new ReviewStateRejected(this.review)); } - - @Override - public String toString() { - return "Pending"; - } } \ No newline at end of file diff --git a/src/main/java/snackscription/review/model/ReviewStateRejected.java b/src/main/java/snackscription/review/model/ReviewStateRejected.java index 68df2cd..90fa050 100644 --- a/src/main/java/snackscription/review/model/ReviewStateRejected.java +++ b/src/main/java/snackscription/review/model/ReviewStateRejected.java @@ -1,8 +1,10 @@ package snackscription.review.model; public class ReviewStateRejected extends ReviewState { + ReviewStateRejected(Review review) { super(review); + this.name = "Rejected"; } @Override @@ -14,9 +16,4 @@ public void approve() { public void reject() { throw new RuntimeException("Review already rejected."); } - - @Override - public String toString() { - return "Rejected"; - } } \ No newline at end of file diff --git a/src/test/java/snackscription/review/model/ReviewTest.java b/src/test/java/snackscription/review/model/ReviewTest.java index 74815f8..c68085d 100644 --- a/src/test/java/snackscription/review/model/ReviewTest.java +++ b/src/test/java/snackscription/review/model/ReviewTest.java @@ -10,16 +10,11 @@ public class ReviewTest { @BeforeEach void setUp() { - this.review = new Review( - 5, - "Bagus bgt dah", - "1", - "111" - ); + this.review = new Review(5, "Great product!", "user1", "subsbox1"); } @Test - void testCreateReview() { + void testConstructor() { int rating = 3; String content = "Wow!"; String userId = "33";