baza danych rozwiniecie

This commit is contained in:
lukasgol committed 2014-04-22 01:15:52 +02:00
1 parent 99c1aef51c
commit ffc0dcff67
11 files changed
+291 -34

No files matched your search

@@ -0,0 +1,60 @@
package application.controllers;
import application.model.Role;
import application.repositories.RoleRepository;
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;
/**
* Created by lukasgol on 21.04.14.
*/
@Controller
@RequestMapping(value = "/roles")
public class RoleController {
@Autowired
private RoleRepository roleRepository;
@RequestMapping(value = "/add")
@ResponseBody
public String addRole(@RequestParam String rolename,@RequestParam String password) {
Role role = new Role(rolename,password);
roleRepository.save(role);
return "saved role: "+role.getRoleId();
}
@RequestMapping("/{roleId}")
public String getRole(@PathVariable("roleId") Long roleId) {
Role role = roleRepository.findOne(roleId);
return "role: "+role.toString();
}
@RequestMapping(value = "/all")
public
@ResponseBody
String listRolesJson(ModelMap model) throws JSONException {
JSONArray roleArray = new JSONArray();
for (Role role : roleRepository.findAll()) {
JSONObject roleJSON = new JSONObject();
roleJSON.put("id", role.getRoleId());
roleJSON.put("role", role.getRoleName());
roleJSON.put("password", role.getPassword());
roleArray.put(roleJSON);
}
return roleArray.toString();
}
@RequestMapping("/remove/{roleId}")
public String removeRole(@PathVariable("roleId") Long roleId) {
roleRepository.delete(roleId);
return "role deleted: "+roleId;
}
}