Skip to content

Commit

Permalink
Merge pull request #17 from ADPRO-C11/basic-crud
Browse files Browse the repository at this point in the history
Basic crud
  • Loading branch information
asteriskzie authored May 26, 2024
2 parents 78a3a29 + 854e80e commit afb8d20
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 215 deletions.
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

import snackscription.review.model.Review;
import snackscription.review.service.ReviewService;
import snackscription.review.service.ReviewServiceImpl;

@CrossOrigin
@RestController
@RequestMapping("/reviews")
@RequestMapping("")
public class ReviewController {
private ReviewService reviewService;

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

@PostMapping("/subscription-boxes/{subsbox}")
public ResponseEntity<Review> createSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
@PostMapping("/subscription-boxes/{subsbox}/users/self")
public ResponseEntity<Review> createSelfSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
String author = body.get(BODY_AUTHOR);
int rating = Integer.parseInt(body.get(BODY_RATING));
Expand All @@ -42,7 +44,7 @@ public ResponseEntity<Review> createSubsboxReview(@RequestBody Map<String,String
}
}

@GetMapping("/subscription-boxes/{subsbox}/public")
@GetMapping("/subscription-boxes/{subsbox}")
public ResponseEntity<List<Review>> getPublicSubsboxReview(@PathVariable String subsbox) {
try {
List<Review> reviews = reviewService.getSubsboxReview(subsbox, "APPROVED");
Expand All @@ -52,74 +54,39 @@ public ResponseEntity<List<Review>> getPublicSubsboxReview(@PathVariable String
}
}

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

@PutMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> editReview(@RequestBody Map<String,String> body, @PathVariable String subsbox, @PathVariable String user) {
@PutMapping("/subscription-boxes/{subsbox}/users/self")
public ResponseEntity<Review> editSelfReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
String sender = body.get(BODY_AUTHOR);
if (!authenticate(sender, user)) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}

int rating = Integer.parseInt(body.get(BODY_RATING));
String content = body.get(BODY_CONTENT);
String author = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
int rating = Integer.parseInt(body.get("rating"));
String content = body.get("content");

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

private boolean authenticate(String sender, String user) {
return true;
}

@DeleteMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> deleteReview(@PathVariable String subsbox, @PathVariable String user) {
@DeleteMapping("/subscription-boxes/{subsbox}/users/self")
public ResponseEntity<Review> deleteSelfReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
reviewService.deleteReview(subsbox, user);
String author = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
reviewService.deleteReview(subsbox, author);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

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

@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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

import io.micrometer.common.lang.Nullable;
import snackscription.review.model.Review;
import snackscription.review.model.ReviewId;
import snackscription.review.model.ReviewState;

public interface ReviewRepository extends JpaRepository<Review, ReviewId> {
@Nullable
List<Review> findByIdSubsbox(String subsbox);

@Nullable
List<Review> findByIdAuthor(String author);

@Nullable
List<Review> findByIdSubsboxAndState(String subsbox, ReviewState state);

@Nullable
Review findByIdSubsboxAndIdAuthor(String subsbox, String author);

void deleteByIdSubsboxAndIdAuthor(String subsbox, String author);
}
93 changes: 10 additions & 83 deletions src/main/java/snackscription/review/service/ReviewService.java
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;
}
Loading

0 comments on commit afb8d20

Please sign in to comment.