Add new classes for database
This commit is contained in:
@@ -1,35 +1,43 @@
|
|||||||
package greenify.server.data.model;
|
package greenify.server.data.model;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.*;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
|
|
||||||
//@AllArgsConstructor
|
@EnableAutoConfiguration
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@Table(name = "users")
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
private @Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
Long id;
|
private Long id;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
private int veganMeal;
|
||||||
|
|
||||||
|
public User() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* makes a user object.
|
* makes a user object.
|
||||||
* @param id the id of the user.
|
* @param id the id of the user.
|
||||||
* @param name the supplied username
|
* @param name the supplied username
|
||||||
* @param password the supplied password
|
* @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.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
|
this.veganMeal = veganMeal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,6 +48,8 @@ public class User {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) { this.id = id; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the name.
|
* gets the name.
|
||||||
* @return the name
|
* @return the name
|
||||||
@@ -48,6 +58,8 @@ public class User {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setName(String name) { this.name = name; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the password.
|
* gets the password.
|
||||||
* @return the password
|
* @return the password
|
||||||
@@ -56,21 +68,15 @@ public class User {
|
|||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(Long id) {
|
public void setPassword(String password) { this.password = password; }
|
||||||
this.id = id;
|
|
||||||
|
/**
|
||||||
|
* gets the number of vegan meal.
|
||||||
|
* @return the veganMeal
|
||||||
|
*/
|
||||||
|
public int getVeganMeal() {
|
||||||
|
return veganMeal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; }
|
||||||
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 + ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package greenify.server.data.repository;
|
package greenify.server.data.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
import greenify.server.data.model.User;
|
import greenify.server.data.model.User;
|
||||||
|
|
||||||
public interface UserRepository {
|
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||||
User findByName(String name);
|
User findByName(String name);
|
||||||
<T extends User> T save(T user);
|
<T extends User> T save(T user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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<User> getAllUsers() {
|
||||||
|
// This returns a JSON or XML with the users
|
||||||
|
return userRepository.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,10 +17,4 @@ public class UserController {
|
|||||||
@RequestParam(value = "password") String password) {
|
@RequestParam(value = "password") String password) {
|
||||||
return userService.registerUser(name, 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ public class UserService {
|
|||||||
if (user != null) {
|
if (user != null) {
|
||||||
throw new ApplicationException("User already exists");
|
throw new ApplicationException("User already exists");
|
||||||
} else {
|
} 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());
|
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
|
||||||
return new UserDTO(user.getId(), user.getName());
|
return new UserDTO(user.getId(), user.getName());
|
||||||
@@ -38,15 +38,16 @@ public class UserService {
|
|||||||
* @param password the password of the user
|
* @param password the password of the user
|
||||||
* @return a userDTO of the logged in user
|
* @return a userDTO of the logged in user
|
||||||
*/
|
*/
|
||||||
public UserDTO login(String name, String password) {
|
// public UserDTO login(String name, String password) {
|
||||||
User user = userRepository.findByName(name);
|
// User user = userRepository.findByName(name);
|
||||||
if (user == null) {
|
// if (user == null) {
|
||||||
throw new ApplicationException("User does not exist");
|
// throw new ApplicationException("User does not exist");
|
||||||
} else {
|
// } else {
|
||||||
if (!user.getPassword().equals(password)) {
|
// if (!user.getPassword().equals(password)) {
|
||||||
throw new ApplicationException("Wrong password");
|
// throw new ApplicationException("Wrong password");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return new UserDTO(user.getId(), user.getName());
|
// return new UserDTO(user.getId(), user.getName());
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user