ADD::added comments to most classes to make it more understandable
This commit is contained in:
@@ -12,11 +12,15 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
//springbootApplication is so Spring knows that this is a Spring application
|
||||
@SpringBootApplication
|
||||
public class Application extends javafx.application.Application {
|
||||
//configurable application is for spring so it knows that it can use it
|
||||
private static ConfigurableApplicationContext springContext;
|
||||
//logger to log all the things that happen to the console
|
||||
private static final Logger log = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
//launch is to launch the GUI things
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
@@ -25,8 +29,10 @@ public class Application extends javafx.application.Application {
|
||||
* This method takes an url and return a parent.
|
||||
* @param url which is being loaded.
|
||||
* @return parent object.
|
||||
* @throws IOException if it can't find an FXML file
|
||||
*/
|
||||
public static Parent load(java.net.URL url) throws IOException {
|
||||
//loader to load the FXML file
|
||||
FXMLLoader loader = new FXMLLoader();
|
||||
loader.setControllerFactory(springContext::getBean);
|
||||
loader.setLocation(url);
|
||||
@@ -35,16 +41,22 @@ public class Application extends javafx.application.Application {
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
//run the application
|
||||
springContext = SpringApplication.run(Application.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
//load the fxml file
|
||||
Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/sample.fxml"));
|
||||
//set the title for the window
|
||||
primaryStage.setTitle("Greenify");
|
||||
//set the scene
|
||||
Scene scene = new Scene(rootNode);
|
||||
//add the stylesheet
|
||||
scene.getStylesheets()
|
||||
.add(getClass().getClassLoader().getResource("stylesheets/LoginWindowStyle.css").toExternalForm());
|
||||
.add(getClass().getClassLoader().getResource("stylesheets/LoginWindowStyle.css")
|
||||
.toExternalForm());
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@ import javafx.util.Duration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
//Class that controls the dashboard fxml file (the GUI Screen)
|
||||
|
||||
|
||||
@Controller
|
||||
public class DashBoardController {
|
||||
@Autowired
|
||||
@@ -36,12 +39,19 @@ public class DashBoardController {
|
||||
@FXML
|
||||
private Label welcomebacktext;
|
||||
|
||||
FadeTransition fadeTrans;
|
||||
FadeTransition fadeTrans; //transition for switching between the different panels
|
||||
|
||||
/**
|
||||
* loads the 'welcome back' text before anything else.
|
||||
*/
|
||||
public void initialize() {
|
||||
welcomebacktext.setText("Welcome back, " + userService.currentUser.getName() + "!");
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a fade transition for switching between the different panes.
|
||||
* @param node the node on which the transition needs to act
|
||||
*/
|
||||
public void addFadeTransition(Node node) {
|
||||
|
||||
fadeTrans = new FadeTransition(Duration.millis(400), node);
|
||||
|
||||
@@ -11,7 +11,7 @@ import javafx.stage.Stage;
|
||||
import javafx.stage.Window;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
//class that controls the actions for the register window
|
||||
@Controller
|
||||
public class RegisterWindowController {
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.springframework.stereotype.Controller;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
//class that controls the actions for the login screen
|
||||
@Controller
|
||||
public class UserController {
|
||||
@Autowired
|
||||
@@ -34,12 +35,22 @@ public class UserController {
|
||||
@FXML
|
||||
private Button signupButton;
|
||||
|
||||
/**
|
||||
* handles when the user clicks on the login button.
|
||||
* it checks if the username and password fields are filled
|
||||
* and gives alerts if they aren't filled in.
|
||||
* @param event the click of the login button
|
||||
* @throws IOException an exception for logging in the user
|
||||
*/
|
||||
@FXML
|
||||
protected void handleLoginButtonAction(ActionEvent event) throws IOException {
|
||||
Window owner = loginButton.getScene().getWindow();
|
||||
|
||||
Window owner = loginButton.getScene().getWindow(); //get the current window
|
||||
if (usernameField.getText().isEmpty()) {
|
||||
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Log-in Error!",
|
||||
"Please enter your username");
|
||||
//checks if the username field is filled,
|
||||
// and gives an alert if it is not
|
||||
return;
|
||||
} else {
|
||||
System.out.println("Username is " + usernameField.getText());
|
||||
@@ -47,13 +58,18 @@ public class UserController {
|
||||
if (passwordField.getText().isEmpty()) {
|
||||
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Log-in Error!",
|
||||
"Please enter a password");
|
||||
//checks if the password field id filled,
|
||||
// and gives an alert if it is not.
|
||||
return;
|
||||
} else {
|
||||
System.out.println("Password is " + passwordField.getText());
|
||||
}
|
||||
//log the user in with the userService method
|
||||
userService.loginUser(usernameField.getText(), passwordField.getText());
|
||||
Stage current = (Stage) owner;
|
||||
//after logging in, close the login window
|
||||
current.close();
|
||||
//open the other dashboard window
|
||||
openDashboard();
|
||||
|
||||
}
|
||||
@@ -64,17 +80,21 @@ public class UserController {
|
||||
* @author sem
|
||||
*/
|
||||
public void openDashboard() throws IOException {
|
||||
//load the fxml file
|
||||
Parent dash = Application.load(this.getClass().getClassLoader()
|
||||
.getResource("fxml/dashboard.fxml"));
|
||||
Scene scene = new Scene(dash);
|
||||
//add the stylesheet for the CSS
|
||||
scene.getStylesheets().add(getClass().getClassLoader()
|
||||
.getResource("stylesheets/dashboardStyle.css").toExternalForm());
|
||||
Stage appStage = new Stage();
|
||||
appStage.setScene(scene);
|
||||
//set the title
|
||||
appStage.setTitle("Greenify - " + usernameField.getText());
|
||||
appStage.show();
|
||||
}
|
||||
|
||||
//class for showing the alerts
|
||||
public static class AlertHelper {
|
||||
/**
|
||||
* alerts for the login screen.
|
||||
@@ -99,7 +119,7 @@ public class UserController {
|
||||
/**
|
||||
* The method handles register button.
|
||||
* @param event User clicks to the button
|
||||
* @throws Exception when the file couldn't find
|
||||
* @throws Exception when the file couldn't be found
|
||||
*/
|
||||
public void handleRegisterButtonAction(ActionEvent event) throws Exception {
|
||||
Parent registerWindow = Application.load(this.getClass().getClassLoader()
|
||||
|
||||
@@ -31,14 +31,26 @@ public class UserService {
|
||||
* @param password the password of the user
|
||||
* @return a userDTO
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
//this suppressWarnings is to get rid of the errors of duplicate code
|
||||
//because the methods are very similar
|
||||
public UserDto registerUser(String name, String password) {
|
||||
//headers for http
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
//set the accept header in JSÖN value
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
//connect to the server with the needed url
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/registerUser")
|
||||
.queryParam("name", name)
|
||||
//getting the name from the database
|
||||
.queryParam("password", password);
|
||||
//getting the password from the database
|
||||
|
||||
//create a http entity to be sent
|
||||
HttpEntity<?> entity = new HttpEntity<>(headers);
|
||||
System.out.println(builder.build().encode().toUri());
|
||||
|
||||
//the result to be sent is a userDto
|
||||
UserDto result = this.restTemplate.getForObject(builder.build()
|
||||
.encode().toUri(), UserDto.class);
|
||||
this.currentUser = result;
|
||||
@@ -51,7 +63,9 @@ public class UserService {
|
||||
* @param password the password of the user
|
||||
* @return a userDTO
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public UserDto loginUser(String name, String password) {
|
||||
//this method is almost the same as the registerUser one, but with a different link
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/loginUser")
|
||||
@@ -71,7 +85,9 @@ public class UserService {
|
||||
* @param name the username of the user
|
||||
* @return a userDTO
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public UserDto addVeganMeal(Long id, String name) {
|
||||
//this method is almost the same as the registerUser one, but with a different link
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addVeganMeal")
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class UserServiceTest {
|
||||
//logger that logs everything to the console
|
||||
private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class);
|
||||
|
||||
@Mock
|
||||
@@ -25,6 +26,7 @@ public class UserServiceTest {
|
||||
|
||||
@Test
|
||||
public void userRegisterTest() throws Exception {
|
||||
//tests if registering works
|
||||
UserDto testUser = new UserDto(1L, "Eric", 0);
|
||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
|
||||
UserDto.class))
|
||||
@@ -36,6 +38,7 @@ public class UserServiceTest {
|
||||
|
||||
@Test
|
||||
public void userLoginTest() throws Exception {
|
||||
//tests if logging in works
|
||||
UserDto testUser = new UserDto(1L, "Eric", 0);
|
||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
|
||||
UserDto.class))
|
||||
@@ -46,6 +49,7 @@ public class UserServiceTest {
|
||||
|
||||
@Test
|
||||
public void addVeganMealTest() throws Exception {
|
||||
//tests if adding a vegetarian meal works
|
||||
UserDto testUser = new UserDto(1L, "Eric", 0);
|
||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/addVeganMeal?id=1&name=Eric"),
|
||||
UserDto.class))
|
||||
|
||||
@@ -11,10 +11,10 @@ public class ActivityDto {
|
||||
|
||||
/**
|
||||
* Constructor for ActivityDto.
|
||||
* @param id of the activity
|
||||
* @param name of the activity
|
||||
* @param description of the activity
|
||||
* @param score of the activity
|
||||
* @param id id of the activity
|
||||
* @param name name of the activity
|
||||
* @param description description of the activity
|
||||
* @param score score of the activity
|
||||
*/
|
||||
public ActivityDto(Long id, String name, String description, int score) {
|
||||
this.id = id;
|
||||
@@ -23,34 +23,68 @@ public class ActivityDto {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
//all the getters and setters of the class
|
||||
|
||||
/**
|
||||
* gets the name of the activity.
|
||||
* @return the name of the activity
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the name of the activity.
|
||||
* @param name the name to be set of the activity.
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the id of the activity.
|
||||
* @return the id of the activity.
|
||||
*/
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the id of the activity.
|
||||
* @param id the id to be set of the activity.
|
||||
*/
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the description of the activity.
|
||||
* @return the description of the activity.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the description of the activity.
|
||||
* @param description the description to be set of the activity.
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the score of the activity.
|
||||
* @return the score of the activity.
|
||||
*/
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the score of the activity.
|
||||
* @param score the score to be set of the activity.
|
||||
*/
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package greenify.server.data.repository;
|
||||
import greenify.server.data.model.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
//userRepository that saves all the user and talks to the database
|
||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||
User findByName(String name);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
//class that handles exceptions for the rest server
|
||||
@RestControllerAdvice
|
||||
public class RestExceptionHandler {
|
||||
@ExceptionHandler(ApplicationException.class)
|
||||
|
||||
@@ -7,23 +7,28 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
//class that controls the user with regards to the server and sending data between them
|
||||
//this class kind of 'redirects' the requests from the client to the server
|
||||
@RestController
|
||||
public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
//registers a user in the userService
|
||||
@RequestMapping("/registerUser")
|
||||
public UserDto registerUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.registerUser(name, password);
|
||||
}
|
||||
|
||||
//logs a user in in the userService
|
||||
@RequestMapping("/loginUser")
|
||||
public UserDto loginUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.loginUser(name, password);
|
||||
}
|
||||
|
||||
//adds a vegan meal to the user
|
||||
@RequestMapping("/addVeganMeal")
|
||||
public void addVeganMeal(@RequestParam(value = "id") Long id,
|
||||
@RequestParam(value = "name") String name) {
|
||||
|
||||
@@ -11,12 +11,14 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
//userService class that gets used by the server to handle requests for users
|
||||
@Service
|
||||
public class UserService {
|
||||
Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
//userRepository to talk with the database
|
||||
|
||||
/**
|
||||
* registers the user.
|
||||
@@ -26,13 +28,16 @@ public class UserService {
|
||||
*/
|
||||
public UserDto registerUser(String name, String password) {
|
||||
User user = userRepository.findByName(name);
|
||||
//find the name of the user in the database
|
||||
if (user == null) {
|
||||
user = new User(null, name, password, 0);
|
||||
//if the user isn't already in the database, save it in there
|
||||
userRepository.save(user);
|
||||
} else {
|
||||
throw new ApplicationException("User already exists");
|
||||
}
|
||||
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
|
||||
//return a transferable user object that has been saved
|
||||
return new UserDto(user.getId(), user.getName(), user.getVeganMeal());
|
||||
}
|
||||
|
||||
@@ -44,13 +49,16 @@ public class UserService {
|
||||
*/
|
||||
public UserDto loginUser(String name, String password) {
|
||||
User user = userRepository.findByName(name);
|
||||
//again find the name
|
||||
if (user == null) {
|
||||
throw new ApplicationException("User does not exist");
|
||||
//if it doesn't exist or the password is wrong, throw an exception
|
||||
} else {
|
||||
if (!user.getPassword().equals(password)) {
|
||||
throw new ApplicationException("Wrong password");
|
||||
}
|
||||
}
|
||||
//return a transferable user object that has been logged in
|
||||
return new UserDto(user.getId(), user.getName(), user.getVeganMeal());
|
||||
}
|
||||
|
||||
@@ -62,8 +70,10 @@ public class UserService {
|
||||
public void addVeganMeal(Long id, String name) {
|
||||
User user = userRepository.findByName(name);
|
||||
int count = user.getVeganMeal();
|
||||
//find the user and update their vegetarian meal count
|
||||
count++;
|
||||
user.setVeganMeal(count);
|
||||
//save it to the database
|
||||
userRepository.save(user);
|
||||
logger.info("Added vegan meal to user(id=" + user.getId()
|
||||
+ ", name=" + user.getName() + ")");
|
||||
|
||||
Reference in New Issue
Block a user