Edit for saving setted new inputs and calculated footprint
This commit is contained in:
@@ -4,9 +4,22 @@ import greenify.common.ApplicationException;
|
|||||||
import greenify.server.InputValidator;
|
import greenify.server.InputValidator;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
|
||||||
|
import javax.persistence.Table;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
@@ -130,10 +143,22 @@ public class User {
|
|||||||
this.footPrintInputs = footPrintInputs;
|
this.footPrintInputs = footPrintInputs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the friends of the user.
|
||||||
|
* @return friends list of the user
|
||||||
|
*/
|
||||||
public ArrayList<User> getFriends() {
|
public ArrayList<User> getFriends() {
|
||||||
return (ArrayList<User>)this.friends;
|
return (ArrayList<User>)this.friends;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the friend list of the user.
|
||||||
|
* @param friends friend list of the user
|
||||||
|
*/
|
||||||
|
public void setFriends(Collection<User> friends) {
|
||||||
|
this.friends = friends;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a friend to the friendslist of the user.
|
* Adds a friend to the friendslist of the user.
|
||||||
* @param user the friend you want to add.
|
* @param user the friend you want to add.
|
||||||
@@ -172,9 +197,8 @@ public class User {
|
|||||||
return result + "]";
|
return result + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** This method checks whether two users are equal or not.
|
||||||
* This method checks whether two users are equal or not.
|
* * @param other an other user
|
||||||
* @param other an other user
|
|
||||||
* @return users are (not) equal
|
* @return users are (not) equal
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class CalculatorService {
|
public class CalculatorService {
|
||||||
@Autowired
|
@Autowired
|
||||||
RestTemplate restTemplate;
|
RestTemplate restTemplate;
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ class CalculatorService {
|
|||||||
* @param map used variables to calculate a footprint
|
* @param map used variables to calculate a footprint
|
||||||
* @return a footprint
|
* @return a footprint
|
||||||
*/
|
*/
|
||||||
Float invokeExternalService(Map<String, String> map) {
|
public Float invokeExternalService(Map<String, String> map) {
|
||||||
/*
|
/*
|
||||||
* curl -X GET "https://apis.berkeley.edu/coolclimate/footprint-sandbox?input_location_mode=1
|
* curl -X GET "https://apis.berkeley.edu/coolclimate/footprint-sandbox?input_location_mode=1
|
||||||
* &input_location=48001&input_income=1&input_size=0&input_footprint_transportation_miles1=3
|
* &input_location=48001&input_income=1&input_size=0&input_footprint_transportation_miles1=3
|
||||||
@@ -49,7 +49,7 @@ class CalculatorService {
|
|||||||
headers.set("app_key", "b9167c4918cb2b3143614b595065d83b");
|
headers.set("app_key", "b9167c4918cb2b3143614b595065d83b");
|
||||||
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
|
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
|
||||||
UriComponentsBuilder builder =
|
UriComponentsBuilder builder =
|
||||||
UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint-sandbox");
|
UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint");
|
||||||
for (String inputId : map.keySet()) {
|
for (String inputId : map.keySet()) {
|
||||||
builder = builder.queryParam(inputId, map.get(inputId));
|
builder = builder.queryParam(inputId, map.get(inputId));
|
||||||
}
|
}
|
||||||
@@ -71,7 +71,7 @@ class CalculatorService {
|
|||||||
* @param user the user
|
* @param user the user
|
||||||
* @return the footprint of the user
|
* @return the footprint of the user
|
||||||
*/
|
*/
|
||||||
Float calculateFootprint(User user) {
|
public Float calculateFootprint(User user) {
|
||||||
return invokeExternalService(user.getFootPrintInputs());
|
return invokeExternalService(user.getFootPrintInputs());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ public class UserService {
|
|||||||
if (user == null) {
|
if (user == null) {
|
||||||
user = new User(null, name, password);
|
user = new User(null, name, password);
|
||||||
user.setFootPrintInputs(InputValidator.getDefaultValues());
|
user.setFootPrintInputs(InputValidator.getDefaultValues());
|
||||||
|
Float footprint = calculatorService.calculateFootprint(user);
|
||||||
|
user.setFootPrint(footprint);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
} else {
|
} else {
|
||||||
throw new ApplicationException("User already exists");
|
throw new ApplicationException("User already exists");
|
||||||
@@ -64,7 +66,6 @@ public class UserService {
|
|||||||
* Adds a friend to the friendlist of the user.
|
* Adds a friend to the friendlist of the user.
|
||||||
* @param name the username of the user
|
* @param name the username of the user
|
||||||
* @param friend the name of the friend you want to add.
|
* @param friend the name of the friend you want to add.
|
||||||
* @return a userDTO of the logged in user
|
|
||||||
*/
|
*/
|
||||||
public void addFriend(String name, String friend) {
|
public void addFriend(String name, String friend) {
|
||||||
User user = userRepository.findByName(name);
|
User user = userRepository.findByName(name);
|
||||||
@@ -99,7 +100,9 @@ public class UserService {
|
|||||||
if (InputValidator.isValidItem(inputName)
|
if (InputValidator.isValidItem(inputName)
|
||||||
&& InputValidator.isValidItemValue(inputName, value)) {
|
&& InputValidator.isValidItemValue(inputName, value)) {
|
||||||
user.getFootPrintInputs().put(inputName, value);
|
user.getFootPrintInputs().put(inputName, value);
|
||||||
|
userRepository.save(user);
|
||||||
user.setFootPrint(calculatorService.calculateFootprint(user));
|
user.setFootPrint(calculatorService.calculateFootprint(user));
|
||||||
|
userRepository.save(user);
|
||||||
} else {
|
} else {
|
||||||
throw new ApplicationException("Invalid input");
|
throw new ApplicationException("Invalid input");
|
||||||
}
|
}
|
||||||
@@ -112,7 +115,7 @@ public class UserService {
|
|||||||
* @param inputName name of the input
|
* @param inputName name of the input
|
||||||
* @return input value
|
* @return input value
|
||||||
*/
|
*/
|
||||||
String getInput(String name, String inputName) {
|
public String getInput(String name, String inputName) {
|
||||||
User user = userRepository.findByName(name);
|
User user = userRepository.findByName(name);
|
||||||
if (InputValidator.isValidItem(inputName)) {
|
if (InputValidator.isValidItem(inputName)) {
|
||||||
return user.getFootPrintInputs().get(inputName);
|
return user.getFootPrintInputs().get(inputName);
|
||||||
@@ -126,9 +129,11 @@ public class UserService {
|
|||||||
* @param name name of the user
|
* @param name name of the user
|
||||||
* @return footprint of the user
|
* @return footprint of the user
|
||||||
*/
|
*/
|
||||||
Float getFootprint(String name) {
|
public Float getFootprint(String name) {
|
||||||
User user = userRepository.findByName(name);
|
User user = userRepository.findByName(name);
|
||||||
return calculatorService.calculateFootprint(user);
|
user.setFootPrint(calculatorService.calculateFootprint(user));
|
||||||
|
userRepository.save(user);
|
||||||
|
return user.getFootPrint();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -137,7 +142,7 @@ public class UserService {
|
|||||||
*/
|
*/
|
||||||
@GetMapping(path = "/all")
|
@GetMapping(path = "/all")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
Iterable<User> getAllUsers() {
|
public Iterable<User> getAllUsers() {
|
||||||
return userRepository.findAll();
|
return userRepository.findAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user