Merge branch 'master' of gitlab.ewi.tudelft.nl:cse1105/2018-2019/oopp-group-43/template
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
package greenify.client.controller;
|
package greenify.client.controller;
|
||||||
|
|
||||||
|
import greenify.client.Application;
|
||||||
import greenify.client.rest.UserService;
|
import greenify.client.rest.UserService;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.FXMLLoader;
|
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.Alert;
|
import javafx.scene.control.Alert;
|
||||||
@@ -23,15 +23,9 @@ public class UserController {
|
|||||||
UserService userService;
|
UserService userService;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public TextField usernameField;
|
private TextField usernameField;
|
||||||
|
|
||||||
@FXML
|
|
||||||
private PasswordField passwordField;
|
private PasswordField passwordField;
|
||||||
|
|
||||||
@FXML
|
|
||||||
private Button loginButton;
|
private Button loginButton;
|
||||||
|
|
||||||
@FXML
|
|
||||||
private Button signupButton;
|
private Button signupButton;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -64,12 +58,10 @@ public class UserController {
|
|||||||
* @author sem
|
* @author sem
|
||||||
*/
|
*/
|
||||||
public void openDashboard() throws IOException {
|
public void openDashboard() throws IOException {
|
||||||
Parent dash = FXMLLoader.load(
|
Parent dash = Application.load(this.getClass().getClassLoader()
|
||||||
this.getClass().getClassLoader().getResource("fxml/dashboard.fxml")
|
.getResource("fxml/dashboard.fxml"));
|
||||||
);
|
|
||||||
Scene scene = new Scene(dash);
|
Scene scene = new Scene(dash);
|
||||||
scene.getStylesheets().add(getClass()
|
scene.getStylesheets().add(getClass().getClassLoader()
|
||||||
.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);
|
||||||
@@ -98,21 +90,16 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handles the click of the 'sign up' button.
|
* The method handles register button.
|
||||||
* opens a new stage where the user can register.
|
* @param event User clicks to the button
|
||||||
* @param event the click of the button
|
* @throws Exception when the file couldn't find
|
||||||
* @throws Exception an exception if the fxml file is not found
|
|
||||||
*/
|
*/
|
||||||
public void handleRegisterButtonAction(ActionEvent event) throws Exception {
|
public void handleRegisterButtonAction(ActionEvent event) throws Exception {
|
||||||
//load the fxml file
|
Parent registerWindow = Application.load(this.getClass().getClassLoader()
|
||||||
Parent registerWindow = FXMLLoader.load(
|
.getResource("fxml/RegisterWindow.fxml"));
|
||||||
this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml")
|
Scene registerScene = new Scene(registerWindow);
|
||||||
);
|
|
||||||
//make the window use the scene
|
|
||||||
Scene registerscene = new Scene(registerWindow);
|
|
||||||
Stage registerStage = new Stage();
|
Stage registerStage = new Stage();
|
||||||
//open the window
|
registerStage.setScene(registerScene);
|
||||||
registerStage.setScene(registerscene);
|
|
||||||
registerStage.setTitle("Enter register credentials");
|
registerStage.setTitle("Enter register credentials");
|
||||||
registerStage.show();
|
registerStage.show();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import org.springframework.web.util.UriComponentsBuilder;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
RestTemplate restTemplate;
|
RestTemplate restTemplate;
|
||||||
|
|
||||||
@@ -28,10 +27,9 @@ public class UserService {
|
|||||||
* registers the user.
|
* registers the user.
|
||||||
* @param name the username of the user
|
* @param name the username of the user
|
||||||
* @param password the password of the user
|
* @param password the password of the user
|
||||||
* @return a built userDTO with the required information
|
* @return a userDTO
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("Duplicates")
|
public UserDto registerUser(String name, String password) {
|
||||||
public UserDTO registerUser(String name, String password) {
|
|
||||||
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/registerUser")
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/registerUser")
|
||||||
@@ -46,13 +44,12 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* logd the user in.
|
* sign ins the user.
|
||||||
* @param name the username of the user
|
* @param name the username of the user
|
||||||
* @param password the password of the user
|
* @param password the password of the user
|
||||||
* @return a built userDTO with the required information
|
* @return a userDTO
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("Duplicates")
|
public UserDto loginUser(String name, String password) {
|
||||||
public UserDTO loginUser(String name, String password) {
|
|
||||||
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")
|
||||||
@@ -67,13 +64,12 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds a vegetarian meal to the user.
|
* a user adds vegan meal.
|
||||||
* @param id the id of the user
|
* @param id the id of the user
|
||||||
* @param name the username of the user
|
* @param name the username of the user
|
||||||
* @return a built userDTO with the required information
|
* @return a userDTO
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("Duplicates")
|
public UserDto addVeganMeal(Long id, String name) {
|
||||||
public UserDTO addVeganMeal(Long id, String name) {
|
|
||||||
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")
|
||||||
@@ -83,20 +79,4 @@ public class UserService {
|
|||||||
System.out.println(builder.build().encode().toUri());
|
System.out.println(builder.build().encode().toUri());
|
||||||
return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import java.lang.*?>
|
|
||||||
<?import javafx.scene.image.*?>
|
|
||||||
<?import javafx.scene.shape.*?>
|
|
||||||
<?import javafx.scene.control.*?>
|
<?import javafx.scene.control.*?>
|
||||||
<?import javafx.scene.image.Image?>
|
<?import javafx.scene.image.Image?>
|
||||||
<?import javafx.scene.image.ImageView?>
|
<?import javafx.scene.image.ImageView?>
|
||||||
<?import javafx.scene.layout.*?>
|
<?import javafx.scene.layout.*?>
|
||||||
<?import javafx.scene.shape.Line?>
|
<?import javafx.scene.shape.Line?>
|
||||||
<?import javafx.scene.text.*?>
|
<?import javafx.scene.text.*?>
|
||||||
|
|
||||||
<AnchorPane prefHeight="602.0" prefWidth="934.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.DashBoardController">
|
<AnchorPane prefHeight="602.0" prefWidth="934.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.DashBoardController">
|
||||||
<children>
|
<children>
|
||||||
<AnchorPane fx:id="menuBar" prefHeight="603.0" prefWidth="216.0" style="-fx-background-color: #5a635c;">
|
<AnchorPane fx:id="menuBar" prefHeight="603.0" prefWidth="216.0" style="-fx-background-color: #5a635c;">
|
||||||
@@ -39,84 +35,21 @@
|
|||||||
</children></AnchorPane>
|
</children></AnchorPane>
|
||||||
<AnchorPane fx:id="activitiesPane" layoutX="214.0" prefHeight="603.0" prefWidth="711.0" visible="false">
|
<AnchorPane fx:id="activitiesPane" layoutX="214.0" prefHeight="603.0" prefWidth="711.0" visible="false">
|
||||||
<children>
|
<children>
|
||||||
<Text fill="#002c0c" layoutX="101.0" layoutY="74.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Available Activities" AnchorPane.leftAnchor="60.0" AnchorPane.topAnchor="40.0">
|
<Text layoutX="101.0" layoutY="74.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Available Activities" AnchorPane.leftAnchor="60.0" AnchorPane.topAnchor="40.0">
|
||||||
<font>
|
<font>
|
||||||
<Font size="30.0" />
|
<Font size="30.0" />
|
||||||
</font>
|
</font>
|
||||||
</Text>
|
</Text>
|
||||||
<Pane layoutX="60.0" layoutY="93.0" prefHeight="471.0" prefWidth="291.0" style="-fx-background-color: #f4fff4;">
|
<Button fx:id="veganMealButton" layoutX="60.0" layoutY="116.0" mnemonicParsing="false" style="-fx-background-color: #009623; -fx-border-radius: 25px;" onAction="#addVeganMeal" text="Add a vegetarian meal" textFill="#e0ffe1">
|
||||||
<children>
|
|
||||||
<Button fx:id="veganMealButton" layoutX="60.0" layoutY="55.0" mnemonicParsing="false" onAction="#addVeganMeal" style="-fx-background-color: #009623; -fx-border-radius: 25px;" text="Eating a vegetarian meal" textFill="#e0ffe1">
|
|
||||||
<font>
|
<font>
|
||||||
<Font size="14.0" />
|
<Font size="14.0" />
|
||||||
</font>
|
</font>
|
||||||
</Button>
|
</Button>
|
||||||
<Button fx:id="localProduceButton" layoutX="71.0" layoutY="100.0" mnemonicParsing="false" style="-fx-background-color: #009623; -fx-border-radius: 25px;" text="Buying local produce" textFill="#e0ffe1">
|
<Label fx:id="counter" layoutX="288.0" layoutY="110.0" prefHeight="44.0" prefWidth="115.0" text="Count: ">
|
||||||
<font>
|
<font>
|
||||||
<Font size="14.0" />
|
<Font size="25.0" />
|
||||||
</font>
|
|
||||||
</Button>
|
|
||||||
<Button fx:id="bikeButton" layoutX="48.0" layoutY="145.0" mnemonicParsing="false" style="-fx-background-color: #009623; -fx-border-radius: 25px;" text="Using a bike instead of a car" textFill="#e0ffe1">
|
|
||||||
<font>
|
|
||||||
<Font size="14.0" />
|
|
||||||
</font>
|
|
||||||
</Button>
|
|
||||||
<Button fx:id="publicTransportButton" layoutX="18.0" layoutY="190.0" mnemonicParsing="false" style="-fx-background-color: #009623; -fx-border-radius: 25px;" text="Using public transport instead of a car" textFill="#e0ffe1">
|
|
||||||
<font>
|
|
||||||
<Font size="14.0" />
|
|
||||||
</font>
|
|
||||||
</Button>
|
|
||||||
<Button fx:id="temperatureButton" layoutX="31.0" layoutY="235.0" mnemonicParsing="false" style="-fx-background-color: #009623; -fx-border-radius: 25px;" text="Lowering your home temperature" textFill="#e0ffe1">
|
|
||||||
<font>
|
|
||||||
<Font size="14.0" />
|
|
||||||
</font>
|
|
||||||
</Button>
|
|
||||||
<Button fx:id="solarPanelButton" layoutX="68.0" layoutY="280.0" mnemonicParsing="false" style="-fx-background-color: #009623; -fx-border-radius: 25px;" text="Installing solar panels" textFill="#e0ffe1">
|
|
||||||
<font>
|
|
||||||
<Font size="14.0" />
|
|
||||||
</font>
|
|
||||||
</Button>
|
|
||||||
</children>
|
|
||||||
</Pane>
|
|
||||||
<Pane layoutX="372.0" layoutY="93.0" prefHeight="471.0" prefWidth="291.0" style="-fx-background-color: #f4fff4;">
|
|
||||||
<children>
|
|
||||||
<Text layoutX="78.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Activities done">
|
|
||||||
<font>
|
|
||||||
<Font size="21.0" />
|
|
||||||
</font>
|
|
||||||
</Text>
|
|
||||||
<Label fx:id="veganMealCounter" alignment="CENTER" contentDisplay="CENTER" layoutX="88.0" layoutY="55.0" prefHeight="30.0" prefWidth="115.0" text="0">
|
|
||||||
<font>
|
|
||||||
<Font size="18.0" />
|
|
||||||
</font>
|
</font>
|
||||||
</Label>
|
</Label>
|
||||||
<Label fx:id="localProduceCounter" alignment="CENTER" contentDisplay="CENTER" layoutX="88.0" layoutY="100.0" prefHeight="30.0" prefWidth="115.0" text="0">
|
|
||||||
<font>
|
|
||||||
<Font size="18.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
<Label fx:id="bikeCounter" alignment="CENTER" contentDisplay="CENTER" layoutX="88.0" layoutY="145.0" prefHeight="30.0" prefWidth="115.0" text="0">
|
|
||||||
<font>
|
|
||||||
<Font size="18.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
<Label fx:id="publicTransportCounter" alignment="CENTER" contentDisplay="CENTER" layoutX="88.0" layoutY="190.0" prefHeight="30.0" prefWidth="115.0" text="0">
|
|
||||||
<font>
|
|
||||||
<Font size="18.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
<Label fx:id="temperatureCounter" alignment="CENTER" contentDisplay="CENTER" layoutX="88.0" layoutY="235.0" prefHeight="30.0" prefWidth="115.0" text="0">
|
|
||||||
<font>
|
|
||||||
<Font size="18.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
<Label fx:id="solarPanelCounter" alignment="CENTER" contentDisplay="CENTER" layoutX="88.0" layoutY="280.0" prefHeight="30.0" prefWidth="115.0" text="0">
|
|
||||||
<font>
|
|
||||||
<Font size="18.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
</children>
|
|
||||||
</Pane>
|
|
||||||
</children></AnchorPane>
|
</children></AnchorPane>
|
||||||
<AnchorPane fx:id="userPane" layoutX="215.0" layoutY="-1.0" prefHeight="603.0" prefWidth="711.0" visible="false">
|
<AnchorPane fx:id="userPane" layoutX="215.0" layoutY="-1.0" prefHeight="603.0" prefWidth="711.0" visible="false">
|
||||||
<children>
|
<children>
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
package greenify.common;
|
|
||||||
|
|
||||||
public class ActivityDTO {
|
|
||||||
private Long id;
|
|
||||||
private String name;
|
|
||||||
private String description;
|
|
||||||
private int score;
|
|
||||||
|
|
||||||
public ActivityDTO() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public ActivityDTO(Long id, String name, String description, int score) {
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
this.description = description;
|
|
||||||
this.score= score;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getScore() {
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
57
src/Common/src/main/java/greenify/common/ActivityDto.java
Normal file
57
src/Common/src/main/java/greenify/common/ActivityDto.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package greenify.common;
|
||||||
|
|
||||||
|
public class ActivityDto {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private int score;
|
||||||
|
|
||||||
|
public ActivityDto() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for ActivityDto.
|
||||||
|
* @param id of the activity
|
||||||
|
* @param name of the activity
|
||||||
|
* @param description of the activity
|
||||||
|
* @param score of the activity
|
||||||
|
*/
|
||||||
|
public ActivityDto(Long id, String name, String description, int score) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getScore() {
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(int score) {
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/Common/src/test/java/ActivityDtoTest.java
Normal file
31
src/Common/src/test/java/ActivityDtoTest.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import greenify.common.ActivityDto;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ActivityDtoTest {
|
||||||
|
@Test
|
||||||
|
public void setAndGetTest() {
|
||||||
|
ActivityDto testActivity = new ActivityDto();
|
||||||
|
testActivity.setId(1L);
|
||||||
|
testActivity.setName("eatVeganMeal");
|
||||||
|
testActivity.setDescription("User adds a vegan meal");
|
||||||
|
testActivity.setScore(10);
|
||||||
|
ActivityDto activity = new ActivityDto(1L, "eatVeganMeal", "User adds a vegan meal", 10);
|
||||||
|
assertTrue(activity.getId() == 1L);
|
||||||
|
assertEquals(activity.getName(), "eatVeganMeal");
|
||||||
|
assertEquals(activity.getDescription(), "User adds a vegan meal");
|
||||||
|
assertEquals(activity.getScore(), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void equalsTest() {
|
||||||
|
ActivityDto first = new ActivityDto(1L, "eatVeganMeal", "User adds a vegan meal", 10);
|
||||||
|
ActivityDto second = new ActivityDto(1L, "eatVeganMeal", "User adds a vegan meal", 10);
|
||||||
|
assertEquals(first.getId(), second.getId());
|
||||||
|
assertEquals(first.getName(), second.getName());
|
||||||
|
assertEquals(first.getDescription(), second.getDescription());
|
||||||
|
assertEquals(first.getScore(), second.getScore());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,15 +3,17 @@ package greenify.server.data.model;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@@ -74,7 +76,9 @@ public class User {
|
|||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPassword(String password) { this.password = password; }
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the number of vegan meal.
|
* gets the number of vegan meal.
|
||||||
@@ -84,58 +88,7 @@ public class User {
|
|||||||
return veganMeal;
|
return veganMeal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVeganMeal(int veganMeal) { this.veganMeal = 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,8 @@ import greenify.server.data.model.User;
|
|||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||||
// User findByName(@Param("name") String name);
|
|
||||||
User findByName(String name);
|
User findByName(String name);
|
||||||
|
|
||||||
User findById(Long id);
|
|
||||||
|
|
||||||
<T extends User> T save(T user);
|
<T extends User> T save(T user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ public class UserController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
UserService userService;
|
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")
|
@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) {
|
||||||
@@ -34,11 +38,6 @@ public class UserController {
|
|||||||
userService.addVeganMeal(id, name);
|
userService.addVeganMeal(id, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/getUsername")
|
|
||||||
public void getUsername(@RequestParam(value = "id") Long id) {
|
|
||||||
userService.getUsername(id);
|
|
||||||
|
|
||||||
|
|
||||||
@GetMapping(path = "/all")
|
@GetMapping(path = "/all")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Iterable<User> getAllUsers() {
|
public Iterable<User> getAllUsers() {
|
||||||
|
|||||||
@@ -23,16 +23,15 @@ 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);
|
||||||
if (user != null) {
|
if (user == null) {
|
||||||
throw new ApplicationException("User already exists");
|
|
||||||
} else {
|
|
||||||
user = userRepository.save(new User(null, name, password, 0));
|
user = userRepository.save(new User(null, name, password, 0));
|
||||||
|
} else {
|
||||||
|
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 new UserDto(user.getId(), user.getName());
|
return new UserDto(user.getId(), user.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* logs the user in.
|
* logs the user in.
|
||||||
* @param name the username of the user
|
* @param name the username of the user
|
||||||
@@ -61,20 +60,9 @@ public class UserService {
|
|||||||
int count = user.getVeganMeal();
|
int count = user.getVeganMeal();
|
||||||
count++;
|
count++;
|
||||||
user.setVeganMeal(count);
|
user.setVeganMeal(count);
|
||||||
logger.info("Added vegan meal to user(id="
|
userRepository.save(user);
|
||||||
+ 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user