Skip to content

Instantly share code, notes, and snippets.

@krams915
Created January 20, 2012 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krams915/1645998 to your computer and use it in GitHub Desktop.
Save krams915/1645998 to your computer and use it in GitHub Desktop.
package org.krams.controller;
import org.krams.domain.Role;
import org.krams.domain.User;
import org.krams.dto.UserListDto;
import org.krams.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService service;
@RequestMapping
public String getUsersPage() {
return "users";
}
@RequestMapping(value="/records")
public @ResponseBody UserListDto getUsers() {
UserListDto userListDto = new UserListDto();
userListDto.setUsers(service.readAll());
return userListDto;
}
@RequestMapping(value="/get")
public @ResponseBody User get(@RequestBody User user) {
return service.read(user);
}
@RequestMapping(value="/create", method=RequestMethod.POST)
public @ResponseBody User create(
@RequestParam String username,
@RequestParam String password,
@RequestParam String firstName,
@RequestParam String lastName,
@RequestParam Integer role) {
Role newRole = new Role();
newRole.setRole(role);
User newUser = new User();
newUser.setUsername(username);
newUser.setPassword(password);
newUser.setFirstName(firstName);
newUser.setLastName(lastName);
newUser.setRole(newRole);
return service.create(newUser);
}
@RequestMapping(value="/update", method=RequestMethod.POST)
public @ResponseBody User update(
@RequestParam String username,
@RequestParam String firstName,
@RequestParam String lastName,
@RequestParam Integer role) {
Role existingRole = new Role();
existingRole.setRole(role);
User existingUser = new User();
existingUser.setUsername(username);
existingUser.setFirstName(firstName);
existingUser.setLastName(lastName);
existingUser.setRole(existingRole);
return service.update(existingUser);
}
@RequestMapping(value="/delete", method=RequestMethod.POST)
public @ResponseBody Boolean delete(
@RequestParam String username) {
User existingUser = new User();
existingUser.setUsername(username);
return service.delete(existingUser);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment