/** * /create --> Create a new user and save it in the database. * * @param email User's email * @param name User's name * @return A string describing if the user is successfully created or not. */ @RequestMapping("/create") @ResponseBody public String create(String email, String name) { User user = null; try { user = new User(email, name); userDao.save(user); } catch (Exception ex) { return "Error creating the user: " + ex.toString(); } return "User succesfully created! (id = " + user.getId() + ")"; }
/** * POST /user -> create a new user. * * @param user A json object representing the user. * @return An http 2xx status in case of success. */ @RequestMapping( value = "/user", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<?> addUser(@RequestBody User user) { user.setCreateTime(new DateTime()); userRepository.save(user); return new ResponseEntity<>(HttpStatus.OK); }
@RequestMapping(value="/get-by-email") @ResponseBody public String getByEmail(String email) { String userId; try { User user = _userDao.getByEmail(email); userId = String.valueOf(user.getId()); } catch(Exception ex) { return "User not found"; } return "The user id is: " + userId; }
/** * Update the email and the name for the user indentified by the passed id. */ @RequestMapping(value="/update") @ResponseBody public String updateName(long id, String email, String name) { try { User user = userDao.getById(id); user.setEmail(email); user.setName(name); userDao.update(user); } catch (Exception ex) { return "Error updating the user: " + ex.toString(); } return "User succesfully updated!"; }
@RequestMapping(value="/save") @ResponseBody public String create(String email, String name) { try { User user = new User(email, name); _userDao.save(user); } catch(Exception ex) { return ex.getMessage(); } return "User succesfully saved!"; }
try { User user = userRepository.findOne(id); user.setEmail(email);
/** * Retrieve the id for the user with the passed email address. */ @RequestMapping(value="/get-by-email") @ResponseBody public String getByEmail(String email) { String userId; try { User user = userDao.getByEmail(email); userId = String.valueOf(user.getId()); } catch (Exception ex) { return "User not found: " + ex.toString(); } return "The user id is: " + userId; }
/** * /update --> Update the email and the name for the user in the database * having the passed id. * * @param id The id for the user to update. * @param email The new email. * @param name The new name. * @return A string describing if the user is successfully updated or not. */ @RequestMapping("/update") @ResponseBody public String updateUser(long id, String email, String name) { try { User user = userDao.findOne(id); user.setEmail(email); user.setName(name); userDao.save(user); } catch (Exception ex) { return "Error updating the user: " + ex.toString(); } return "User successfully updated!"; }
@RequestMapping(value="/delete") @ResponseBody public String delete(long id) { try { User user = new User(id); _userDao.delete(user); } catch(Exception ex) { return ex.getMessage(); } return "User succesfully deleted!"; }
/** * /get-by-email --> Return the id for the user having the passed email. * * @param email The email to search in the database. * @return The user id or a message error if the user is not found. */ @RequestMapping("/get-by-email") @ResponseBody public String getByEmail(String email) { String userId; try { User user = userDao.findByEmail(email); userId = String.valueOf(user.getId()); } catch (Exception ex) { return "User not found"; } return "The user id is: " + userId; }
/** * Delete the user with the passed id. */ @RequestMapping(value="/delete") @ResponseBody public String delete(long id) { try { User user = new User(id); userDao.delete(user); } catch (Exception ex) { return "Error deleting the user: " + ex.toString(); } return "User succesfully deleted!"; }
/** * /user/get?email=[email] -> return the user having the passed email. * * @param email The email to search in the database. * @return The user id or a message error if the user is not found. */ @RequestMapping("/user/get") @ResponseBody public String getUser(String email) { String userId = ""; String userType = ""; try { User user = userRepository.findByEmail(email); userId = String.valueOf(user.getId()); // get the user type if (user instanceof Person) userType = "Person"; else if (user instanceof Company) userType = "Company"; } catch (Exception ex) { return "User not found"; } return "The " + userType + " id is: " + userId; }
/** * Create a new user with an auto-generated id and email and name as passed * values. */ @RequestMapping(value="/create") @ResponseBody public String create(String email, String name) { try { User user = new User(email, name); userDao.create(user); } catch (Exception ex) { return "Error creating the user: " + ex.toString(); } return "User succesfully created!"; }
/** * /delete --> Delete the user having the passed id. * * @param id The id of the user to delete * @return A string describing if the user is successfully deleted or not. */ @RequestMapping("/delete") @ResponseBody public String delete(long id) { try { User user = new User(id); userDao.delete(user); } catch (Exception ex) { return "Error deleting the user: " + ex.toString(); } return "User successfully deleted!"; }