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