Skip to content

Commit

Permalink
added search filter
Browse files Browse the repository at this point in the history
  • Loading branch information
harmlessprince committed Jan 26, 2025
1 parent 3c399f6 commit af7a992
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("v1/carts/checkout").authenticated()
.requestMatchers(HttpMethod.GET, "v1/generate/session/id").permitAll()
.requestMatchers(HttpMethod.GET, "v1/categories").permitAll()
.requestMatchers(HttpMethod.GET, "v1/reviews/product/{productId}").permitAll()
.requestMatchers("/error").permitAll()
.requestMatchers("/actuator/**").permitAll()
.anyRequest().authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -20,6 +21,7 @@ public class ReviewController {
private final ReviewMapper reviewMapper;

@GetMapping
@PreAuthorize("hasAnyAuthority('admin', 'superadmin')")
public ResponseEntity<CustomResponse<List<ReviewResourceDTO>>> index(@RequestParam Map<String, Object> queryParams) {
List<ReviewResourceDTO> reviewResourceDTOS = reviewService.findAll();
return ResponseEntity.ok(CustomResponse.sendSuccessResponse(reviewResourceDTOS, "Review submitted"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@ public ReviewResourceDTO fromEntity(Review review) {
}

public ReviewResourceDTO fromEntityWithProduct(Review review) {
User user = review.getUser();

Product product = review.getProduct();
return new ReviewResourceDTO(review.getId(), review.getCreatedAt(), review.getRating(), review.getComment(), new UserSlimResponse(user.getId(), user.getFullName()), new ProductSlimDto(product.getId(), product.getName(), product.getDescription(), product.getImage()));
User user = review.getUser();
ReviewResourceDTO reviewResourceDTO = new ReviewResourceDTO();
reviewResourceDTO.setId(review.getId());
reviewResourceDTO.setCreatedDate(review.getCreatedAt());
reviewResourceDTO.setRating(review.getRating());
reviewResourceDTO.setComment(review.getComment());
if (user != null) {
reviewResourceDTO.setUser(
new UserSlimResponse(user.getId(), user.getFullName())
);
}
if (product != null) {
new ProductSlimDto(product.getId(), product.getName(), product.getDescription(), product.getImage());
}
return reviewResourceDTO;
}

public Review toEntity(CreateReviewRequest reviewRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public interface ReviewRepository extends MongoRepository<Review, String> {

List<Review> findByProductIsNotNull();
Boolean existsByUserIdAndProductId(String userId, String productId);
List<Review> findByProductId(String productId);
List<Review> findByProductIdOrderByCreatedAtAsc(String productId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<ReviewResourceDTO> findAllByProductId(String productId) {
}

public List<ReviewResourceDTO> findAll() {
List<Review> reviews = reviewRepository.findAll();
List<Review> reviews = reviewRepository.findByProductIsNotNull();
return reviews.stream().map(reviewMapper::fromEntityWithProduct).collect(Collectors.toList());
}

Expand Down

0 comments on commit af7a992

Please sign in to comment.