Merge branch 'master' of gitlab.ewi.tudelft.nl:cse1105/2018-2019/oopp-group-43/template
This commit is contained in:
@@ -17,4 +17,16 @@ public class UserController {
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.registerUser(name, password);
|
||||
}
|
||||
|
||||
@RequestMapping("/loginUser")
|
||||
public UserDTO loginUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.loginUser(name, password);
|
||||
}
|
||||
|
||||
@RequestMapping("/addVeganMeal")
|
||||
public void addVeganMeal(@RequestParam(value = "id") Long id,
|
||||
@RequestParam(value = "name") String name) {
|
||||
userService.addVeganMeal(id, name);
|
||||
}
|
||||
}
|
||||
@@ -32,22 +32,37 @@ public class UserService {
|
||||
return new UserDTO(user.getId(), user.getName());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* logs the user in.
|
||||
* @param name the username of the user
|
||||
* @param password the password of the user
|
||||
* @return a userDTO of the logged in user
|
||||
*/
|
||||
// public UserDTO login(String name, String password) {
|
||||
// User user = userRepository.findByName(name);
|
||||
// if (user == null) {
|
||||
// throw new ApplicationException("User does not exist");
|
||||
// } else {
|
||||
// if (!user.getPassword().equals(password)) {
|
||||
// throw new ApplicationException("Wrong password");
|
||||
// }
|
||||
// }
|
||||
// return new UserDTO(user.getId(), user.getName());
|
||||
// }
|
||||
public UserDTO loginUser(String name, String password) {
|
||||
User user = userRepository.findByName(name);
|
||||
if (user == null) {
|
||||
throw new ApplicationException("User does not exist");
|
||||
} else {
|
||||
if (!user.getPassword().equals(password)) {
|
||||
throw new ApplicationException("Wrong password");
|
||||
}
|
||||
}
|
||||
return new UserDTO(user.getId(), user.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
logger.info("Added vegan meal to user(id=" + user.getId() + ", name=" + user.getName() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user