-
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 #24 from ADPRO-C11/staging
Staging
- Loading branch information
Showing
8 changed files
with
460 additions
and
215 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/snackscription/review/controller/ReviewAdminController.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,49 @@ | ||
package snackscription.review.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import snackscription.review.model.Review; | ||
import snackscription.review.service.ReviewServiceImpl; | ||
|
||
import java.util.List; | ||
|
||
@CrossOrigin | ||
@RestController | ||
@RequestMapping("/admin") | ||
public class ReviewAdminController { | ||
private ReviewServiceImpl reviewService; | ||
|
||
public ReviewAdminController(ReviewServiceImpl reviewService) { | ||
this.reviewService = reviewService; | ||
} | ||
|
||
@PutMapping("/subscription-boxes/{subsbox}/users/{user}/approve") | ||
public ResponseEntity<Review> approveReview(@PathVariable String subsbox, @PathVariable String user) { | ||
try { | ||
Review review = reviewService.approveReview(subsbox, user); | ||
return new ResponseEntity<>(review, HttpStatus.OK); | ||
} catch (Exception e) { | ||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
|
||
@PutMapping("/subscription-boxes/{subsbox}/users/{user}/reject") | ||
public ResponseEntity<Review> rejectReview(@PathVariable String subsbox, @PathVariable String user) { | ||
try { | ||
Review review = reviewService.rejectReview(subsbox, user); | ||
return new ResponseEntity<>(review, HttpStatus.OK); | ||
} catch (Exception e) { | ||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
|
||
@GetMapping("/subscription-boxes/{subsbox}/reviews") | ||
public ResponseEntity<List<Review>> getSubsboxReviews( | ||
@PathVariable String subsbox, | ||
@RequestParam(required = false) String state) throws Exception { | ||
List<Review> reviews = reviewService.getSubsboxReview(subsbox, state); | ||
return ResponseEntity.ok().body(reviews); | ||
} | ||
|
||
} |
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
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
93 changes: 10 additions & 83 deletions
93
src/main/java/snackscription/review/service/ReviewService.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,88 +1,15 @@ | ||
package snackscription.review.service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.stereotype.Service; | ||
|
||
import snackscription.review.exception.InvalidStateException; | ||
import snackscription.review.exception.ReviewNotFoundException; | ||
import snackscription.review.model.Review; | ||
import snackscription.review.model.ReviewId; | ||
import snackscription.review.model.ReviewState; | ||
import snackscription.review.repository.ReviewRepository; | ||
|
||
@Service | ||
@Component | ||
public class ReviewService { | ||
private ReviewRepository reviewRepository; | ||
|
||
public ReviewService (ReviewRepository reviewRepository) { | ||
this.reviewRepository = reviewRepository; | ||
} | ||
|
||
public Review createReview(int rating, String content, String subscriptionBoxId, String userId) throws Exception { | ||
Review review = new Review(rating, content, subscriptionBoxId, userId); | ||
reviewRepository.save(review); | ||
return review; | ||
} | ||
|
||
public Review getReview(String subsbox, String user) throws Exception { | ||
Optional<Review> oreview = reviewRepository.findById(new ReviewId(user, subsbox)); | ||
if (oreview.isEmpty()) { | ||
throw new ReviewNotFoundException(); | ||
} | ||
return oreview.get(); | ||
} | ||
|
||
public List<Review> getSubsboxReview(String subscriptionBoxId, String state) throws Exception { | ||
if (state == null) { | ||
return reviewRepository.findByIdSubsbox(subscriptionBoxId); | ||
} else { | ||
state = state.toUpperCase(); | ||
ReviewState reviewState = Enum.valueOf(ReviewState.class, state); | ||
if (reviewState == null) { | ||
throw new InvalidStateException(); | ||
} | ||
return reviewRepository.findByIdSubsboxAndState(subscriptionBoxId, reviewState); | ||
} | ||
} | ||
|
||
public Review editReview(int rating, String content, String subscriptionBoxId, String userId) throws Exception { | ||
Review review = reviewRepository.findByIdSubsboxAndIdAuthor(subscriptionBoxId, userId); | ||
|
||
if (review == null) { | ||
throw new ReviewNotFoundException(); | ||
} | ||
|
||
review.setRating(rating); | ||
review.setContent(content); | ||
|
||
return reviewRepository.save(review); | ||
} | ||
|
||
public Review approveReview(String subsbox, String user) throws Exception { | ||
Review review = getReview(subsbox, user); | ||
review.approve(); | ||
return reviewRepository.save(review); | ||
} | ||
|
||
public Review rejectReview(String subsbox, String user) throws Exception { | ||
Review review = getReview(subsbox, user); | ||
review.reject(); | ||
return reviewRepository.save(review); | ||
} | ||
|
||
public void deleteReview(String subsbox, String user) throws Exception { | ||
Review review = reviewRepository.findByIdSubsboxAndIdAuthor(subsbox, user); | ||
|
||
if (review == null) { | ||
throw new ReviewNotFoundException(); | ||
} | ||
import java.util.List; | ||
|
||
reviewRepository.delete(review); | ||
} | ||
public interface ReviewService { | ||
public boolean reviewExist(String subsbox, String user); | ||
public Review createReview(int rating, String content, String subscriptionBoxId, String userId) throws Exception; | ||
public Review getReview(String subsbox, String user) throws Exception; | ||
public List<Review> getSubsboxReview(String subscriptionBoxId, String state) throws Exception; | ||
public Review editReview(int rating, String content, String subscriptionBoxId, String userId) throws Exception; | ||
public void deleteReview(String subsbox, String user) throws Exception; | ||
public Review approveReview(String subsbox, String user) throws Exception; | ||
public Review rejectReview(String subsbox, String user) throws Exception; | ||
} |
Oops, something went wrong.