package application.controllers; import application.model.Message; import application.model.Role; import application.model.User; import application.repositories.RoleRepository; import application.repositories.UserRepository; import org.json.JSONArray; 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; import javax.persistence.PersistenceContext; import java.util.HashSet; import java.util.Set; /** * Created by lukasgol on 21.04.14. */ @Controller @RequestMapping(value="/users") public class UserController { @Autowired private UserRepository userRepository; @Autowired private RoleRepository roleRepository; @PersistenceContext private EntityManager entityManager; @RequestMapping(value = "/add") @ResponseBody //powinno mapowac obiekt json na obiekt user public String addUser(@RequestBody User user) { /*User user = new User(); user.setFirstName(username); Set roles = new HashSet(); roles.add(new Role("lala","lala")); roles.add(new Role("lala1","lala1")); user.setRoles(roles);*/ userRepository.save(user); return "saved user: "+user.getUserId(); } @RequestMapping(value = "/login", method = RequestMethod.POST) @ResponseBody public String login(@RequestParam String login,@RequestParam String password) throws JSONException{ JSONObject loginResponse = new JSONObject(); boolean userFound = false; for(User user: userRepository.findAll()){ if(login.equals(user.getEmail()) && password.equals(user.getPassword())){ userFound = true; loginResponse.put("success", true); loginResponse.put("userId", user.getUserId()); //access codes JSONArray accessCodes = new JSONArray(); for(Role role : user.getRoles()){ JSONObject returnedRole = new JSONObject(); returnedRole.put("role", role.getRoleName()); returnedRole.put("password", role.getPassword()); accessCodes.put(returnedRole); } //message JSONArray messages = new JSONArray(); for( Message message : user.getMessages()){ JSONObject returnedMessage = new JSONObject(); returnedMessage.put("messageId", message.getMessageId()); returnedMessage.put("dateTime", message.getDateTime()); returnedMessage.put("isRead", message.isRead()); returnedMessage.put("message", message.getMessage()); messages.put(returnedMessage); } loginResponse.put("accessCodes", accessCodes); loginResponse.put("messages", messages); } if(login.equals(user.getEmail()) && !password.equals(user.getPassword())){ userFound = true; loginResponse.put("success", false); loginResponse.put("error_code", 2); loginResponse.put("error_message", "BAD_PASSWORD"); } } if(!userFound){ loginResponse.put("success", false); loginResponse.put("error_code", 1); loginResponse.put("error_message", "NO_SUCH_USER"); } System.out.println(loginResponse.toString()); return loginResponse.toString(); } @RequestMapping("/addMe") @ResponseBody public String addMe() { User marcin = new User("Marcin", "Michalek", "m", "p"); Set roles = new HashSet(); roles.add(new Role("CLEANER", "shityJob")); roles.add(new Role("BOSS", "fuckYea")); marcin.setRoles(roles); marcin.addMessage(new Message("hello")); marcin.addMessage(new Message("You're fired")); userRepository.save(marcin); return "User created" + marcin.getUserId(); } @RequestMapping("/{userId}") public String getUser(@PathVariable("userId") Long userId) throws JSONException{ User user = userRepository.findOne(userId); 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 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(); } @RequestMapping("/remove/{userId}") public String removeUser(@PathVariable("userId") Long userId) { userRepository.delete(userId); return "user deleted: "+userId; } }