Skip to content
Snippets Groups Projects

feat: send request to visualization if getDriver is called

Merged Alžbeta Hajná requested to merge feat-01 into develop
5 files
+ 51
8
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -4,11 +4,20 @@ import cz.muni.pa165.data.model.Driver;
@@ -4,11 +4,20 @@ import cz.muni.pa165.data.model.Driver;
import cz.muni.pa165.data.repository.DriverRepository;
import cz.muni.pa165.data.repository.DriverRepository;
import cz.muni.pa165.exceptions.BadRequestException;
import cz.muni.pa165.exceptions.BadRequestException;
import cz.muni.pa165.exceptions.ResourceNotFoundException;
import cz.muni.pa165.exceptions.ResourceNotFoundException;
 
import org.slf4j.Logger;
 
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
 
import org.springframework.http.HttpEntity;
 
import org.springframework.http.HttpHeaders;
 
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Transactional;
 
import org.springframework.web.client.RestClientException;
 
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.List;
import java.util.List;
 
import java.util.Map;
import java.util.Optional;
import java.util.Optional;
/**
/**
@@ -17,19 +26,35 @@ import java.util.Optional;
@@ -17,19 +26,35 @@ import java.util.Optional;
@Service
@Service
public class DriverService extends DomainService<Driver> {
public class DriverService extends DomainService<Driver> {
 
private static final Logger log = LoggerFactory.getLogger(DriverService.class);
 
private final DriverRepository driverRepository;
private final DriverRepository driverRepository;
 
private final RestTemplate restTemplate;
 
 
private static final String VISUALIZATION_MODULE_URL = "http://localhost:8082/visualization";
 
@Autowired
@Autowired
public DriverService(DriverRepository repository) {
public DriverService(DriverRepository repository, RestTemplate restTemplate) {
super(repository, Driver.class);
super(repository, Driver.class);
driverRepository = repository;
driverRepository = repository;
 
this.restTemplate = restTemplate;
}
}
@Transactional(readOnly = true)
@Transactional(readOnly = true)
public Driver findById(Long id) {
public Driver findById(Long id) {
return driverRepository.findById(id)
Optional<Driver> dbDriver = driverRepository.findById(id);
.orElseThrow(() ->
if (dbDriver.isEmpty()) {
new ResourceNotFoundException(entityClass, id));
throw new ResourceNotFoundException(entityClass, id);
 
}
 
 
try {
 
sendPostRequest(dbDriver.get());
 
} catch (RestClientException | IllegalArgumentException e) {
 
log.debug(String.format("The visualization module is not reachable on the URL: %s", VISUALIZATION_MODULE_URL));
 
}
 
 
return dbDriver.get();
}
}
public Driver update(Long id, Driver driver) {
public Driver update(Long id, Driver driver) {
@@ -66,5 +91,17 @@ public class DriverService extends DomainService<Driver> {
@@ -66,5 +91,17 @@ public class DriverService extends DomainService<Driver> {
repository.delete(driverFromDb.get());
repository.delete(driverFromDb.get());
}
}
 
 
private void sendPostRequest(Driver driver) {
 
Map<String, Object> body = new HashMap<>();
 
body.put("data", driver.toString());
 
 
HttpHeaders headers = new HttpHeaders();
 
headers.setContentType(MediaType.APPLICATION_JSON);
 
HttpEntity<Map<String, Object>> request = new HttpEntity<>(body, headers);
 
restTemplate.postForObject(VISUALIZATION_MODULE_URL, request, String.class);
 
 
log.debug(String.format("Sent request %s to %s.", request, VISUALIZATION_MODULE_URL));
 
}
}
}
Loading