Skip to content

Commit

Permalink
Merge pull request #7 from ADPRO-C11/basic-crud
Browse files Browse the repository at this point in the history
Basic crud
  • Loading branch information
asteriskzie authored May 1, 2024
2 parents 6ec68aa + a9ea2c1 commit fb3c52e
Show file tree
Hide file tree
Showing 15 changed files with 1,042 additions and 114 deletions.
128 changes: 122 additions & 6 deletions src/main/java/snackscription/review/controller/ReviewController.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,146 @@
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 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("/")
public class ReviewController {

private final ReviewRepository reviewRepository;

public ReviewController(ReviewRepository reviewRepository) {
this.reviewRepository = reviewRepository;
private ReviewService reviewService;

public ReviewController(ReviewService reviewService) {
this.reviewService = reviewService;
}

@GetMapping("")
public ResponseEntity<String> reviewPage() {
return ResponseEntity.ok().body("Welcome to the review service!");
}

@GetMapping("/all")
public Iterable<Review> findAllReview() {
return this.reviewRepository.findAll();
@PostMapping("/api/subscription-boxes/{subscriptionBoxId}")
public ResponseEntity<Review> createSubscriptionBoxReview(@RequestBody Map<String,String> body, @PathVariable String subscriptionBoxId) {

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

Review review = reviewService.createReview(rating, content, subscriptionBoxId, userId);
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) {
try {
List<Review> reviews = reviewService.getAllSubscriptionBoxReview(subscriptionBoxId, "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) {
try {
String userId = body.get("userId");
Review review = reviewService.getReview(subscriptionBoxId, userId);
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) {
try {
String userId = body.get("userId");
int rating = Integer.parseInt(body.get("rating"));
String content = body.get("content");

Review review = reviewService.editReview(rating, content, subscriptionBoxId, userId);
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);
}
}

@DeleteMapping("/api/subscription-boxes/{subscriptionBoxId}/users/{userId}")
public ResponseEntity<Review> deleteSubscriptionBoxReview(@PathVariable String subscriptionBoxId, @PathVariable String userId) {
try {
reviewService.deleteReview(subscriptionBoxId, userId);
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);
}

@PutMapping("/api/reviews/{reviewId}/approve")
public ResponseEntity<Review> approveReview(@PathVariable String reviewId) {
try {
Review review = reviewService.approveReview(reviewId);
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) {
try {
Review review = reviewService.rejectReview(reviewId);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package snackscription.review.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import snackscription.review.model.Review;

@ControllerAdvice
public class ControllerAdvisor extends ResponseEntityExceptionHandler {
@ExceptionHandler(ReviewNotFoundException.class)
public ResponseEntity<Review> handleReviewNotFound(ReviewNotFoundException exc, WebRequest req) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@ExceptionHandler(InvalidStateException.class)
public ResponseEntity<Review> handleInvalidState(InvalidStateException exc, WebRequest req) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package snackscription.review.exception;

public class InvalidStateException extends Exception {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package snackscription.review.exception;

public class ReviewNotFoundException extends Exception {

}
18 changes: 11 additions & 7 deletions src/main/java/snackscription/review/model/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


@Getter
@Setter
@Entity
@Table(name = "review")
public class Review {
Expand All @@ -17,14 +18,10 @@ public class Review {
@Column(name = "rating", nullable = false)
private int rating;

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

// @Transient
@Setter
@ManyToOne
@JoinColumn(name = "state_id", nullable = false)
@Column(name = "state", nullable = false)
private ReviewState state;

@Column(name="user_id", nullable = false)
Expand All @@ -40,12 +37,11 @@ public Review(int rating, String content, String userId, String subscriptionBoxI
this.id = UUID.randomUUID().toString();
this.rating = rating;
this.content = content;
this.state = new ReviewStatePending(this);
this.state = ReviewState.PENDING;
this.userId = userId;
this.subscriptionBoxId = subscriptionBoxId;
}


public void editReview(int rating, String content) {
this.setRating(rating);
this.setContent(content);
Expand All @@ -57,4 +53,12 @@ public void setRating(int rating) {
}
this.rating = rating;
}

public void approve() {
this.state.approve(this);
}

public void reject() {
this.state.reject(this);
}
}
67 changes: 44 additions & 23 deletions src/main/java/snackscription/review/model/ReviewState.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
package snackscription.review.model;

import jakarta.persistence.*;


@Entity
@Table(name = "review_state")
public abstract class ReviewState {
public enum ReviewState {
PENDING(new PendingState()),
APPROVED(new ApprovedState()),
REJECTED(new RejectedState());
private final StateTransition state;
private ReviewState(StateTransition state) {
this.state = state;
}

@Transient
Review review;
void approve(Review review) {
state.approve(review);
}
void reject(Review review) {
state.reject(review);
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
private interface StateTransition {
void approve(Review review);
void reject(Review review);
}

@Column(nullable = false)
String name;
private static class PendingState implements StateTransition {
@Override
public void approve(Review review) {
review.setState(APPROVED);
}

ReviewState(Review review) {
this.review = review;
@Override
public void reject(Review review) {
review.setState(REJECTED);
}
}

public ReviewState() {
private static class ApprovedState implements StateTransition {
@Override
public void approve(Review review) {
throw new RuntimeException("Review already approved.");
}

@Override public void reject(Review review) {
review.setState(REJECTED);
}
}

public abstract void approve();

public abstract void reject();
private static class RejectedState implements StateTransition {
@Override
public void approve(Review review) {
review.setState(APPROVED);
}

@Override
public String toString() {
return this.name;
@Override public void reject(Review review) {
throw new RuntimeException("Review already rejected.");
}
}
}
}
19 changes: 0 additions & 19 deletions src/main/java/snackscription/review/model/ReviewStateApproved.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/snackscription/review/model/ReviewStatePending.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/snackscription/review/model/ReviewStateRejected.java

This file was deleted.

Loading

0 comments on commit fb3c52e

Please sign in to comment.