package com.getit.todoapp.rest; import com.getit.todoapp.domain.Userinfo; import com.getit.todoapp.service.UserService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; 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.ResponseBody; @RooWebJson(jsonObject = Userinfo.class) @Controller @RequestMapping("/userinfoes") public class UserinfoController { private static final String APPLICATION_JSON = "application/json"; private static final String CONTENT_TYPE = "Content-Type"; @Autowired private UserService userService; @RequestMapping(value = "/{id}", method = RequestMethod.GET, headers = "Accept=application/json") @ResponseBody public ResponseEntity<String> showJson(@PathVariable("id") Long id) { Userinfo userinfo = userService.findUserinfo(id); HttpHeaders headers = new HttpHeaders(); headers.add(CONTENT_TYPE, "application/json; charset=utf-8"); if (userinfo == null) { return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND); } return new ResponseEntity<String>(userinfo.toJson(), headers, HttpStatus.OK); } @RequestMapping(headers = "Accept=application/json") @ResponseBody public ResponseEntity<String> listJson() { HttpHeaders headers = new HttpHeaders(); headers.add(CONTENT_TYPE, "application/json; charset=utf-8"); List<Userinfo> result = userService.findAllUserinfoes(); return new ResponseEntity<String>(Userinfo.toJsonArray(result), headers, HttpStatus.OK); } @RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json") public ResponseEntity<String> createFromJson(@RequestBody String json) { Userinfo userinfo = Userinfo.fromJsonToUserinfo(json); userService.saveUserinfo(userinfo); HttpHeaders headers = new HttpHeaders(); headers.add(CONTENT_TYPE, APPLICATION_JSON); return new ResponseEntity<String>(headers, HttpStatus.CREATED); } @RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json") public ResponseEntity<String> createFromJsonArray(@RequestBody String json) { for (Userinfo userinfo: Userinfo.fromJsonArrayToUserinfoes(json)) { userService.saveUserinfo(userinfo); } HttpHeaders headers = new HttpHeaders(); headers.add(CONTENT_TYPE, APPLICATION_JSON); return new ResponseEntity<String>(headers, HttpStatus.CREATED); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT, headers = "Accept=application/json") public ResponseEntity<String> updateFromJson(@RequestBody String json, @PathVariable("id") Long id) { HttpHeaders headers = new HttpHeaders(); headers.add(CONTENT_TYPE, APPLICATION_JSON); Userinfo userinfo = Userinfo.fromJsonToUserinfo(json); if (userService.updateUserinfo(userinfo) == null) { return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND); } return new ResponseEntity<String>(headers, HttpStatus.OK); } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json") public ResponseEntity<String> deleteFromJson(@PathVariable("id") Long id) { Userinfo userinfo = userService.findUserinfo(id); HttpHeaders headers = new HttpHeaders(); headers.add(CONTENT_TYPE, APPLICATION_JSON); if (userinfo == null) { return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND); } userService.deleteUserinfo(userinfo); return new ResponseEntity<String>(headers, HttpStatus.OK); } }