Skip to content

Commit

Permalink
Merge pull request #8 from ADPRO-C11/basic-crud
Browse files Browse the repository at this point in the history
Basic crud
  • Loading branch information
asteriskzie authored May 15, 2024
2 parents fb3c52e + a416bbc commit d8e6f42
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 366 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ jobs:
name: Publish Docker Image
runs-on: ubuntu-latest
needs: test
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: snackscription_review
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# * For example: A author cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
Expand Down
101 changes: 39 additions & 62 deletions src/main/java/snackscription/review/controller/ReviewController.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
package snackscription.review.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import snackscription.review.model.Review;
import snackscription.review.repository.ReviewRepository;
import snackscription.review.service.ReviewService;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;




@RestController
@RequestMapping("/")
@RequestMapping("/reviews")
public class ReviewController {


Expand All @@ -43,101 +26,95 @@ public ResponseEntity<String> reviewPage() {
return ResponseEntity.ok().body("Welcome to the review service!");
}

@PostMapping("/api/subscription-boxes/{subscriptionBoxId}")
public ResponseEntity<Review> createSubscriptionBoxReview(@RequestBody Map<String,String> body, @PathVariable String subscriptionBoxId) {

@PostMapping("/subscription-boxes/{subsbox}")
public ResponseEntity<Review> createSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
String userId = body.get("userId");
String author = body.get("author");
int rating = Integer.parseInt(body.get("rating"));
String content = body.get("content");

Review review = reviewService.createReview(rating, content, subscriptionBoxId, userId);
Review review = reviewService.createReview(rating, content, subsbox, author);
return new ResponseEntity<>(review, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/api/subscription-boxes/{subscriptionBoxId}")
public ResponseEntity<List<Review>> getAllPublicSubscriptionBoxReview(@PathVariable String subscriptionBoxId) {
@GetMapping("/subscription-boxes/{subsbox}/public")
public ResponseEntity<List<Review>> getPublicSubsboxReview(@PathVariable String subsbox) {
try {
List<Review> reviews = reviewService.getAllSubscriptionBoxReview(subscriptionBoxId, "APPROVED");
List<Review> reviews = reviewService.getSubsboxReview(subsbox, "APPROVED");
return new ResponseEntity<>(reviews, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/api/subscription-boxes/{subscriptionBoxId}/users/self")
public ResponseEntity<Review> getSelfSubscriptionBoxReview(@RequestBody Map<String,String> body, @PathVariable String subscriptionBoxId) {
@GetMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> getSelfSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox, @PathVariable String user) {
try {
String userId = body.get("userId");
Review review = reviewService.getReview(subscriptionBoxId, userId);
String sender = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
if (!authenticate(sender, user)) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
Review review = reviewService.getReview(subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@PutMapping("/api/subscription-boxes/{subscriptionBoxId}/users/self")
public ResponseEntity<Review> editSelfSubscriptionBoxId(@RequestBody Map<String,String> body, @PathVariable String subscriptionBoxId) {
@PutMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> editReview(@RequestBody Map<String,String> body, @PathVariable String subsbox, @PathVariable String user) {
try {
String userId = body.get("userId");
String sender = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
if (!authenticate(sender, user)) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}

int rating = Integer.parseInt(body.get("rating"));
String content = body.get("content");

Review review = reviewService.editReview(rating, content, subscriptionBoxId, userId);
Review review = reviewService.editReview(rating, content, subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@DeleteMapping("/api/subscription-boxes/{subscriptionBoxId}/users/self")
public ResponseEntity<Review> deleteSelfSubscriptionBoxReview(@RequestBody Map<String,String> body, @PathVariable String subscriptionBoxId) {
try {
String userId = body.get("userId");
reviewService.deleteReview(subscriptionBoxId, userId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
private boolean authenticate(String sender, String user) {
return true;
}

@DeleteMapping("/api/subscription-boxes/{subscriptionBoxId}/users/{userId}")
public ResponseEntity<Review> deleteSubscriptionBoxReview(@PathVariable String subscriptionBoxId, @PathVariable String userId) {
@DeleteMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> deleteReview(@PathVariable String subsbox, @PathVariable String user) {
try {
reviewService.deleteReview(subscriptionBoxId, userId);
reviewService.deleteReview(subsbox, user);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/api/reviews/{subsboxId}")
public List<Review> getBySubscriptionBoxId(@PathVariable String subsboxId) throws Exception {
return reviewService.getAllSubscriptionBoxReview(subsboxId, null);
}

@GetMapping("/api/reviews/{reviewId}")
public Review getById(@PathVariable String reviewId) throws Exception {
return reviewService.findById(reviewId);
@GetMapping("/subscription-boxes/{subsbox}")
public List<Review> getSubsboxReview(@PathVariable String subsbox) throws Exception {
return reviewService.getSubsboxReview(subsbox, null);
}

@PutMapping("/api/reviews/{reviewId}/approve")
public ResponseEntity<Review> approveReview(@PathVariable String reviewId) {
@PutMapping("/subscription-boxes/{subsbox}/users/{user}/approve")
public ResponseEntity<Review> approveReview(@PathVariable String subsbox, @PathVariable String user) {
try {
Review review = reviewService.approveReview(reviewId);
Review review = reviewService.approveReview(subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@PutMapping("/api/reviews/{reviewId}/reject")
public ResponseEntity<Review> rejectReview(@PathVariable String reviewId) {
@PutMapping("/subscription-boxes/{subsbox}/users/{user}/reject")
public ResponseEntity<Review> rejectReview(@PathVariable String subsbox, @PathVariable String user) {
try {
Review review = reviewService.rejectReview(reviewId);
Review review = reviewService.rejectReview(subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/snackscription/review/model/Review.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package snackscription.review.model;

import lombok.Getter;
import lombok.Setter;

import java.util.UUID;
import lombok.Data;
import jakarta.persistence.*;


@Getter
@Setter
@Data
@Entity
@Table(name = "review")
@Table
public class Review {
@Id
private String id;
@EmbeddedId
private ReviewId id;

@Column(name = "rating", nullable = false)
private int rating;
Expand All @@ -24,22 +20,14 @@ public class Review {
@Column(name = "state", nullable = false)
private ReviewState state;

@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();
public Review(int rating, String content, String subsbox, String user) {
this.id = new ReviewId(subsbox, user);
this.rating = rating;
this.content = content;
this.state = ReviewState.PENDING;
this.userId = userId;
this.subscriptionBoxId = subscriptionBoxId;
}

public void editReview(int rating, String content) {
Expand All @@ -61,4 +49,12 @@ public void approve() {
public void reject() {
this.state.reject(this);
}

public String getSubsbox() {
return this.id.getSubsbox();
}

public String getAuthor() {
return this.id.getAuthor();
}
}
25 changes: 25 additions & 0 deletions src/main/java/snackscription/review/model/ReviewId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package snackscription.review.model;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.Data;

import java.io.Serializable;

@Data
@Embeddable
public class ReviewId implements Serializable {
@Column(name = "subsbox", nullable = false)
private String subsbox;

@Column(name = "author", nullable = false)
private String author;

public ReviewId(String subsbox, String author) {
this.subsbox = subsbox;
this.author = author;
}
public ReviewId() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import snackscription.review.model.Review;
import snackscription.review.model.ReviewId;
import snackscription.review.model.ReviewState;

public interface ReviewRepository extends JpaRepository<Review, String> {
List<Review> findBySubscriptionBoxId(String subsboxId);
List<Review> findBySubscriptionBoxIdAndState(String subsboxId, ReviewState state);
Review findBySubscriptionBoxIdAndUserId(String subsboxId, String userId);
void deleteBySubscriptionBoxIdAndUserId(String subsboxId, String userId);
public interface ReviewRepository extends JpaRepository<Review, ReviewId> {
List<Review> findByIdSubsbox(String subsbox);
List<Review> findByIdAuthor(String author);
List<Review> findByIdSubsboxAndState(String subsbox, ReviewState state);
Review findByIdSubsboxAndIdAuthor(String subsbox, String author);
void deleteByIdSubsboxAndIdAuthor(String subsbox, String author);
}
Loading

0 comments on commit d8e6f42

Please sign in to comment.