Implemented Feign
parent
5b5bae2010
commit
ae9f3554da
|
@ -51,10 +51,6 @@ public class BikeController {
|
||||||
public ResponseEntity<List<Bike>> getByUserId(@PathVariable("userId") int userId) {
|
public ResponseEntity<List<Bike>> getByUserId(@PathVariable("userId") int userId) {
|
||||||
List<Bike> bikes = bikeService.findByUserId(userId);
|
List<Bike> bikes = bikeService.findByUserId(userId);
|
||||||
|
|
||||||
if (bikes.isEmpty()) {
|
return ResponseEntity.ok(bikes);
|
||||||
return ResponseEntity.noContent().build();
|
|
||||||
} else {
|
|
||||||
return ResponseEntity.ok(bikes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,10 +51,6 @@ public class CarController {
|
||||||
public ResponseEntity<List<Car>> getByUserId(@PathVariable("userId") int userId) {
|
public ResponseEntity<List<Car>> getByUserId(@PathVariable("userId") int userId) {
|
||||||
List<Car> cars = carService.findByUserId(userId);
|
List<Car> cars = carService.findByUserId(userId);
|
||||||
|
|
||||||
if (cars.isEmpty()) {
|
return ResponseEntity.ok(cars);
|
||||||
return ResponseEntity.noContent().build();
|
|
||||||
} else {
|
|
||||||
return ResponseEntity.ok(cars);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,11 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
<version>3.1.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
|
@ -42,6 +47,17 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>2021.0.4</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -2,8 +2,10 @@ package com.example.userservice;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
public class UserServiceApplication {
|
public class UserServiceApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.example.userservice.controller;
|
package com.example.userservice.controller;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
@ -71,4 +73,34 @@ public class UserController {
|
||||||
return ResponseEntity.ok(userService.getBikes(userId));
|
return ResponseEntity.ok(userService.getBikes(userId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/savecar/{userId}")
|
||||||
|
public ResponseEntity<Car> saveCar(@PathVariable("userId") int userId, @RequestBody Car car) {
|
||||||
|
User user = userService.getUserById(userId);
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.ok(userService.saveCar(car, userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/savebike/{userId}")
|
||||||
|
public ResponseEntity<Bike> saveBike(@PathVariable("userId") int userId, @RequestBody Bike bike) {
|
||||||
|
User user = userService.getUserById(userId);
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.ok(userService.saveBike(bike, userId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/getAll/{userId}")
|
||||||
|
public ResponseEntity<Map<String, Object>> getAllVehicules(@PathVariable("userId") int userId) {
|
||||||
|
Map<String, Object> result = userService.getUserAndVehicules(userId);
|
||||||
|
return ResponseEntity.ok(result);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.example.userservice.feignclients;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import com.example.userservice.model.Bike;
|
||||||
|
|
||||||
|
@FeignClient(name = "bike-service", url = "http://localhost:8003/bikes")
|
||||||
|
public interface BikeFeignClient {
|
||||||
|
@PostMapping
|
||||||
|
Bike save(@RequestBody Bike bike);
|
||||||
|
|
||||||
|
@GetMapping("/byuser/{userId}")
|
||||||
|
List<Bike> getBikes(@PathVariable("userId") int userId);
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.example.userservice.feignclients;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import com.example.userservice.model.Car;
|
||||||
|
|
||||||
|
@FeignClient(name = "car-service", url = "http://localhost:8002/cars")
|
||||||
|
public interface CarFeignClient {
|
||||||
|
@PostMapping
|
||||||
|
Car save(@RequestBody Car car);
|
||||||
|
|
||||||
|
@GetMapping("/byuser/{userId}")
|
||||||
|
List<Car> getCars(@PathVariable("userId") int userId);
|
||||||
|
}
|
|
@ -10,4 +10,5 @@ import lombok.NoArgsConstructor;
|
||||||
public class Bike {
|
public class Bike {
|
||||||
private String brand;
|
private String brand;
|
||||||
private String model;
|
private String model;
|
||||||
|
private int userId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,4 +10,5 @@ import lombok.NoArgsConstructor;
|
||||||
public class Car {
|
public class Car {
|
||||||
private String brand;
|
private String brand;
|
||||||
private String model;
|
private String model;
|
||||||
|
private int userId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
package com.example.userservice.service;
|
package com.example.userservice.service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import com.example.userservice.entity.User;
|
import com.example.userservice.entity.User;
|
||||||
|
import com.example.userservice.feignclients.BikeFeignClient;
|
||||||
|
import com.example.userservice.feignclients.CarFeignClient;
|
||||||
import com.example.userservice.model.Bike;
|
import com.example.userservice.model.Bike;
|
||||||
import com.example.userservice.model.Car;
|
import com.example.userservice.model.Car;
|
||||||
import com.example.userservice.repository.UserRepository;
|
import com.example.userservice.repository.UserRepository;
|
||||||
|
@ -17,6 +21,10 @@ public class UserService {
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
RestTemplate restTemplate;
|
RestTemplate restTemplate;
|
||||||
|
@Autowired
|
||||||
|
CarFeignClient carFeignClient;
|
||||||
|
@Autowired
|
||||||
|
BikeFeignClient bikeFeignClient;
|
||||||
|
|
||||||
public List<User> getAll() {
|
public List<User> getAll() {
|
||||||
return userRepository.findAll();
|
return userRepository.findAll();
|
||||||
|
@ -37,4 +45,38 @@ public class UserService {
|
||||||
public List<Bike> getBikes(int userId) {
|
public List<Bike> getBikes(int userId) {
|
||||||
return restTemplate.getForObject("http://localhost:8003/bikes/byuser/" + userId, List.class);
|
return restTemplate.getForObject("http://localhost:8003/bikes/byuser/" + userId, List.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Car saveCar(Car car, int userId) {
|
||||||
|
car.setUserId(userId);
|
||||||
|
return carFeignClient.save(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bike saveBike(Bike bike, int userId) {
|
||||||
|
bike.setUserId(userId);
|
||||||
|
return bikeFeignClient.save(bike);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getUserAndVehicules(int userId) {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
User user = userRepository.findById(userId).orElse(null);
|
||||||
|
if (user == null) {
|
||||||
|
result.put("Mensaje", "No existe el usuario");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
result.put("User", user);
|
||||||
|
List<Car> cars = carFeignClient.getCars(userId);
|
||||||
|
if (cars.isEmpty()) {
|
||||||
|
result.put("Cars", "Este usuario no tiene coches");
|
||||||
|
} else {
|
||||||
|
result.put("Cars", cars);
|
||||||
|
}
|
||||||
|
List<Bike> bikes = bikeFeignClient.getBikes(userId);
|
||||||
|
if (cars.isEmpty()) {
|
||||||
|
result.put("Bikes", "Este usuario no tiene motos");
|
||||||
|
} else {
|
||||||
|
result.put("Bikes", bikes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue