add new user to initializer, refactor listUsersJson :)

This commit is contained in:
Marcin Michałek committed 2015-02-12 08:47:51 +01:00
1 parent 0bc44cf04e
commit 06621727e8
2 files changed
+11 -31

No files matched your search

@@ -15,7 +15,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.persistence.EntityManager;
@@ -197,36 +196,8 @@ public class UserController {
@RequestMapping
public
@ResponseBody
String listUsersJson(ModelMap model) throws JSONException {
JSONArray userArray = new JSONArray();
for (User user : userRepository.findAll()) {
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);
}
JSONArray messageArray = new JSONArray();
for(Message m:user.getMessages()){
JSONObject messJSON = new JSONObject();
messJSON.put("dataTime", m.getDateTime());
messJSON.put("message", m.getMessage());
messJSON.put("isRead", m.isRead());
messageArray.put(messJSON);
}
userJSON.put("messages", messageArray);
userArray.put(userJSON);
}
return userArray.toString();
Iterable<User> listUsersJson() throws JSONException {
return userRepository.findAll();
}
@RequestMapping("/remove/{userId}")
@@ -45,6 +45,15 @@ public class Initializer {
marcin.addMessage(new Message("You're fired"));
userRepository.save(marcin);
User lukasz = new User("Lukasz", "Golinski", "l", "p");
Set<Role> roles2 = new HashSet<Role>();
roles2.add(new Role("NERD", "greatJob"));
roles2.add(new Role("BOSS", "fuckYea"));
lukasz.setRoles(roles2);
lukasz.addMessage(new Message("hello2"));
lukasz.addMessage(new Message("You're fired"));
userRepository.save(lukasz);
DoorMessage message=new DoorMessage("Be Careful");
doorRepository.save(new Door(message));
}