Restaurant API using Spring Boot
@Autowired
private RestaurantService restaurantService;
//to create a restaurant
@PostMapping("/restaurant/add")
public ResponseEntity<Object> postRestaurant(@RequestBody Restaurant restaurant){
return restaurantService.postRestaurant(restaurant);
}
//to get a restaurant by id
@GetMapping("/restaurant/get/{id}")
public ResponseEntity<Object>getRestaurantById(@PathVariable int id){
return restaurantService.getRestaurantById(id);
}
//to get restaurants which are equal to or greater than the given rating
@GetMapping("/restaurant/ratings")
public ResponseEntity<Stream<Restaurant>> getGreaterRating(@RequestParam("rating") Double rating){
return restaurantService.getGreaterRating(rating);
}
//to get the Sorted Restaurant with the given param value
@GetMapping("/restaurant/sortBy")
public ResponseEntity<Object> getRestaurantBySort(@RequestParam("value") String value){
return restaurantService.getRestaurantBySort(value);
}
//to update the restaurant's vote and rating by id as PathVariable and restaurant Object
@PutMapping("/restaurant/{id}")
public ResponseEntity<Object> updateRestaurant(@PathVariable int id, @RequestBody Restaurant restaurant){
return restaurantService.updateRestaurant(id, restaurant);
}
// to delete the restaurant by using id as PathVariable
@DeleteMapping("/restaurant/{id}")
public ResponseEntity<Object> deleteRestaurantById(@PathVariable int id){
return restaurantService.deleteRestaurantById(id);
}
// manually seed the h2 database
@GetMapping("/restaurant/seed")
public ResponseEntity<Object> getResturantSeed(){
return restaurantService.seedRestaurant();
}
@GetMapping("/restaurant/getall")
public ResponseEntity<Object> getAllResturant(){
return restaurantService.allRestaurant();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true)
private int id;
private String name;
private String city;
private int estimatedCost;
private double averageRating;
private int votes;
private long contactNo;
public interface RestaurantRepository extends JpaRepository<Restaurant,Integer> {
List<Restaurant> findByAverageRatingGreaterThanEqual(double rating);
@Query(value = "SELECT max(id) FROM Restaurant")
public int getMaxId();
}
private static final Logger log = Logger.getLogger(RestaurantController.class.getName());
/*
* Implement the business logic for the RestaurantService operations in this
* class Make sure to add required annotations
*/
@Autowired
private RestaurantRepository restaurantRepository;
// to create a restaurant
// created->201
// badRequest()->400
public ResponseEntity<Object> postRestaurant(Restaurant restaurant) {
if (restaurant == null) {
return ResponseEntity.badRequest().build();
}
log.info(restaurant.toString());
restaurantRepository.save(restaurant);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
// to get a restaurant by id
// ok()->200
// badRequest()->400
public ResponseEntity<Object> getRestaurantById(int id) {
Restaurant restaurant = restaurantRepository.findById(id).orElse(null);
if (restaurant == null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(restaurant);
}
// to get restaurants which are equal to or greater than the given rating
// ok()->200
// badRequest()->400
public ResponseEntity<Stream<Restaurant>> getGreaterRating(Double rating) {
List<Restaurant> matchingRestaurants = restaurantRepository.findByAverageRatingGreaterThanEqual(rating);
if (matchingRestaurants.isEmpty()) {
return ResponseEntity.badRequest().build();
}
Stream<Restaurant> restaurantStream = matchingRestaurants.stream();
return ResponseEntity.ok().body(restaurantStream);
}
// to get the Sorted Restaurant with the given param value
// ok()->200
// badRequest()->400
public ResponseEntity<Object> getRestaurantBySort(String value) {
List<Restaurant> restaurants = restaurantRepository.findAll();
if(restaurants.isEmpty()) {
return ResponseEntity.badRequest().build();
}
switch (value) {
case "id":
restaurants.sort(Comparator.comparing(Restaurant::getId));
break;
case "name":
restaurants.sort(Comparator.comparing(Restaurant::getName));
break;
case "city":
restaurants.sort(Comparator.comparing(Restaurant::getCity));
break;
case "estimatedCost":
restaurants.sort(Comparator.comparingInt(Restaurant::getEstimatedCost));
break;
case "averageRating":
restaurants.sort(Comparator.comparingDouble(Restaurant::getAverageRating));
break;
case "votes":
restaurants.sort(Comparator.comparingInt(Restaurant::getVotes));
break;
case "contactNo":
restaurants.sort(Comparator.comparingLong(Restaurant::getContactNo));
break;
default:
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(restaurants);
}
// to delete the restaurant by using id as PathVariable
// ok()->200
// badRequest()->400
public ResponseEntity<Object> deleteRestaurantById(int id) {
if (!restaurantRepository.existsById(id)) {
return ResponseEntity.badRequest().build();
}
restaurantRepository.deleteById(id);
return ResponseEntity.ok().build();
}
// to update the restaurant's vote and rating by id as PathVariable and
// restaurant Object
// ok()->200
// badRequest()->400
public ResponseEntity<Object> updateRestaurant(int id, Restaurant restaurant) {
Restaurant fetchedRestaurant = restaurantRepository.findById(id).orElse(null);
if(fetchedRestaurant == null) {
return ResponseEntity.badRequest().build();
}
restaurant.setId(id);
if(!StringUtils.hasText(restaurant.getName())) {
restaurant.setName(fetchedRestaurant.getName());
}
if(!StringUtils.hasText(restaurant.getCity())) {
restaurant.setCity(fetchedRestaurant.getCity());
}
if(restaurant.getEstimatedCost() == 0) {
restaurant.setEstimatedCost(fetchedRestaurant.getEstimatedCost());
}
if(restaurant.getAverageRating() == 0) {
restaurant.setAverageRating(fetchedRestaurant.getAverageRating());
}
if(restaurant.getVotes() == 0) {
restaurant.setVotes(fetchedRestaurant.getVotes());
}
if(restaurant.getContactNo() == 0) {
restaurant.setContactNo(fetchedRestaurant.getContactNo());
}
restaurantRepository.save(restaurant);
return ResponseEntity.ok(restaurant);
}
// done by me to manually seed the h2 database
public ResponseEntity<Object> seedRestaurant(){
postRestaurant(new Restaurant(1, "Accord","Delhi",40000,4.5,98,7547898765L));
postRestaurant(new Restaurant(2, "Plaso", "Chennai", 50000, 4.1, 100, 9854728626L));
postRestaurant(new Restaurant(3, "MassHotel", "Mumbai",20000,3.5,66,8637954894L));
postRestaurant(new Restaurant(4, "GetToGather", "Kerala",10000,2.9,459,6383475894L));
return ResponseEntity.ok().build();
}
public ResponseEntity<Object> allRestaurant(){
List<Restaurant> restaurants = restaurantRepository.findAll();
if(restaurants.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(restaurants);
}
# added below
spring.h2.console.path=/h2
spring.datasource.inititalization-mode=always
# Already present
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=true
Comments
Post a Comment