Add methods for getting the results of a user

This commit is contained in:
cugurlu
2019-04-08 15:36:21 +02:00
parent 9fd3b1cf5b
commit 4dd02649b8
4 changed files with 77 additions and 0 deletions

View File

@@ -301,6 +301,23 @@ public class UserService {
.encode().toUri(), Map.class);
}
/**
* Gets the results of a user.
* @param name name of the user
* @return Map with all results
*/
@SuppressWarnings("Duplicates")
public Map<String, String> getResults(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getResults")
.queryParam("name", name);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
return this.restTemplate.getForObject(builder.build()
.encode().toUri(), Map.class);
}
/**
* Gets the list of all users.
*/

View File

@@ -185,5 +185,15 @@ public class UserController {
public Map<String, Boolean> getAchievements(@RequestParam(value = "name") String name) {
return userService.getAchievements(name);
}
/**
* This method gets the result for schema.
* @param name name of the user
* @return map of all results of the user
*/
@RequestMapping("/getResults")
public Map<String, String> getResults(@RequestParam(value = "name") String name) {
return userService.getResults(name);
}
}

View File

@@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@@ -108,5 +109,43 @@ public class CalculatorService {
inputs.put("input_footprint_shopping_goods_total", netShopping + "");
user.setFootPrintInputs(inputs);
}
public Map<String, String> getResults(Map<String, String> map) {
HttpHeaders headers = new HttpHeaders();
headers.set("accept", MediaType.APPLICATION_JSON_VALUE);
headers.set("app_id", "a98272e3");
headers.set("app_key", "b9167c4918cb2b3143614b595065d83b");
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint");
for (String inputId : map.keySet()) {
builder = builder.queryParam(inputId, map.get(inputId));
}
ResponseEntity<String> response = restTemplate
.exchange(builder.build().encode().toUri(), HttpMethod.GET,
entity, String.class);
String transport = response.getBody().substring(Objects.requireNonNull(response.getBody())
.indexOf("<result_transport_total>")
+ 24, response.getBody().indexOf("</result_transport_total>"));
String housing = response.getBody().substring(Objects.requireNonNull(response.getBody())
.indexOf("<result_housing_total>")
+ 22, response.getBody().indexOf("</result_housing_total>"));
String food = response.getBody().substring(Objects.requireNonNull(response.getBody())
.indexOf("<result_food_total>")
+ 19, response.getBody().indexOf("</result_food_total>"));
String goods = response.getBody().substring(Objects.requireNonNull(response.getBody())
.indexOf("<result_goods_total>")
+ 20, response.getBody().indexOf("</result_goods_total>"));
String services = response.getBody().substring(Objects.requireNonNull(response.getBody())
.indexOf("<result_services_total>")
+ 23, response.getBody().indexOf("</result_services_total>"));
Map<String, String> resultMap = new HashMap<>();
resultMap.put("transport", transport);
resultMap.put("housing", housing);
resultMap.put("food", food);
resultMap.put("goods", goods);
resultMap.put("services", services);
return resultMap;
}
}

View File

@@ -280,6 +280,17 @@ public class UserService {
return user.getAchievements();
}
/**
* This method gets all achievements of a user.
* @param name name of the user
* @return map with all achievements of a user
*/
public Map<String, String> getResults(String name) {
User user = userRepository.findByName(name);
Map<String, String> results = calculatorService.getResults(user.getFootPrintInputs());
return results;
}
/**
* This method gets the list of all users.
* @return list of all users