Merge branch 'master' into 'fixMaster'
This commit is contained in:
@@ -3,6 +3,7 @@ package greenify.server.data.repository;
|
||||
import greenify.server.data.model.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
//userRepository that saves all the user and talks to the database
|
||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||
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.RestControllerAdvice;
|
||||
|
||||
//class that handles exceptions for the rest server
|
||||
@RestControllerAdvice
|
||||
public class RestExceptionHandler {
|
||||
@ExceptionHandler(ApplicationException.class)
|
||||
|
||||
@@ -7,26 +7,43 @@ import greenify.server.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
//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
|
||||
public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
//registers a user in the userService
|
||||
@RequestMapping("/registerUser")
|
||||
//requestMapping is for the communication (GET, POST, PUT requests)
|
||||
//as with Web and Database Technology
|
||||
public UserDto registerUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
//the requestParams are the parameters that are sent with the request
|
||||
//so in this case that it wants to register with the name and password
|
||||
return userService.registerUser(name, password);
|
||||
}
|
||||
|
||||
//logs a user in in the userService
|
||||
@RequestMapping("/loginUser")
|
||||
public UserDto loginUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.loginUser(name, password);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* adds a vegetarian meal to the user.
|
||||
* @param id the id of the user
|
||||
* @param name thr username of the user
|
||||
*/
|
||||
@RequestMapping("/addVeganMeal")
|
||||
public void addVeganMeal(@RequestParam(value = "id") Long id,
|
||||
@RequestParam(value = "name") String name) {
|
||||
//here the requestParams are the id and name, because that is needed for the
|
||||
//addVeganMeal method of the userService
|
||||
userService.addVeganMeal(id, name);
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,14 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
//userService class that gets used by the server to handle requests for users
|
||||
@Service
|
||||
public class UserService {
|
||||
Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
//userRepository to talk with the database
|
||||
|
||||
/**
|
||||
* registers the user.
|
||||
@@ -26,13 +28,16 @@ public class UserService {
|
||||
*/
|
||||
public UserDto registerUser(String name, String password) {
|
||||
User user = userRepository.findByName(name);
|
||||
//find the name of the user in the database
|
||||
if (user == null) {
|
||||
user = new User(null, name, password, 0);
|
||||
//if the user isn't already in the database, save it in there
|
||||
userRepository.save(user);
|
||||
} else {
|
||||
throw new ApplicationException("User already exists");
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -44,13 +49,16 @@ public class UserService {
|
||||
*/
|
||||
public UserDto loginUser(String name, String password) {
|
||||
User user = userRepository.findByName(name);
|
||||
//again find the name
|
||||
if (user == null) {
|
||||
throw new ApplicationException("User does not exist");
|
||||
//if it doesn't exist or the password is wrong, throw an exception
|
||||
} else {
|
||||
if (!user.getPassword().equals(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());
|
||||
}
|
||||
|
||||
@@ -62,8 +70,10 @@ public class UserService {
|
||||
public void addVeganMeal(Long id, String name) {
|
||||
User user = userRepository.findByName(name);
|
||||
int count = user.getVeganMeal();
|
||||
//find the user and update their vegetarian meal count
|
||||
count++;
|
||||
user.setVeganMeal(count);
|
||||
//save it to the database
|
||||
userRepository.save(user);
|
||||
logger.info("Added vegan meal to user(id=" + user.getId()
|
||||
+ ", name=" + user.getName() + ")");
|
||||
|
||||
Reference in New Issue
Block a user