Add new classes for database
This commit is contained in:
@@ -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 + ")";
|
||||
}
|
||||
public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; }
|
||||
}
|
||||
@@ -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, Integer> {
|
||||
User findByName(String name);
|
||||
<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) {
|
||||
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) {
|
||||
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());
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user