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;
}
}
@@ -0,0 +1,7 @@
package application.controllers;
/**
* Created by lukasgol on 21.04.14.
*/
public class TimeSheetController {
}
@@ -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;
}
}