package ch.progetto152.controller; import ch.progetto152.entity.WaypointsEntity; import ch.progetto152.services.WaypointService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/progetto152/waypoint") public class WaypointController { private final WaypointService waypointService; @Autowired public WaypointController(WaypointService waypointService) { this.waypointService = waypointService; } @GetMapping("/{location}") public ResponseEntity> getAllWaypoints(@PathVariable("location") String location) { List waypoint = waypointService.getAllWaypointsByLocation(location); return new ResponseEntity<>(waypoint, HttpStatus.OK); } @GetMapping("/{location}/{id}") public ResponseEntity getWaypointById(@PathVariable("location") String location, @PathVariable("id") Long id) { WaypointsEntity waypoint = waypointService.getWaypointById(id); if (waypoint != null) { if (waypoint.getLocationName().equals(location)) { return new ResponseEntity<>(waypoint, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @GetMapping("/id/{id}") public ResponseEntity getWaypointById(@PathVariable("id") Long id) { WaypointsEntity waypoint = waypointService.getWaypointById(id); if (waypoint != null) { return new ResponseEntity<>(waypoint, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @PostMapping("") public ResponseEntity createWaypoint(@RequestBody WaypointsEntity waypoint) { WaypointsEntity createdWaypoint = waypointService.createWaypoint(waypoint); if (createdWaypoint == null) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); } @PutMapping("/{id}") public ResponseEntity updateWaypoint(@PathVariable("id") Long id, @RequestBody WaypointsEntity waypoint) { WaypointsEntity updatedWaypoint = waypointService.updateWaypoint(id, waypoint); if (updatedWaypoint != null) { return new ResponseEntity<>(updatedWaypoint, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @DeleteMapping("/{id}") public ResponseEntity deleteWaypoint(@PathVariable("id") Long id) { boolean deleted = waypointService.deleteWaypoint(id); if (deleted) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } }