diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index a47d358..89720f6 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -1,35 +1,43 @@ package greenify.server.data.model; import lombok.Data; -import lombok.NoArgsConstructor; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; +import javax.validation.constraints.NotNull; -//@AllArgsConstructor +@EnableAutoConfiguration @Entity @Data -@NoArgsConstructor +@Table(name = "users") public class User { - private @Id + @Id @GeneratedValue(strategy = GenerationType.AUTO) - Long id; + private Long id; + + @NotNull private String name; + + @NotNull private String password; + private int veganMeal; + + public User() {} + /** * makes a user object. * @param id the id of the user. * @param name the supplied username * @param password the supplied password + * @param veganMeal the supplied number of vegan meal */ - public User(Long id, String name, String password) { + public User(Long id, String name, String password, int veganMeal) { this.id = id; this.name = name; this.password = password; + this.veganMeal = veganMeal; } /** @@ -40,6 +48,8 @@ public class User { return id; } + public void setId(Long id) { this.id = id; } + /** * gets the name. * @return the name @@ -48,6 +58,8 @@ public class User { return name; } + public void setName(String name) { this.name = name; } + /** * gets the password. * @return the password @@ -56,21 +68,15 @@ public class User { return password; } - public void setId(Long id) { - this.id = id; + public void setPassword(String password) { this.password = password; } + + /** + * gets the number of vegan meal. + * @return the veganMeal + */ + public int getVeganMeal() { + return veganMeal; } - public void setName(String name) { - this.name = name; - } - - public void setPassword(String password) { - this.password = password; - } - - @Override - public String toString() { - - return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")"; - } -} \ No newline at end of file + public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } +} diff --git a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java index e98f956..5e7d511 100644 --- a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java +++ b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java @@ -1,8 +1,10 @@ package greenify.server.data.repository; +import org.springframework.data.repository.CrudRepository; import greenify.server.data.model.User; -public interface UserRepository { +public interface UserRepository extends CrudRepository { User findByName(String name); T save(T user); } + diff --git a/src/Server/src/main/java/greenify/server/rest/MainController.java b/src/Server/src/main/java/greenify/server/rest/MainController.java new file mode 100644 index 0000000..b985819 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/rest/MainController.java @@ -0,0 +1,38 @@ +package greenify.server.rest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +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) +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) { + // @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); + return "Saved"; + } + + @GetMapping(path="/all") + public @ResponseBody Iterable getAllUsers() { + // This returns a JSON or XML with the users + return userRepository.findAll(); + } +} diff --git a/src/Server/src/main/java/greenify/server/rest/UserController.java b/src/Server/src/main/java/greenify/server/rest/UserController.java index c80288a..83eee13 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -17,10 +17,4 @@ public class UserController { @RequestParam(value = "password") String password) { return userService.registerUser(name, password); } - - @RequestMapping("/login") - public UserDTO login(@RequestParam(value = "name") String name, - @RequestParam(value = "password") String password) { - return userService.login(name, password); - } } \ No newline at end of file diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 4756726..6f759ab 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -26,7 +26,7 @@ public class UserService { if (user != null) { throw new ApplicationException("User already exists"); } else { - user = userRepository.save(new User(null, name, password)); + 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()); @@ -38,15 +38,16 @@ public class UserService { * @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 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()); +// } } +