Merge branch 'Develop_GUI_further' into master

# Conflicts:
#	src/.idea/workspace.xml
#	src/Client/src/main/java/greenify/client/controller/DashBoardController.java
#	src/Client/src/main/java/greenify/client/controller/UserController.java
#	src/Client/src/main/java/greenify/client/rest/UserService.java
#	src/Server/src/main/java/greenify/server/data/model/User.java
#	src/Server/src/main/java/greenify/server/data/repository/UserRepository.java
#	src/Server/src/main/java/greenify/server/rest/UserController.java
#	src/Server/src/main/java/greenify/server/service/UserService.java

resolved conflicts manually, used the ones from 'Develop_GUI_further'
This commit is contained in:
Sem van der Hoeven
2019-03-17 17:52:25 +01:00
18 changed files with 213 additions and 323 deletions

View File

@@ -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;
}
}

View File

@@ -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, Integer> {
// User findByName(@Param("name") String name);
User findByName(String name);
User findById(Long id);
<T extends User> T save(T user);
}

View File

@@ -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<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();

View File

@@ -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<User> 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);
}
}

View File

@@ -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;
}
}