diff --git a/src/.idea/checkstyle-idea.xml b/src/.idea/checkstyle-idea.xml deleted file mode 100644 index ef7efd6..0000000 --- a/src/.idea/checkstyle-idea.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/.idea/checkstyleidea-libs/readme.txt b/src/.idea/checkstyleidea-libs/readme.txt deleted file mode 100644 index cd71e1b..0000000 --- a/src/.idea/checkstyleidea-libs/readme.txt +++ /dev/null @@ -1,6 +0,0 @@ -This folder contains libraries copied from the "OOPP" project. -It is managed by the CheckStyle-IDEA IDE plugin. -Do not modify this folder while the IDE is running. -When the IDE is stopped, you may delete this folder at any time. It will be recreated as needed. -In order to prevent the CheckStyle-IDEA IDE plugin from creating this folder, -uncheck the "Copy libraries from project directory" option in the CheckStyle-IDEA settings dialog. diff --git a/src/.idea/compiler.xml b/src/.idea/compiler.xml deleted file mode 100644 index a1757ae..0000000 --- a/src/.idea/compiler.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/src/.idea/encodings.xml b/src/.idea/encodings.xml deleted file mode 100644 index 15a15b2..0000000 --- a/src/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/.idea/gradle.xml b/src/.idea/gradle.xml deleted file mode 100644 index fb07249..0000000 --- a/src/.idea/gradle.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml deleted file mode 100644 index bc8d0a3..0000000 --- a/src/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/src/.idea/uiDesigner.xml b/src/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/src/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml deleted file mode 100644 index 6c0b863..0000000 --- a/src/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java index b44eff2..bfa0576 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -6,13 +6,10 @@ import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class DashBoardController { - @Autowired - UserService userService; private int count = 0; @@ -25,19 +22,41 @@ public class DashBoardController { public Button dashboardButton; public Button activitiesButton; public Button userButton; - public Button veganMealButton; - public Label counter; public Label scoreField; + //activities buttons + @FXML + public Button veganMealButton; + public Button localProduceButton; + public Button bikeButton; + public Button publicTransportButton; + public Button temperatureButton; + public Button solarPanelButton; + + //activities counters + public Label veganMealCounter; + public Label localProduceCounter; + public Label bikeCounter; + public Label publicTransportCounter; + public Label temperatureCounter; + public Label solarPanelCounter; + /** * displays the dashboard pane. * @param event the event (clicking the button) */ public void displayDashboard(ActionEvent event) { System.out.println("display dashboard"); + + // UserService service = new UserService(); + // UserDTO user = service.getName(null); + // String name = user.getName(); + // welcomebacktext.setText("Welcome back, " + name); + dashboardPane.setVisible(true); userPane.setVisible(false); activitiesPane.setVisible(false); + } /** @@ -68,9 +87,10 @@ public class DashBoardController { */ public void addVeganMeal(ActionEvent event) { count++; - counter.setText("Count: " + count); - System.out.println(userService); - userService.addVeganMeal(userService.currentUser.getId(), userService.currentUser.getName()); + veganMealCounter.setText("Count: " + count); + UserService service = new UserService(); + service.addVeganMeal(null, null); System.out.println("Vegetarian meal is added"); + } } diff --git a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java index a0e6a8b..df99252 100644 --- a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java +++ b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java @@ -33,7 +33,7 @@ public class RegisterWindowController { */ @FXML public void handleSignUpButton(ActionEvent event) { - //set the window to the current window (for displaying the alerts) + //set the window to the current window (for displaying the alerts) Window owner = signupButton.getScene().getWindow(); //check if the username field is empty if (userNameText.getText().isEmpty()) { diff --git a/src/Client/src/main/java/greenify/client/controller/UserController.java b/src/Client/src/main/java/greenify/client/controller/UserController.java index 2668d93..23c8a47 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -1,6 +1,5 @@ package greenify.client.controller; -import greenify.client.Application; import greenify.client.rest.UserService; import javafx.event.ActionEvent; import javafx.fxml.FXML; @@ -65,9 +64,13 @@ public class UserController { * @author sem */ public void openDashboard() throws IOException { - Parent dash = Application.load (this.getClass().getClassLoader().getResource("fxml/dashboard.fxml")); + Parent dash = FXMLLoader.load( + this.getClass().getClassLoader().getResource("fxml/dashboard.fxml") + ); Scene scene = new Scene(dash); - scene.getStylesheets().add(getClass().getClassLoader().getResource("stylesheets/dashboardStyle.css").toExternalForm()); + scene.getStylesheets().add(getClass() + .getClassLoader() + .getResource("stylesheets/dashboardStyle.css").toExternalForm()); Stage appStage = new Stage(); appStage.setScene(scene); appStage.show(); @@ -94,14 +97,22 @@ public class UserController { } } - public void handleRegisterButtonAction(ActionEvent event) throws Exception{ + /** + * handles the click of the 'sign up' button. + * opens a new stage where the user can register. + * @param event the click of the button + * @throws Exception an exception if the fxml file is not found + */ + public void handleRegisterButtonAction(ActionEvent event) throws Exception { //load the fxml file - Parent registerWindow = Application.load (this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml")); + Parent registerWindow = FXMLLoader.load( + this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml") + ); //make the window use the scene - Scene registerScene = new Scene(registerWindow); + Scene registerscene = new Scene(registerWindow); Stage registerStage = new Stage(); //open the window - registerStage.setScene(registerScene); + registerStage.setScene(registerscene); registerStage.setTitle("Enter register credentials"); registerStage.show(); } diff --git a/src/Client/src/main/java/greenify/client/rest/UserService.java b/src/Client/src/main/java/greenify/client/rest/UserService.java index d1822f0..e951973 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -4,19 +4,19 @@ import greenify.common.UserDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; -import org.springframework.http.*; -import org.springframework.stereotype.Component; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @Service public class UserService { + @Autowired RestTemplate restTemplate; - public UserDTO currentUser; - @Bean RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); @@ -26,8 +26,9 @@ public class UserService { * registers the user. * @param name the username of the user * @param password the password of the user - * @return a userDTO + * @return a built userDTO with the required information */ + @SuppressWarnings("Duplicates") public UserDTO registerUser(String name, String password) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); @@ -36,17 +37,16 @@ public class UserService { .queryParam("password", password); HttpEntity entity = new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); - UserDTO result = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); - this.currentUser = result; - return result; + return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); } /** - * sign ins the user. + * logd the user in. * @param name the username of the user * @param password the password of the user - * @return a userDTO + * @return a built userDTO with the required information */ + @SuppressWarnings("Duplicates") public UserDTO loginUser(String name, String password) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); @@ -55,17 +55,16 @@ public class UserService { .queryParam("password", password); HttpEntity entity = new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); - UserDTO result = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); - this.currentUser = result; - return result; + return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); } /** - * a user adds vegan meal. + * adds a vegetarian meal to the user. * @param id the id of the user * @param name the username of the user - * @return a userDTO + * @return a built userDTO with the required information */ + @SuppressWarnings("Duplicates") public UserDTO addVeganMeal(Long id, String name) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); @@ -76,4 +75,20 @@ public class UserService { System.out.println(builder.build().encode().toUri()); return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); } + + /** + * gets the username from the user. + * @param id the id of the user + * @return a UserDTO with the required information + */ + @SuppressWarnings("Duplicates") + public UserDTO getName(Long id) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getUsername") + .queryParam("id", id); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); + } } diff --git a/src/Client/src/main/resources/fxml/dashboard.fxml b/src/Client/src/main/resources/fxml/dashboard.fxml index 1627774..900ed93 100644 --- a/src/Client/src/main/resources/fxml/dashboard.fxml +++ b/src/Client/src/main/resources/fxml/dashboard.fxml @@ -1,11 +1,15 @@ + + + + @@ -35,21 +39,84 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + 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 d588eb1..d2b8aa7 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 @@ -5,7 +5,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import javax.persistence.*; import javax.validation.constraints.NotNull; -import java.util.Objects; @EnableAutoConfiguration @Entity @@ -49,7 +48,9 @@ public class User { return id; } - public void setId(Long id) { this.id = id; } + public void setId(Long id) { + this.id = id; + } /** * gets the name. @@ -59,7 +60,9 @@ public class User { return name; } - public void setName(String name) { this.name = name; } + public void setName(String name) { + this.name = name; + } /** * gets the password. @@ -69,7 +72,9 @@ public class User { return password; } - public void setPassword(String password) { this.password = password; } + public void setPassword(String password) { + this.password = password; + } /** * gets the number of vegan meal. @@ -79,58 +84,7 @@ public class User { return veganMeal; } - public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } - - /** - * checks if two users are equal. - * @param other the object to compare the user with - * @return a boolean value of true if the user is equal to the object - */ - @Override - public boolean equals(Object other) { - - if (this == other) { - return true; - } - if (other == null) { - return false; - } - if (getClass() != other.getClass()) { - return false; - } - if (other instanceof User) { - User that = (User) other; - return this.getName().equals(that.getName()) - && this.getId().equals(that.getId()) - && this.getPassword().equals(that.getPassword()) - && this.getVeganMeal() == that.getVeganMeal(); - } - return false; - } - - /** - * creates a string of the user object. - * in the form of: User(id=, name=, password=, veganMeal=) - * @return a string of the user object - */ - @Override - public String toString() { - return "User(id=" - + this.id - + ", name=" - + this.name - + ", password=" - + this.password - + ", veganMeal=" - + this.veganMeal + ")"; - } - - /** - * hashes the User object. - * @return a hashcode for the user object - */ - @Override - public int hashCode() { - return Objects.hash(id, name, password, veganMeal); + public void setVeganMeal(int veganMeal) { + this.veganMeal = veganMeal; } } diff --git a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java index 5e7d511..1e8d910 100644 --- a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java +++ b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java @@ -1,10 +1,14 @@ package greenify.server.data.repository; -import org.springframework.data.repository.CrudRepository; import greenify.server.data.model.User; +import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository { + // User findByName(@Param("name") String name); User findByName(String name); + + User findById(Long id); + T save(T user); } diff --git a/src/Server/src/main/java/greenify/server/rest/MainController.java b/src/Server/src/main/java/greenify/server/rest/MainController.java index b985819..5cdb8f4 100644 --- a/src/Server/src/main/java/greenify/server/rest/MainController.java +++ b/src/Server/src/main/java/greenify/server/rest/MainController.java @@ -11,26 +11,32 @@ import greenify.server.data.model.User; import greenify.server.data.repository.UserRepository; @Controller // This means that this class is a Controller -@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path) +@RequestMapping(path = "/demo") // This means URL's start with /demo (after Application path) public class MainController { @Autowired // This means to get the bean called userRepository // Which is auto-generated by Spring, we will use it to handle the data private UserRepository userRepository; - @GetMapping(path="/add") // Map ONLY GET Requests - public @ResponseBody String addNewUser (@RequestParam String name - , @RequestParam String password) { + /** + * getmapping for adding a user. + * @param name the username of the user + * @param password the password of the user + * @return a response for adding the user, "saved", if it succeeded + */ + @GetMapping(path = "/add") // Map ONLY GET Requests + public @ResponseBody String addNewUser(@RequestParam String name, + @RequestParam String password) { // @ResponseBody means the returned String is the response, not a view name // @RequestParam means it is a parameter from the GET or POST request - User n = new User(); - n.setName(name); - n.setPassword(password); - userRepository.save(n); + User newuser = new User(); + newuser.setName(name); + newuser.setPassword(password); + userRepository.save(newuser); return "Saved"; } - @GetMapping(path="/all") + @GetMapping(path = "/all") public @ResponseBody Iterable getAllUsers() { // This returns a JSON or XML with the users return userRepository.findAll(); diff --git a/src/Server/src/main/java/greenify/server/rest/UserController.java b/src/Server/src/main/java/greenify/server/rest/UserController.java index 452689f..908efd7 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -1,21 +1,18 @@ package greenify.server.rest; import greenify.common.UserDTO; -import greenify.server.data.model.User; -import greenify.server.data.repository.UserRepository; import greenify.server.service.UserService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired UserService userService; - @Autowired // This means to get the bean called userRepository - // Which is auto-generated by Spring, we will use it to handle the data - UserRepository userRepository; - @RequestMapping("/registerUser") public UserDTO registerUser(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) { @@ -28,16 +25,14 @@ public class UserController { return userService.loginUser(name, password); } - @GetMapping(path="/all") - public @ResponseBody - Iterable getAllUsers() { - // This returns a JSON or XML with the users - return userRepository.findAll(); - } - @RequestMapping("/addVeganMeal") public void addVeganMeal(@RequestParam(value = "id") Long id, @RequestParam(value = "name") String name) { userService.addVeganMeal(id, name); } + + @GetMapping("/getUsername") + public void getUsername(@RequestParam(value = "id") Long id) { + userService.getUsername(id); + } } \ No newline at end of file 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 68d257a..cb6a895 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -23,15 +23,16 @@ public class UserService { */ public UserDTO registerUser(String name, String password) { User user = userRepository.findByName(name); - if (user == null) { - user = userRepository.save(new User(null, name, password, 0)); - } else { + if (user != null) { throw new ApplicationException("User already exists"); + } else { + user = userRepository.save(new User(null, name, password, 0)); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); return new UserDTO(user.getId(), user.getName()); } + /** * logs the user in. * @param name the username of the user @@ -54,15 +55,26 @@ public class UserService { * add vegan meal to the user. * @param id the id of the user * @param name the name of the user - * @return a userDTO of the user added vegan meal */ public void addVeganMeal(Long id, String name) { User user = userRepository.findByName(name); int count = user.getVeganMeal(); count++; user.setVeganMeal(count); - userRepository.save(user); - logger.info("Added vegan meal to user(id=" + user.getId() + ", name=" + user.getName() + ")"); + logger.info("Added vegan meal to user(id=" + + user.getId() + ", name=" + user.getName() + ")"); + } + + /** + * gets the username of the user with the specified id. + * @param id the id of the user + * @return the username of the user + */ + public String getUsername(Long id) { + User user = userRepository.findById(id); + String name = user.getName(); + logger.info("retrieved username from user with username=" + name + ", id=" + id); + return name; } }