Implemented api for all entities

This commit is contained in:
grata
2023-04-27 11:39:39 +02:00
parent 10fd85cf8f
commit 3a7586f495
7 changed files with 261 additions and 52 deletions

View File

@@ -0,0 +1,73 @@
package ch.progetto152.controller;
import ch.progetto152.entity.Location;
import ch.progetto152.services.LocationService;
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("/api/locations")
public class LocationController {
private final LocationService locationService;
@Autowired
public LocationController(LocationService locationService) {
this.locationService = locationService;
}
@GetMapping("/get/all")
public ResponseEntity<List<Location>> getAllLocations() {
List<Location> Locations = locationService.getAllLocations();
return new ResponseEntity<>(Locations, HttpStatus.OK);
}
@GetMapping("/get/{id}")
public ResponseEntity<Location> getLocationById(@PathVariable("id") Long id) {
Location location = locationService.getLocationByIdService(id);
if (location != null) {
return new ResponseEntity<>(location, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/get/name/{name}")
public ResponseEntity<Location> getLocationByName(@PathVariable("name") String name) {
Location location = locationService.getLocationByNameService(name);
if (location != null) {
return new ResponseEntity<>(location, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/create/{id}")
public ResponseEntity<Location> createLocation(@RequestBody Location location) {
Location createdLocation = locationService.createLocation(location);
return new ResponseEntity<>(createdLocation, HttpStatus.CREATED);
}
@PutMapping("/put/{id}")
public ResponseEntity<Location> updateLocation(@PathVariable("id") Long id, @RequestBody Location location) {
Location location1 = locationService.updateLocation(id, location);
if (location1 != null) {
return new ResponseEntity<>(location1, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<Void> deleteLocation(@PathVariable("id") Long id) {
boolean deleted = locationService.deleteLocation(id);
if (deleted) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}