Skip to content

Commit

Permalink
Fix : #945
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 2, 2023
1 parent 2b42c5e commit 22b7545
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.mongoes.document.Grades;
import com.example.mongoes.document.Restaurant;
import com.example.mongoes.response.GenericMessage;
import com.example.mongoes.web.model.RestaurantRequest;
import com.example.mongoes.web.service.RestaurantService;
import io.micrometer.core.annotation.Timed;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -76,17 +77,19 @@ public Mono<ResponseEntity<Restaurant>> addNotesToRestaurant(
}

@PostMapping("/restaurant")
public ResponseEntity<Object> createRestaurant(@RequestBody Restaurant restaurant) {
public ResponseEntity<GenericMessage> createRestaurant(
@RequestBody RestaurantRequest restaurantRequest) {
return ResponseEntity.created(
URI.create(
String.format(
"/restaurant/%s",
this.restaurantService
.createRestaurant(restaurant)
.createRestaurant(restaurantRequest)
.map(Restaurant::getName))))
.body(
new GenericMessage(
String.format(
"restaurant with name %s created", restaurant.getName())));
"restaurant with name %s created",
restaurantRequest.name())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.mongoes.web.model;

import com.example.mongoes.document.Address;
import com.example.mongoes.document.Grades;
import com.example.mongoes.document.Restaurant;
import java.util.List;

public record RestaurantRequest(
Long restaurantId,
String name,
String borough,
String cuisine,
Address address,
List<Grades> grades) {

public Restaurant toRestaurant() {
Restaurant restaurant = new Restaurant();
restaurant.setAddress(address);
restaurant.setBorough(borough);
restaurant.setCuisine(cuisine);
restaurant.setGrades(grades);
restaurant.setName(name);
restaurant.setRestaurantId(restaurantId);
return restaurant;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.example.mongoes.mongodb.repository.RestaurantRepository;
import com.example.mongoes.utils.AppConstants;
import com.example.mongoes.utils.DateUtility;
import com.example.mongoes.web.model.RestaurantRequest;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import com.mongodb.client.model.changestream.OperationType;
import java.io.IOException;
Expand Down Expand Up @@ -208,8 +209,9 @@ public Mono<SearchPage<Restaurant>> findAllRestaurants(Integer offset, Integer l
return this.restaurantESRepository.findAll(pageable);
}

public Mono<Restaurant> createRestaurant(Restaurant restaurant) {
return save(restaurant);
public Mono<Restaurant> createRestaurant(RestaurantRequest restaurantRequest) {

return save(restaurantRequest.toRestaurant());
}

public Mono<Void> deleteAll() {
Expand Down

0 comments on commit 22b7545

Please sign in to comment.