Skip to content

Commit

Permalink
Merge pull request #3 from ADPRO-C11/setup-db
Browse files Browse the repository at this point in the history
Add JPA & SQL Confirugration
  • Loading branch information
asteriskzie authored Apr 24, 2024
2 parents a1a8083 + b0a3fd1 commit 30081f5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/snackscription/review/model/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/snackscription/review/model/ReviewState.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
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;
}

public abstract void approve();

public abstract void reject();

@Override
public String toString() {
return this.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ReviewStateApproved extends ReviewState {

ReviewStateApproved(Review review) {
super(review);
this.name = "Approved";
}

@Override
Expand All @@ -15,9 +16,4 @@ public void approve() {
public void reject() {
this.review.setState(new ReviewStateRejected(this.review));
}

@Override
public String toString() {
return "Approved";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ReviewStatePending extends ReviewState {

ReviewStatePending(Review review) {
super(review);
this.name = "Pending";
}

@Override
Expand All @@ -15,9 +16,4 @@ public void approve() {
public void reject() {
this.review.setState(new ReviewStateRejected(this.review));
}

@Override
public String toString() {
return "Pending";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package snackscription.review.model;

public class ReviewStateRejected extends ReviewState {

ReviewStateRejected(Review review) {
super(review);
this.name = "Rejected";
}

@Override
Expand All @@ -14,9 +16,4 @@ public void approve() {
public void reject() {
throw new RuntimeException("Review already rejected.");
}

@Override
public String toString() {
return "Rejected";
}
}
9 changes: 2 additions & 7 deletions src/test/java/snackscription/review/model/ReviewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 30081f5

Please sign in to comment.