diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index b2ffc45..4f85d38 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -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 getFriends() { return (ArrayList)this.friends; } + /** + * This method sets the friend list of the user. + * @param friends friend list of the user + */ + public void setFriends(Collection 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 diff --git a/src/Server/src/main/java/greenify/server/service/CalculatorService.java b/src/Server/src/main/java/greenify/server/service/CalculatorService.java index dbf9a65..fa4294d 100644 --- a/src/Server/src/main/java/greenify/server/service/CalculatorService.java +++ b/src/Server/src/main/java/greenify/server/service/CalculatorService.java @@ -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 map) { + public Float invokeExternalService(Map 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 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()); } } diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 901a0ea..6d84a81 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -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 getAllUsers() { + public Iterable getAllUsers() { return userRepository.findAll(); } }