-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReviewRestController.java
140 lines (125 loc) · 6.61 KB
/
ReviewRestController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package app.controller.restController;
import java.net.URI;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import app.model.Game;
import app.model.User;
import app.model.Review;
import app.service.ReviewService;
import app.service.UserService;
import app.service.GameService;
@RestController
@RequestMapping("/api/reviews")
public class ReviewRestController {
@Autowired
private GameService gameService;
@Autowired
private ReviewService reviewService;
@Autowired
private UserService userService;
@Operation(summary = "Get more reviews of a game")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "More reviews", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Review.class)) }),
@ApiResponse(responseCode = "404", description = "Game not found", content = @Content) })
@GetMapping("/more/{id}/{page}")
public Page<Review> getMoreReviews(@Parameter(description = "Page number") @PathVariable int page,
@Parameter(description = "Game id") @PathVariable long id) {
// Before returning a page it confirms that there are more left
Game game = gameService.findById(id).orElseThrow();
if (page <= (int) Math.ceil(reviewService.countByGame(game) / 6)) {
return reviewService.findByGame(game, PageRequest.of(page, 6));
}
return null;
}
@Operation(summary = "Add a review to a game")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Review added", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Review.class)) }),
@ApiResponse(responseCode = "404", description = "Game not found", content = @Content),
@ApiResponse(responseCode = "403", description = "forbiden o dont have permissions", content = @Content) })
@PostMapping("/{gameId}/{userId}")
public ResponseEntity<Review> addReview(@Parameter(description = "Id of the game") @PathVariable long gameId,
@Parameter(description = "Id of the user") @PathVariable long userId,
HttpServletRequest request, String comment, int reviewRate) {
try {
User user = userService.findByMail(request.getUserPrincipal().getName()).orElseThrow();
ResponseEntity<Review> addReview = reviewService.addReview(gameId, user, userId, comment, reviewRate);
if (addReview.getStatusCode() == HttpStatus.CREATED) {
Review review = addReview.getBody();
Game game = review.getGame();
int index = fromCurrentRequest().toUriString().indexOf("/api/reviews");
String value = fromCurrentRequest().toUriString().substring(0, index) + "/api/reviews/"
+ game.getReviews().get(game.getReviews().size() - 1).getId();
URI location = URI.create(value);
return ResponseEntity.created(location).body(review);
} else {
return addReview;
}
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
@Operation(summary = "Delete a review")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Review deleted", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Review.class)) }),
@ApiResponse(responseCode = "404", description = "Review not found", content = @Content),
@ApiResponse(responseCode = "403", description = "forbiden o dont have permissions", content = @Content) })
@DeleteMapping("/{reviewId}")
public ResponseEntity<Review> deleteReview(@Parameter(description = "Id of the review") @PathVariable long reviewId,
HttpServletRequest request) {
try {
User user = userService.findByMail(request.getUserPrincipal().getName()).orElseThrow();
return reviewService.deleteReview(reviewId, user);
} catch (Exception e) {
return null;
}
}
@Operation(summary = "Get a review")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Review", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Review.class)) }),
@ApiResponse(responseCode = "404", description = "Review not found", content = @Content) })
@GetMapping("/{id}")
public ResponseEntity<Review> getReview(@Parameter(description = "Id of the user") @PathVariable long id) {
// Before returning a page it confirms that there are more left
Optional<Review> opReview = reviewService.findById(id);
if (opReview.isPresent()) {
return new ResponseEntity<>(opReview.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/review/{userId}/{gameId}")
public ResponseEntity<Boolean> getReview(@PathVariable long userId, @PathVariable long gameId) {
// Before returning a page it confirms that there are more left
Optional<User> opUser = userService.findById(userId);
Optional<Game> opGame = gameService.findById(gameId);
try{
Boolean opReview = reviewService.reviewedByUser(opUser.get(), opGame.get());
return new ResponseEntity<>(opReview, HttpStatus.OK);
}catch (Exception e){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}