baza danych rozwiniecie
This commit is contained in:
1 parent
99c1aef51c
commit
ffc0dcff67
11 files changed
+291
-34
No files matched your search
@@ -0,0 +1,69 @@
|
||||
package application.controllers;
|
||||
|
||||
import application.model.Role;
|
||||
import application.model.User;
|
||||
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.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
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;
|
||||
|
||||
@RequestMapping(value = "/add")
|
||||
@ResponseBody
|
||||
public String addUser(@RequestParam String username) {
|
||||
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);
|
||||
userRepository.save(user);
|
||||
return "saved user: "+user.getUserId();
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/{userId}")
|
||||
public String getUser(@PathVariable("userId") Long userId) {
|
||||
User user = userRepository.findOne(userId);
|
||||
return "user: "+user.toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/all")
|
||||
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());
|
||||
userArray.put(userJSON);
|
||||
}
|
||||
return userArray.toString();
|
||||
}
|
||||
|
||||
@RequestMapping("/remove/{userId}")
|
||||
public String removeUser(@PathVariable("userId") Long userId) {
|
||||
userRepository.delete(userId);
|
||||
return "user deleted: "+userId;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user