refactoring
This commit is contained in:
1 parent
abbac0b416
commit
2df87040ed
11 files changed
+129
-256
No files matched your search
@@ -11,15 +11,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 21.04.14.
|
||||
@@ -38,81 +35,32 @@ public class UserController {
|
||||
|
||||
@RequestMapping(value = "/add")
|
||||
@ResponseBody
|
||||
public String addUser(@RequestParam String username) {
|
||||
User user = new User();
|
||||
//powinno mapowac obiekt json na obiekt user
|
||||
public String addUser(@RequestBody User user) {
|
||||
/*User user = new User();
|
||||
user.setFirstName(username);
|
||||
Set<Role> roles = new HashSet<Role>();
|
||||
roles.add(new Role("lala","lala"));
|
||||
roles.add(new Role("lala1","lala1"));
|
||||
user.setRoles(roles);
|
||||
user.setRoles(roles);*/
|
||||
userRepository.save(user);
|
||||
return "saved user: "+user.getUserId();
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/{userId}")
|
||||
public String getUser(@PathVariable("userId") Long userId) {
|
||||
public String getUser(@PathVariable("userId") Long userId) throws JSONException{
|
||||
User user = userRepository.findOne(userId);
|
||||
return "user: "+user.toString();
|
||||
JSONObject userJSON = new JSONObject();
|
||||
userJSON.put("id", user.getUserId());
|
||||
userJSON.put("firstname", user.getFirstName());
|
||||
userJSON.put("lastname", user.getLastName());
|
||||
userJSON.put("email", user.getEmail());
|
||||
JSONArray userArray = new JSONArray();
|
||||
userArray.put(userJSON);
|
||||
return userArray.toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/init")
|
||||
public
|
||||
@ResponseBody
|
||||
String initialise() {
|
||||
|
||||
Role ceo = new Role("CEO","101010");
|
||||
Role worker = new Role("Worker","12345");
|
||||
roleRepository.save(new Role("Cleaner","54321"));
|
||||
ceo = roleRepository.save(ceo);
|
||||
|
||||
|
||||
User user1 = new User("Łukasz", "Goliński", "boss@nfc.pl");
|
||||
user1.addRole(worker);
|
||||
user1.addRole(ceo);
|
||||
userRepository.save(user1);
|
||||
|
||||
|
||||
ArrayList<Role> roles = (ArrayList<Role>) roleRepository.findAll();
|
||||
|
||||
for(Role r: roles){
|
||||
System.out.println(r.getRoleName());
|
||||
}
|
||||
|
||||
Role r = roles.get(0);
|
||||
|
||||
User user3 = new User("Monika", "Bigas", "boss_monika@nfc.pl");
|
||||
user3.addRole(worker);
|
||||
user3.addRole(ceo);
|
||||
userRepository.save(user3);
|
||||
//entityManager.merge(user3);
|
||||
|
||||
User user2 = new User("Marcin", "Michałek", "boss_marcin@nfc.pl");
|
||||
user2.addRole(ceo);
|
||||
userRepository.save(user2);
|
||||
|
||||
|
||||
|
||||
|
||||
ArrayList<User> users = (ArrayList<User>) userRepository.findAll();
|
||||
for(User u: users){
|
||||
System.out.println(u.getFirstName()+" "+u.getLastName());
|
||||
}
|
||||
|
||||
for (User user : userRepository.findAll()) {
|
||||
System.out.println("USER: " + user.getFirstName() + " " + user.getLastName() + " " + user.getEmail());
|
||||
System.out.println("roles: ");
|
||||
for (Role role : user.getRoles()) {
|
||||
System.out.println(" -" + role.getRoleName());
|
||||
}
|
||||
System.out.println("enduser");
|
||||
System.out.println("");
|
||||
}
|
||||
return "initialised";
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/all")
|
||||
@RequestMapping
|
||||
public
|
||||
@ResponseBody
|
||||
String listUsersJson(ModelMap model) throws JSONException {
|
||||
@@ -122,6 +70,17 @@ public class UserController {
|
||||
JSONObject userJSON = new JSONObject();
|
||||
userJSON.put("id", user.getUserId());
|
||||
userJSON.put("firstname", user.getFirstName());
|
||||
userJSON.put("lastname", user.getLastName());
|
||||
userJSON.put("email", user.getEmail());
|
||||
JSONArray roleArray = new JSONArray();
|
||||
for(Role r:user.getRoles()){
|
||||
JSONObject roleJSON = new JSONObject();
|
||||
roleJSON.put("id", r.getRoleId());
|
||||
roleJSON.put("name", r.getRoleName());
|
||||
roleJSON.put("password", r.getPassword());
|
||||
roleArray.put(roleJSON);
|
||||
}
|
||||
userJSON.put("roles", roleArray);
|
||||
userArray.put(userJSON);
|
||||
}
|
||||
return userArray.toString();
|
||||
|
||||
Reference in new issue
Block a user