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

View File

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

View File

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

View File

@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/progetto152/waypointsvisited") @RequestMapping("/progetto152/waypoint/visited")
public class WaypointVisitedController { public class WaypointVisitedController {
private final WaypointVisitedService waypointVisitedService; private final WaypointVisitedService waypointVisitedService;
@@ -26,26 +26,6 @@ public class WaypointVisitedController {
} }
@GetMapping("/{id}") @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) { public ResponseEntity<WaypointsVisitedEntity> getWaypointVisitedByWaypointId(@PathVariable("id") Long id) {
WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByWaypointId(id); WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByWaypointId(id);
if (waypointVisited != null) { 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("") @PostMapping("")
public ResponseEntity<WaypointsVisitedEntity> createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) { public ResponseEntity<WaypointsVisitedEntity> createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) {
WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited); WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited);

View File

@@ -61,4 +61,8 @@ public class WaypointsEntity {
public int hashCode() { public int hashCode() {
return Objects.hash(id, name, lat, lon, description, img, locationName); 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 ch.progetto152.entity.WaypointsEntity;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface WaypointRepository extends JpaRepository<WaypointsEntity, Long> { public interface WaypointRepository extends JpaRepository<WaypointsEntity, Long> {
Optional<WaypointsEntity> findWaypointByName(String name); Optional<WaypointsEntity> findWaypointByName(String name);
List<WaypointsEntity> findAllByLocationName(String locationName);
} }

View File

@@ -23,13 +23,20 @@ public class WaypointService {
return waypointRepository.findAll(); 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) { public WaypointsEntity getWaypointById(Long id) {
return waypointRepository.findById(id).orElse(null); return waypointRepository.findById(id).orElse(null);
} }
public WaypointsEntity getWaypointByName(String name) {
return waypointRepository.findWaypointByName(name).orElse(null);
}
public WaypointsEntity createWaypoint(WaypointsEntity waypoint) { public WaypointsEntity createWaypoint(WaypointsEntity waypoint) {
if (errorChecking.checkWaypoint(waypoint)) { if (errorChecking.checkWaypoint(waypoint)) {

View File

@@ -68,6 +68,10 @@ insert into Waypoints (name, lat, lon, description, img, LocationName) values ('
'Descrizione del punto 3, un grandissimo', 'Descrizione del punto 3, un grandissimo',
'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
'Lugano'); 'Lugano');
insert into Waypoints (id, name, lat, lon, description, img, locationName) values (4, 'Punto 2', 46.123, 8.123,
'Descrizione del punto 4',
'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
'Locarno');
insert into User (name, username, password) values ('Mario Rossi', 'mario.rossi', 'password'); insert into User (name, username, password) values ('Mario Rossi', 'mario.rossi', 'password');
insert into User (name, username, password) values ('Luca Bianchi', 'luca.bianchi', 'password'); insert into User (name, username, password) values ('Luca Bianchi', 'luca.bianchi', 'password');

View File

@@ -1,6 +1,7 @@
get: get:
progetto152/location progetto152/location
progetto152/location/{location} progetto152/location/{location}
progetto152/location/id/{id}
progetto152/waypoint/{location} progetto152/waypoint/{location}
progetto152/waypoint/{location}/{id} progetto152/waypoint/{location}/{id}
maybe: maybe: