BoatController.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package pl.sudra.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.MediaType;
  4. import org.springframework.web.bind.annotation.*;
  5. import pl.sudra.domain.Boat;
  6. import pl.sudra.service.BoatService;
  7. import java.util.List;
  8. @RestController
  9. @CrossOrigin(origins = "http://localhost:1410")
  10. public class BoatController {
  11. private final BoatService boatService;
  12. @Autowired
  13. public BoatController(BoatService boatService) {
  14. this.boatService = boatService;
  15. }
  16. @RequestMapping(
  17. value = "/getAllBoats",
  18. method = RequestMethod.GET,
  19. produces = MediaType.APPLICATION_JSON_VALUE)
  20. public List<Boat> listAddresses() {
  21. System.out.println("Getting Boats");
  22. return boatService.getBoats();
  23. }
  24. // test request
  25. @RequestMapping(value = "/hello", method = RequestMethod.GET)
  26. public String sayHello() {
  27. System.out.println("Greetings!");
  28. return "Hello";
  29. }
  30. @RequestMapping(value = "/addBoat", method = RequestMethod.POST)
  31. public boolean addBoat(@RequestBody Boat boat) {
  32. System.out.println(boat.toString());
  33. boatService.addBoat(boat);
  34. return true;
  35. }
  36. @RequestMapping(
  37. value = "/getBoatNameById",
  38. method = RequestMethod.GET,
  39. produces = MediaType.APPLICATION_JSON_VALUE)
  40. public String getBoatNameById(@RequestParam("id") Long id) {
  41. return this.boatService.getBoat(id).getName();
  42. }
  43. }