fixed getAllWaypointsByLocation by changing db field name

This commit is contained in:
grata
2023-04-28 11:16:57 +02:00
parent fb21a636a4
commit bc8893baef
5 changed files with 12 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ public class WaypointController {
@GetMapping("/{location}") @GetMapping("/{location}")
public ResponseEntity<List<WaypointsEntity>> getAllWaypoints(@PathVariable("location") String location) { public ResponseEntity<List<WaypointsEntity>> getAllWaypoints(@PathVariable("location") String location) {
System.out.println(waypointService.getAllWaypointsByLocation(location));
List<WaypointsEntity> waypoint = waypointService.getAllWaypointsByLocation(location); List<WaypointsEntity> waypoint = waypointService.getAllWaypointsByLocation(location);
return new ResponseEntity<>(waypoint, HttpStatus.OK); return new ResponseEntity<>(waypoint, HttpStatus.OK);
} }

View File

@@ -33,7 +33,7 @@ public class WaypointsEntity {
@Column(name = "img") @Column(name = "img")
private String img; private String img;
@Basic @Basic
@Column(name = "locationName") @Column(name = "location_name")
private String locationName; private String locationName;
public WaypointsEntity(String name, double lat, double lon, String description, String img, String locationName) { public WaypointsEntity(String name, double lat, double lon, String description, String img, String locationName) {

View File

@@ -2,6 +2,7 @@ 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 org.springframework.data.jpa.repository.Query;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -10,6 +11,6 @@ public interface WaypointRepository extends JpaRepository<WaypointsEntity, Long>
Optional<WaypointsEntity> findWaypointByName(String name); Optional<WaypointsEntity> findWaypointByName(String name);
List<WaypointsEntity> findAllByLocationName(String locationName); Optional<List<WaypointsEntity>> findAllByLocationName(String locationName);
} }

View File

@@ -24,8 +24,8 @@ public class WaypointService {
} }
public List<WaypointsEntity> getAllWaypointsByLocation(String locationName) { public List<WaypointsEntity> getAllWaypointsByLocation(String locationName) {
List<WaypointsEntity> waypoints = waypointRepository.findAllByLocationName(locationName); List<WaypointsEntity> waypoints = waypointRepository.findAllByLocationName(locationName).orElse(null);
if (waypoints.isEmpty()) { if (waypoints.isEmpty() || waypoints == null) {
return null; return null;
}else { }else {
return waypoints; return waypoints;

View File

@@ -21,9 +21,9 @@ create table if not exists Waypoints
lon double not null, lon double not null,
description varchar(1000) not null, description varchar(1000) not null,
img varchar(1000) not null, img varchar(1000) not null,
locationName varchar(255) not null, location_name varchar(255) not null,
primary key (id), primary key (id),
foreign key (LocationName) references Location (location) foreign key (Location_name) references Location (location)
); );
create table if not exists User create table if not exists User
@@ -56,19 +56,19 @@ values ('Locarno', 'TI', 46.170794, 8.799534);
insert into Location (location, region, lat, lon) insert into Location (location, region, lat, lon)
values ('Ascona', 'TI', 46.157320, 8.773882); values ('Ascona', 'TI', 46.157320, 8.773882);
insert into Waypoints (name, lat, lon, description, img, LocationName) insert into Waypoints (name, lat, lon, description, img, Location_name)
values ('Cascata Santa Petronilla', 46.35328215446709, 8.97758397155138, 'A', values ('Cascata Santa Petronilla', 46.35328215446709, 8.97758397155138, 'A',
'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'Biasca'); 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'Biasca');
insert into Waypoints (name, lat, lon, description, img, LocationName) insert into Waypoints (name, lat, lon, description, img, Location_name)
values ('Casa Küng', 46.363570208549994, 8.963464722308554, 'Descrizione del punto 2', values ('Casa Küng', 46.363570208549994, 8.963464722308554, 'Descrizione del punto 2',
'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
'Biasca'); 'Biasca');
insert into Waypoints (name, lat, lon, description, img, LocationName) values ('Punto 1', 46.123, insert into Waypoints (name, lat, lon, description, img, Location_name) values ('Punto 1', 46.123,
8.123, 8.123,
'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, insert into Waypoints (id, name, lat, lon, description, img, Location_name) values (4, 'Punto 2', 46.123, 8.123,
'Descrizione del punto 4', 'Descrizione del punto 4',
'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
'Locarno'); 'Locarno');