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 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 java.util.*;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@@ -130,10 +143,22 @@ public class User {
|
||||
this.footPrintInputs = footPrintInputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method gets the friends of the user.
|
||||
* @return friends list of the user
|
||||
*/
|
||||
public ArrayList<User> getFriends() {
|
||||
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.
|
||||
* @param user the friend you want to add.
|
||||
@@ -172,9 +197,8 @@ public class User {
|
||||
return result + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks whether two users are equal or not.
|
||||
* @param other an other user
|
||||
/** This method checks whether two users are equal or not.
|
||||
* * @param other an other user
|
||||
* @return users are (not) equal
|
||||
*/
|
||||
@Override
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
class CalculatorService {
|
||||
public class CalculatorService {
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@@ -35,7 +35,7 @@ class CalculatorService {
|
||||
* @param map used variables to calculate 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
|
||||
* &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");
|
||||
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
|
||||
UriComponentsBuilder builder =
|
||||
UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint-sandbox");
|
||||
UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint");
|
||||
for (String inputId : map.keySet()) {
|
||||
builder = builder.queryParam(inputId, map.get(inputId));
|
||||
}
|
||||
@@ -71,7 +71,7 @@ class CalculatorService {
|
||||
* @param user the user
|
||||
* @return the footprint of the user
|
||||
*/
|
||||
Float calculateFootprint(User user) {
|
||||
public Float calculateFootprint(User user) {
|
||||
return invokeExternalService(user.getFootPrintInputs());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ public class UserService {
|
||||
if (user == null) {
|
||||
user = new User(null, name, password);
|
||||
user.setFootPrintInputs(InputValidator.getDefaultValues());
|
||||
Float footprint = calculatorService.calculateFootprint(user);
|
||||
user.setFootPrint(footprint);
|
||||
userRepository.save(user);
|
||||
} else {
|
||||
throw new ApplicationException("User already exists");
|
||||
@@ -64,7 +66,6 @@ public class UserService {
|
||||
* Adds a friend to the friendlist of the user.
|
||||
* @param name the username of the user
|
||||
* @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) {
|
||||
User user = userRepository.findByName(name);
|
||||
@@ -99,7 +100,9 @@ public class UserService {
|
||||
if (InputValidator.isValidItem(inputName)
|
||||
&& InputValidator.isValidItemValue(inputName, value)) {
|
||||
user.getFootPrintInputs().put(inputName, value);
|
||||
userRepository.save(user);
|
||||
user.setFootPrint(calculatorService.calculateFootprint(user));
|
||||
userRepository.save(user);
|
||||
} else {
|
||||
throw new ApplicationException("Invalid input");
|
||||
}
|
||||
@@ -112,7 +115,7 @@ public class UserService {
|
||||
* @param inputName name of the input
|
||||
* @return input value
|
||||
*/
|
||||
String getInput(String name, String inputName) {
|
||||
public String getInput(String name, String inputName) {
|
||||
User user = userRepository.findByName(name);
|
||||
if (InputValidator.isValidItem(inputName)) {
|
||||
return user.getFootPrintInputs().get(inputName);
|
||||
@@ -126,9 +129,11 @@ public class UserService {
|
||||
* @param name name of the user
|
||||
* @return footprint of the user
|
||||
*/
|
||||
Float getFootprint(String name) {
|
||||
public Float getFootprint(String 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")
|
||||
@ResponseBody
|
||||
Iterable<User> getAllUsers() {
|
||||
public Iterable<User> getAllUsers() {
|
||||
return userRepository.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user