Api changes, error with findAllByLocationName

This commit is contained in:
2023-04-28 10:44:37 +02:00
parent b9340843a6
commit fb21a636a4
9 changed files with 62 additions and 57 deletions

View File

@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/progetto152/locations")
@RequestMapping("/progetto152/location")
public class LocationController {
private final LocationService locationService;
@@ -25,9 +25,10 @@ public class LocationController {
return new ResponseEntity<>(Locations, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<LocationEntity> getLocationById(@PathVariable("id") Long id) {
LocationEntity location = locationService.getLocationById(id);
@GetMapping("/{name}")
public ResponseEntity<LocationEntity> getLocationByName(@PathVariable("name") String name) {
LocationEntity location = locationService.getLocationByName(name);
if (location != null) {
return new ResponseEntity<>(location, HttpStatus.OK);
} else {
@@ -35,9 +36,9 @@ public class LocationController {
}
}
@GetMapping("?name={name}")
public ResponseEntity<LocationEntity> getLocationByName(@PathVariable("location") String name) {
LocationEntity location = locationService.getLocationByName(name);
@GetMapping("id/{id}")
public ResponseEntity<LocationEntity> getLocationById(@PathVariable("id") Long id) {
LocationEntity location = locationService.getLocationById(id);
if (location != null) {
return new ResponseEntity<>(location, HttpStatus.OK);
} else {
@@ -54,7 +55,7 @@ public class LocationController {
return new ResponseEntity<>(createdLocation, HttpStatus.CREATED);
}
@PutMapping("{id}")
@PutMapping("/{id}")
public ResponseEntity<LocationEntity> updateLocation(@PathVariable("id") Long id, @RequestBody LocationEntity location) {
LocationEntity location1 = locationService.updateLocation(id, location);
if (location1 != null) {
@@ -64,7 +65,7 @@ public class LocationController {
}
}
@DeleteMapping("{id}")
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteLocation(@PathVariable("id") Long id) {
boolean deleted = locationService.deleteLocation(id);
if (deleted) {

View File

@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/progetto152/users")
@RequestMapping("/progetto152/user")
public class UserController {
private final UserService userService;
@@ -27,7 +27,7 @@ public class UserController {
return new ResponseEntity<>(users, HttpStatus.OK);
}
@GetMapping("/{id}")
@GetMapping("/id/{id}")
public ResponseEntity<UserEntity> getUserById(@PathVariable("id") Long id) {
UserEntity user = userService.getUserById(id);
if (user != null) {
@@ -37,17 +37,7 @@ public class UserController {
}
}
@GetMapping("?name={name}")
public ResponseEntity<UserEntity> getUserByName(@PathVariable("name") String name) {
UserEntity user = userService.getUserByName(name);
if (user != null) {
return new ResponseEntity<>(user, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("?username={username}")
@GetMapping("/{username}")
public ResponseEntity<UserEntity> getUserByUsername(@PathVariable("username") String username) {
UserEntity user = userService.getUserByUsername(username);
if (user != null) {

View File

@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/progetto152/waypoints")
@RequestMapping("/progetto152/waypoint")
public class WaypointController {
private final WaypointService waypointService;
@@ -19,25 +19,29 @@ public class WaypointController {
this.waypointService = waypointService;
}
@GetMapping("")
public ResponseEntity<List<WaypointsEntity>> getAllWaypoints() {
List<WaypointsEntity> waypoint = waypointService.getAllWaypoints();
@GetMapping("/{location}")
public ResponseEntity<List<WaypointsEntity>> getAllWaypoints(@PathVariable("location") String location) {
List<WaypointsEntity> waypoint = waypointService.getAllWaypointsByLocation(location);
return new ResponseEntity<>(waypoint, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<WaypointsEntity> getWaypointById(@PathVariable("id") Long id) {
@GetMapping("/{location}/{id}")
public ResponseEntity<WaypointsEntity> getWaypointById(@PathVariable("location") String location, @PathVariable("id") Long id) {
WaypointsEntity waypoint = waypointService.getWaypointById(id);
if (waypoint != null) {
return new ResponseEntity<>(waypoint, HttpStatus.OK);
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("?name={name}")
public ResponseEntity<WaypointsEntity> getWaypointByName(@PathVariable("name") String name) {
WaypointsEntity waypoint = waypointService.getWaypointByName(name);
@GetMapping("/id/{id}")
public ResponseEntity<WaypointsEntity> getWaypointById(@PathVariable("id") Long id) {
WaypointsEntity waypoint = waypointService.getWaypointById(id);
if (waypoint != null) {
return new ResponseEntity<>(waypoint, HttpStatus.OK);
} else {

View File

@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/progetto152/waypointsvisited")
@RequestMapping("/progetto152/waypoint/visited")
public class WaypointVisitedController {
private final WaypointVisitedService waypointVisitedService;
@@ -26,26 +26,6 @@ public class WaypointVisitedController {
}
@GetMapping("/{id}")
public ResponseEntity<WaypointsVisitedEntity> getWaypointVisitedById(@PathVariable("id") Long id) {
WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointById(id);
if (waypointVisited != null) {
return new ResponseEntity<>(waypointVisited, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("?userid={id}")
public ResponseEntity<WaypointsVisitedEntity> getWaypointVisitedByUserId(@PathVariable("id") Long id) {
WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByUserId(id);
if (waypointVisited != null) {
return new ResponseEntity<>(waypointVisited, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("?waypointid={id}")
public ResponseEntity<WaypointsVisitedEntity> getWaypointVisitedByWaypointId(@PathVariable("id") Long id) {
WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByWaypointId(id);
if (waypointVisited != null) {
@@ -55,6 +35,16 @@ public class WaypointVisitedController {
}
}
@GetMapping("/user/{id}")
public ResponseEntity<WaypointsVisitedEntity> getWaypointVisitedByUserId(@PathVariable("id") Long id) {
WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByUserId(id);
if (waypointVisited != null) {
return new ResponseEntity<>(waypointVisited, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("")
public ResponseEntity<WaypointsVisitedEntity> createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) {
WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited);

View File

@@ -61,4 +61,8 @@ public class WaypointsEntity {
public int hashCode() {
return Objects.hash(id, name, lat, lon, description, img, locationName);
}
public String getLocationName() {
return locationName;
}
}

View File

@@ -3,9 +3,13 @@ package ch.progetto152.repository;
import ch.progetto152.entity.WaypointsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
public interface WaypointRepository extends JpaRepository<WaypointsEntity, Long> {
Optional<WaypointsEntity> findWaypointByName(String name);
List<WaypointsEntity> findAllByLocationName(String locationName);
}

View File

@@ -23,13 +23,20 @@ public class WaypointService {
return waypointRepository.findAll();
}
public List<WaypointsEntity> getAllWaypointsByLocation(String locationName) {
List<WaypointsEntity> waypoints = waypointRepository.findAllByLocationName(locationName);
if (waypoints.isEmpty()) {
return null;
}else {
return waypoints;
}
}
public WaypointsEntity getWaypointById(Long id) {
return waypointRepository.findById(id).orElse(null);
}
public WaypointsEntity getWaypointByName(String name) {
return waypointRepository.findWaypointByName(name).orElse(null);
}
public WaypointsEntity createWaypoint(WaypointsEntity waypoint) {
if (errorChecking.checkWaypoint(waypoint)) {