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

@@ -1,47 +0,0 @@
package ch.progetto152.controller;
import ch.progetto152.entity.Location;
import ch.progetto152.entity.User;
import ch.progetto152.entity.Waypoints;
import ch.progetto152.services.LocationService;
import ch.progetto152.services.UserService;
import ch.progetto152.services.WaypointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(value = "api/progetto152", produces = MediaType.APPLICATION_JSON_VALUE)
public class Controller {
private static final String location1 = "location";
private static final String waypoint1 = "waypoint";
@Autowired
private UserService userService;
@GetMapping(value= location1, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Location>> getLocations() {
List<Location> locations = LocationService.getLocations();
return new ResponseEntity<>(locations, HttpStatus.OK);
}
@GetMapping(value= waypoint1, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Waypoints>> getWaypoints() {
List<Waypoints> waypoints = WaypointService.getWaypoints();
return new ResponseEntity<>(waypoints, HttpStatus.OK);
}
@PostMapping(value= location1, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> createLocation(@RequestBody Location location) {
LocationService.createLocation(location);
return new ResponseEntity<>(HttpStatus.CREATED);
}
}

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);
}
}
}

View File

@@ -0,0 +1,73 @@
package ch.progetto152.controller;
import ch.progetto152.entity.Waypoints;
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("/api/waypoints")
public class WaypointController {
private final WaypointService waypointService;
@Autowired
public WaypointController(WaypointService waypointService) {
this.waypointService = waypointService;
}
@GetMapping("/get/all")
public ResponseEntity<List<Waypoints>> getAllWaypoints() {
List<Waypoints> waypoint = waypointService.getAllWaypoints();
return new ResponseEntity<>(waypoint, HttpStatus.OK);
}
@GetMapping("/get/{id}")
public ResponseEntity<Waypoints> getWaypointById(@PathVariable("id") Long id) {
Waypoints waypoint = waypointService.getWaypointByIdService(id);
if (waypoint != null) {
return new ResponseEntity<>(waypoint, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/get/name/{name}")
public ResponseEntity<Waypoints> getWaypointByName(@PathVariable("name") String name) {
Waypoints waypoint = waypointService.getWaypointByNameService(name);
if (waypoint != null) {
return new ResponseEntity<>(waypoint, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/create/{id}")
public ResponseEntity<Waypoints> createWaypoint(@RequestBody Waypoints waypoint) {
Waypoints createdWaypoint = waypointService.createWaypoint(waypoint);
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
}
@PutMapping("/put/{id}")
public ResponseEntity<Waypoints> updateWaypoint(@PathVariable("id") Long id, @RequestBody Waypoints waypoint) {
Waypoints updatedWaypoint = waypointService.updateWaypoint(id, waypoint);
if (updatedWaypoint != null) {
return new ResponseEntity<>(updatedWaypoint, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<Void> 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);
}
}
}

View File

@@ -0,0 +1,11 @@
package ch.progetto152.repository;
import ch.progetto152.entity.Location;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface LocationRepository extends JpaRepository<Location, Long> {
Optional<Location> findLocationByLocation(String name);
}

View File

@@ -0,0 +1,11 @@
package ch.progetto152.repository;
import ch.progetto152.entity.Waypoints;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface WaypointRepository extends JpaRepository<Waypoints, Long> {
Optional<Waypoints> findWaypointByName(String name);
}

View File

@@ -1,15 +1,57 @@
package ch.progetto152.services;
import ch.progetto152.entity.Location;
import ch.progetto152.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LocationService {
public static List<Location> getLocations() {
return null;
private final LocationRepository locationRepository;
@Autowired
public LocationService(LocationRepository locationRepository) {
this.locationRepository = locationRepository;
}
public static void createLocation(Location location) {
public List<Location> getAllLocations() {
return locationRepository.findAll();
}
public Location getLocationByIdService(Long id) {
return locationRepository.findById(id).orElse(null);
}
public Location getLocationByNameService(String name){
return locationRepository.findLocationByLocation(name).orElse(null);
}
public Location createLocation(Location Location) {
return locationRepository.save(Location);
}
public Location updateLocation(Long id, Location Location) {
Location location1 = getLocationByIdService(id);
if (location1 != null) {
location1.setLocation(Location.getLocation());
location1.setRegion(Location.getRegion());
location1.setLat(Location.getLat());
location1.setLon(Location.getLon());
return locationRepository.save(location1);
} else {
return null;
}
}
public boolean deleteLocation(Long id) {
boolean exists = locationRepository.existsById(id);
if(!exists){
return false;
}
locationRepository.deleteById(id);
return true;
}
}

View File

@@ -1,13 +1,59 @@
package ch.progetto152.services;
import ch.progetto152.entity.Waypoints;
import ch.progetto152.repository.WaypointRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WaypointService {
public static List<Waypoints> getWaypoints() {
return null;
private final WaypointRepository waypointRepository;
@Autowired
public WaypointService(WaypointRepository waypointRepository) {
this.waypointRepository = waypointRepository;
}
public List<Waypoints> getAllWaypoints() {
return waypointRepository.findAll();
}
public Waypoints getWaypointByIdService(Long id) {
return waypointRepository.findById(id).orElse(null);
}
public Waypoints getWaypointByNameService(String name) {
return waypointRepository.findWaypointByName(name).orElse(null);
}
public Waypoints createWaypoint(Waypoints waypoint) {
return waypointRepository.save(waypoint);
}
public Waypoints updateWaypoint(Long id, Waypoints waypoint) {
Waypoints waypoint1 = getWaypointByIdService(id);
if (waypoint1 != null) {
waypoint1.setName(waypoint.getName());
waypoint1.setLat(waypoint.getLat());
waypoint1.setLon(waypoint.getLon());
waypoint1.setDescription(waypoint.getDescription());
waypoint1.setImg(waypoint.getImg());
waypoint1.setLocationId(waypoint.getLocationId());
return waypointRepository.save(waypoint1);
} else {
return null;
}
}
public boolean deleteWaypoint(Long id) {
boolean exists = waypointRepository.existsById(id);
if (!exists) {
return false;
}
waypointRepository.deleteById(id);
return true;
}
}