Update changes
This commit is contained in:
@@ -12,26 +12,33 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application extends javafx.application.Application {
|
||||
private ConfigurableApplicationContext springContext;
|
||||
private Parent rootNode;
|
||||
private FXMLLoader fxmlLoader;
|
||||
private static ConfigurableApplicationContext springContext;
|
||||
private static final Logger log = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
|
||||
public static Parent load(java.net.URL url) {
|
||||
FXMLLoader loader = new FXMLLoader();
|
||||
loader.setControllerFactory(springContext::getBean);
|
||||
loader.setLocation(url);
|
||||
try {
|
||||
return loader.load();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
springContext = SpringApplication.run(Application.class);
|
||||
fxmlLoader = new FXMLLoader();
|
||||
fxmlLoader.setControllerFactory(springContext::getBean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
fxmlLoader.setLocation(this.getClass().getClassLoader().getResource("fxml/sample.fxml"));
|
||||
rootNode = fxmlLoader.load();
|
||||
Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/sample.fxml"));
|
||||
primaryStage.setTitle("Greenify");
|
||||
Scene scene = new Scene(rootNode);
|
||||
primaryStage.setScene(scene);
|
||||
|
||||
@@ -6,6 +6,7 @@ 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
|
||||
@@ -13,6 +14,8 @@ public class DashBoardController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
private int count = 0;
|
||||
|
||||
@FXML
|
||||
public AnchorPane menuBar;
|
||||
public AnchorPane dashboardPane;
|
||||
@@ -66,8 +69,8 @@ public class DashBoardController {
|
||||
public void addVeganMeal(ActionEvent event) {
|
||||
count++;
|
||||
counter.setText("Count: " + count);
|
||||
UserService service = new UserService();
|
||||
service.addVeganMeal(null, null);
|
||||
System.out.println(userService);
|
||||
userService.addVeganMeal(userService.currentUser.getId(), userService.currentUser.getName());
|
||||
System.out.println("Vegetarian meal is added");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package greenify.client.controller;
|
||||
|
||||
import greenify.client.Application;
|
||||
import greenify.client.rest.UserService;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
@@ -64,9 +65,7 @@ public class UserController {
|
||||
* @author sem
|
||||
*/
|
||||
public void openDashboard() throws IOException {
|
||||
Parent dash = FXMLLoader.load(
|
||||
this.getClass().getClassLoader().getResource("fxml/dashboard.fxml")
|
||||
);
|
||||
Parent dash = Application.load (this.getClass().getClassLoader().getResource("fxml/dashboard.fxml"));
|
||||
Scene scene = new Scene(dash);
|
||||
scene.getStylesheets().add(getClass().getClassLoader().getResource("stylesheets/dashboardStyle.css").toExternalForm());
|
||||
Stage appStage = new Stage();
|
||||
@@ -97,14 +96,12 @@ public class UserController {
|
||||
|
||||
public void handleRegisterButtonAction(ActionEvent event) throws Exception{
|
||||
//load the fxml file
|
||||
Parent registerWindow = FXMLLoader.load (
|
||||
this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml")
|
||||
);
|
||||
Parent registerWindow = Application.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();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ public class UserService {
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
public UserDTO currentUser;
|
||||
|
||||
@Bean
|
||||
RestTemplate restTemplate(RestTemplateBuilder builder) {
|
||||
return builder.build();
|
||||
@@ -34,6 +36,44 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* sign ins the user.
|
||||
* @param name the username of the user
|
||||
* @param password the password of the user
|
||||
* @return a userDTO
|
||||
*/
|
||||
public UserDTO loginUser(String name, String password) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/loginUser")
|
||||
.queryParam("name", name)
|
||||
.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;
|
||||
}
|
||||
|
||||
/**
|
||||
* a user adds vegan meal.
|
||||
* @param id the id of the user
|
||||
* @param name the username of the user
|
||||
* @return a userDTO
|
||||
*/
|
||||
public UserDTO addVeganMeal(Long id, String name) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addVeganMeal")
|
||||
.queryParam("id", id)
|
||||
.queryParam("name", name);
|
||||
HttpEntity<?> entity = new HttpEntity<>(headers);
|
||||
System.out.println(builder.build().encode().toUri());
|
||||
return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
logging.level.org.springframework.beans.factory=DEBUG
|
||||
0
src/Client/src/test/java/ApplicationTest.java
Normal file
0
src/Client/src/test/java/ApplicationTest.java
Normal file
@@ -21,7 +21,7 @@ public class UserServiceTest {
|
||||
UserService userService;
|
||||
|
||||
@Test
|
||||
public void mocking() throws Exception {
|
||||
public void userRegisterTest() throws Exception {
|
||||
UserDTO testUser = new UserDTO(1L, "Eric");
|
||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
|
||||
UserDTO.class))
|
||||
@@ -30,6 +30,26 @@ public class UserServiceTest {
|
||||
UserDTO user = userService.registerUser("Eric", "password");
|
||||
Assert.assertEquals(testUser, user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userLoginTest() throws Exception {
|
||||
UserDTO testUser = new UserDTO(1L, "Eric");
|
||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
|
||||
UserDTO.class))
|
||||
.thenReturn(testUser);
|
||||
UserDTO user = userService.loginUser("Eric", "password");
|
||||
Assert.assertEquals(testUser, user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addVeganMealTest() throws Exception {
|
||||
UserDTO testUser = new UserDTO(1L, "Eric");
|
||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/addVeganMeal?id=1&name=Eric"),
|
||||
UserDTO.class))
|
||||
.thenReturn(testUser);
|
||||
UserDTO user = userService.addVeganMeal(1L, "Eric");
|
||||
Assert.assertEquals(testUser, user);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user