-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ADPRO-C11/basic-crud
Basic crud
- Loading branch information
Showing
15 changed files
with
1,042 additions
and
114 deletions.
There are no files selected for viewing
128 changes: 122 additions & 6 deletions
128
src/main/java/snackscription/review/controller/ReviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/snackscription/review/exception/ControllerAdvisor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/snackscription/review/exception/InvalidStateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package snackscription.review.exception; | ||
|
||
public class InvalidStateException extends Exception { | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/snackscription/review/exception/ReviewNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package snackscription.review.exception; | ||
|
||
public class ReviewNotFoundException extends Exception { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 44 additions & 23 deletions
67
src/main/java/snackscription/review/model/ReviewState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/snackscription/review/model/ReviewStateApproved.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/main/java/snackscription/review/model/ReviewStatePending.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/main/java/snackscription/review/model/ReviewStateRejected.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.