ADD::added comments to most classes to make it more understandable

This commit is contained in:
Sem van der Hoeven
2019-03-18 17:07:38 +01:00
parent ecdc9f3a4f
commit b8f4415825
11 changed files with 122 additions and 9 deletions

View File

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

View File

@@ -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)

View File

@@ -7,23 +7,28 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
//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")
public UserDto registerUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String 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 vegan meal to the user
@RequestMapping("/addVeganMeal")
public void addVeganMeal(@RequestParam(value = "id") Long id,
@RequestParam(value = "name") String name) {

View File

@@ -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() + ")");