HomeController.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package io.zipcoder.persistenceapp.controller;
  2. import io.zipcoder.persistenceapp.model.Home;
  3. import io.zipcoder.persistenceapp.model.Person;
  4. import io.zipcoder.persistenceapp.service.HomeService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.web.bind.annotation.*;
  9. @RestController
  10. public class HomeController {
  11. private final
  12. HomeService homeService;
  13. @Autowired
  14. public HomeController(HomeService homeService) {
  15. this.homeService = homeService;
  16. }
  17. @PostMapping(value = "/homes")
  18. public ResponseEntity<Home> createHome(@RequestBody Home home) {
  19. homeService.addHome(home);
  20. return new ResponseEntity<>(home, HttpStatus.CREATED);
  21. }
  22. @PutMapping(value= "/homes")
  23. public ResponseEntity<Home> updateHome(Home h, Integer id) {
  24. homeService.updateHome(id, h);
  25. return new ResponseEntity<>(HttpStatus.OK);
  26. }
  27. @GetMapping(value = "/homes")
  28. public ResponseEntity<Iterable<Home>> getHomes() {
  29. Iterable<Home> homes = homeService.getAllHomes();
  30. return new ResponseEntity<>(homes, HttpStatus.OK);
  31. }
  32. @GetMapping(value = "/homes/{id}")
  33. public ResponseEntity<Home> getHomeById(@PathVariable Integer id) {
  34. Home h = homeService.findHomeById(id);
  35. return new ResponseEntity<>(h, HttpStatus.OK);
  36. }
  37. @GetMapping(value = "/homes/homenumber/{homeNumber}")
  38. public ResponseEntity<Home> getHomeByHomeNumber(@PathVariable String homeNumber) {
  39. Home h = homeService.findHomeByHomeNumber(homeNumber);
  40. return new ResponseEntity<>(h, HttpStatus.OK);
  41. }
  42. // Test this
  43. @GetMapping(value = "/homes/address/{address}")
  44. public ResponseEntity<Home> getHomeByAddress(@PathVariable String address) {
  45. Home h = homeService.findHomeByAddress(address);
  46. return new ResponseEntity<>(h, HttpStatus.OK);
  47. }
  48. @GetMapping(value = "/homes/people/{personId}")
  49. public ResponseEntity<Home> getHomeForPersonId(@PathVariable Integer personId) {
  50. Home h = homeService.findHomeByPersonId(personId);
  51. return new ResponseEntity<>(h, HttpStatus.OK);
  52. }
  53. @GetMapping(value = "/homes/{homeId}/people")
  54. public ResponseEntity<Iterable<Person>> getPeopleInHome(@PathVariable Integer homeId){
  55. Iterable<Person> people = homeService.generatePeopleInHome(homeId);
  56. return new ResponseEntity<>(people, HttpStatus.OK);
  57. }
  58. }