Skip to content
Snippets Groups Projects
Commit cd64edc8 authored by hugcubi's avatar hugcubi
Browse files

Fixed api response

parent 281f51da
No related branches found
No related tags found
2 merge requests!36Develop,!31Dev/refactor hotels booking
package com.uva.monolith.api;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
......@@ -9,6 +7,8 @@ import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.JsonNode;
@Component
public class ManagerAPI {
......@@ -21,8 +21,9 @@ public class ManagerAPI {
public Boolean existsManagerById(int id) {
try {
String url = MANAGERS_API_URL + "/{id}";
return restTemplate.getForObject(url, Map.class, id).containsKey("id");
return restTemplate.getForEntity(url, JsonNode.class, id).getStatusCode() == HttpStatus.OK;
} catch (HttpClientErrorException e) {
e.printStackTrace(System.err);
if (e.getStatusCode() != HttpStatus.NOT_FOUND)
throw e;
return false;
......
......@@ -62,13 +62,19 @@ public class HotelController {
// Añadir un hotel con sus habitaciones
@PostMapping
public ResponseEntity<?> addHotel(@RequestBody Hotel hotel) {
boolean exist = managerAPI.existsManagerById(hotel.getManagerId());
if (!exist) {
return new ResponseEntity<>(
"No existe el manager con id " + String.valueOf(hotel.getManagerId()), HttpStatus.BAD_REQUEST);
try {
boolean exist = managerAPI.existsManagerById(hotel.getManagerId());
String message = "No existe el manager con id " + String.valueOf(hotel.getManagerId());
if (!exist) {
return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);
}
Hotel savedHotel = hotelRepository.save(hotel);
return new ResponseEntity<>(savedHotel, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace(System.err);
throw e;
}
Hotel savedHotel = hotelRepository.save(hotel);
return new ResponseEntity<>(savedHotel, HttpStatus.CREATED);
}
// Obtener un hotel por su ID
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment