1 | package com.example.skychasemk.controller;
|
---|
2 |
|
---|
3 | import com.example.skychasemk.dto.ReviewDTO;
|
---|
4 | import com.example.skychasemk.model.Review;
|
---|
5 | import com.example.skychasemk.repository.ReviewRepository;
|
---|
6 | import com.example.skychasemk.services.ReviewService;
|
---|
7 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
8 | import org.springframework.http.ResponseEntity;
|
---|
9 | import org.springframework.web.bind.annotation.*;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Optional;
|
---|
13 |
|
---|
14 | @RestController
|
---|
15 | @RequestMapping("/api/reviews")
|
---|
16 | public class ReviewController {
|
---|
17 |
|
---|
18 | @Autowired
|
---|
19 | private ReviewService reviewService;
|
---|
20 | @Autowired
|
---|
21 | private ReviewRepository reviewRepository;
|
---|
22 |
|
---|
23 | // Get all reviews
|
---|
24 | @GetMapping
|
---|
25 | public List<Review> getAllReviews() {
|
---|
26 | return reviewService.getAllReviews();
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | @GetMapping("/{flightId}")
|
---|
31 | public List<Review> getReviewsByFlightId(@PathVariable("flightId") Integer flightId) {
|
---|
32 | return reviewRepository.findReviews(flightId);
|
---|
33 | }
|
---|
34 |
|
---|
35 | // Get review by ID
|
---|
36 | @GetMapping("/{id}")
|
---|
37 | public Optional<Review> getReviewById(@PathVariable("id") Integer reviewID) {
|
---|
38 | return reviewService.getReviewById(reviewID);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @PostMapping
|
---|
42 | public ResponseEntity<Review> submitReview(@RequestBody ReviewDTO dto) {
|
---|
43 | Review savedReview = reviewService.submitReview(dto);
|
---|
44 | return ResponseEntity.ok(savedReview);
|
---|
45 | }
|
---|
46 |
|
---|
47 | // Update an existing review
|
---|
48 | @PutMapping("/{id}")
|
---|
49 | public Review updateReview(@PathVariable("id") Integer reviewID, @RequestBody Review review) {
|
---|
50 | return reviewService.updateReview(reviewID, review);
|
---|
51 | }
|
---|
52 |
|
---|
53 | // Delete a review
|
---|
54 | @DeleteMapping("/{id}")
|
---|
55 | public void deleteReview(@PathVariable("id") Integer reviewID) {
|
---|
56 | reviewService.deleteReview(reviewID);
|
---|
57 | }
|
---|
58 | }
|
---|