From 0bc69c9808c7205cff029b0ef19ac2bcb4404ed0 Mon Sep 17 00:00:00 2001 From: grata Date: Thu, 27 Apr 2023 14:02:50 +0200 Subject: [PATCH] Added Lombok and created api fow WaypointsVisitedEntity --- .../controller/LocationController.java | 4 +- .../controller/UserController.java | 6 +- .../controller/WaypointController.java | 4 +- .../controller/WaypointVisitedController.java | 83 +++++++++++++++++++ .../ch/progetto152/entity/LocationEntity.java | 38 ++------- .../ch/progetto152/entity/UserEntity.java | 34 ++------ .../progetto152/entity/WaypointsEntity.java | 55 ++---------- .../entity/WaypointsVisitedEntity.java | 22 ++--- .../repository/WaypointVisitedRepository.java | 11 +++ .../progetto152/services/LocationService.java | 6 +- .../ch/progetto152/services/UserService.java | 8 +- .../progetto152/services/WaypointService.java | 6 +- .../services/WaypointVisitedService.java | 58 +++++++++++++ 13 files changed, 195 insertions(+), 140 deletions(-) create mode 100644 src/main/java/ch/progetto152/controller/WaypointVisitedController.java create mode 100644 src/main/java/ch/progetto152/repository/WaypointVisitedRepository.java create mode 100644 src/main/java/ch/progetto152/services/WaypointVisitedService.java diff --git a/src/main/java/ch/progetto152/controller/LocationController.java b/src/main/java/ch/progetto152/controller/LocationController.java index c2383df..d70e30b 100644 --- a/src/main/java/ch/progetto152/controller/LocationController.java +++ b/src/main/java/ch/progetto152/controller/LocationController.java @@ -27,7 +27,7 @@ public class LocationController { @GetMapping("/get/{id}") public ResponseEntity getLocationById(@PathVariable("id") Long id) { - LocationEntity location = locationService.getLocationByIdService(id); + LocationEntity location = locationService.getLocationById(id); if (location != null) { return new ResponseEntity<>(location, HttpStatus.OK); } else { @@ -37,7 +37,7 @@ public class LocationController { @GetMapping("/get/name/{name}") public ResponseEntity getLocationByName(@PathVariable("name") String name) { - LocationEntity location = locationService.getLocationByNameService(name); + LocationEntity location = locationService.getLocationByName(name); if (location != null) { return new ResponseEntity<>(location, HttpStatus.OK); } else { diff --git a/src/main/java/ch/progetto152/controller/UserController.java b/src/main/java/ch/progetto152/controller/UserController.java index 8e864fb..5292a7c 100644 --- a/src/main/java/ch/progetto152/controller/UserController.java +++ b/src/main/java/ch/progetto152/controller/UserController.java @@ -29,7 +29,7 @@ public class UserController { @GetMapping("/get/{id}") public ResponseEntity getUserById(@PathVariable("id") Long id) { - UserEntity user = userService.getUserByIdService(id); + UserEntity user = userService.getUserById(id); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { @@ -39,7 +39,7 @@ public class UserController { @GetMapping("/get/name/{name}") public ResponseEntity getUserByName(@PathVariable("name") String name) { - UserEntity user = userService.getUserByNameService(name); + UserEntity user = userService.getUserByName(name); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { @@ -49,7 +49,7 @@ public class UserController { @GetMapping("/get/username/{username}") public ResponseEntity getUserByUsername(@PathVariable("username") String username) { - UserEntity user = userService.getUserByUsernameService(username); + UserEntity user = userService.getUserByUsername(username); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { diff --git a/src/main/java/ch/progetto152/controller/WaypointController.java b/src/main/java/ch/progetto152/controller/WaypointController.java index 32ad21f..5d09dd5 100644 --- a/src/main/java/ch/progetto152/controller/WaypointController.java +++ b/src/main/java/ch/progetto152/controller/WaypointController.java @@ -27,7 +27,7 @@ public class WaypointController { @GetMapping("/get/{id}") public ResponseEntity getWaypointById(@PathVariable("id") Long id) { - WaypointsEntity waypoint = waypointService.getWaypointByIdService(id); + WaypointsEntity waypoint = waypointService.getWaypointById(id); if (waypoint != null) { return new ResponseEntity<>(waypoint, HttpStatus.OK); } else { @@ -37,7 +37,7 @@ public class WaypointController { @GetMapping("/get/name/{name}") public ResponseEntity getWaypointByName(@PathVariable("name") String name) { - WaypointsEntity waypoint = waypointService.getWaypointByNameService(name); + WaypointsEntity waypoint = waypointService.getWaypointByName(name); if (waypoint != null) { return new ResponseEntity<>(waypoint, HttpStatus.OK); } else { diff --git a/src/main/java/ch/progetto152/controller/WaypointVisitedController.java b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java new file mode 100644 index 0000000..5f5edcf --- /dev/null +++ b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java @@ -0,0 +1,83 @@ +package ch.progetto152.controller; + +import ch.progetto152.entity.WaypointsVisitedEntity; +import ch.progetto152.services.WaypointVisitedService; +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/users") +public class WaypointVisitedController { + private final WaypointVisitedService waypointVisitedService; + + @Autowired + public WaypointVisitedController(WaypointVisitedService waypointVisitedService) { + this.waypointVisitedService = waypointVisitedService; + } + + @GetMapping("/get/all") + public ResponseEntity> getAllWaypointsVisited() { + List waypointVisited = waypointVisitedService.getAllWaypointsVisited(); + return new ResponseEntity<>(waypointVisited, HttpStatus.OK); + } + + @GetMapping("/get/{id}") + public ResponseEntity 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("/get/name/{id}") + public ResponseEntity 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("/get/name/{id}") + public ResponseEntity getWaypointVisitedByWaypointId(@PathVariable("id") Long id) { + WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByWaypointId(id); + if (waypointVisited != null) { + return new ResponseEntity<>(waypointVisited, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @PostMapping("/create/{id}") + public ResponseEntity createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) { + WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited); + return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); + } + + @PutMapping("/put/{id}") + public ResponseEntity updateWaypointVisited(@PathVariable("id") Long id, @RequestBody WaypointsVisitedEntity waypointVisited) { + WaypointsVisitedEntity updatedWaypoint = waypointVisitedService.updateWaypoint(id, waypointVisited); + if (updatedWaypoint != null) { + return new ResponseEntity<>(updatedWaypoint, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @DeleteMapping("/delete/{id}") + public ResponseEntity deleteWaypointVisited(@PathVariable("id") Long id) { + boolean deleted = waypointVisitedService.deleteWaypoint(id); + if (deleted) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } +} diff --git a/src/main/java/ch/progetto152/entity/LocationEntity.java b/src/main/java/ch/progetto152/entity/LocationEntity.java index 2132cb5..22f21f4 100644 --- a/src/main/java/ch/progetto152/entity/LocationEntity.java +++ b/src/main/java/ch/progetto152/entity/LocationEntity.java @@ -1,10 +1,16 @@ package ch.progetto152.entity; import jakarta.persistence.*; +import lombok.*; import java.util.Objects; @Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString @Table(name = "Location", schema = "Progetto152", catalog = "") public class LocationEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -21,38 +27,6 @@ public class LocationEntity { @Column(name = "lon") private double lon; - public String getLocation() { - return location; - } - - public void setLocation(String location) { - this.location = location; - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - public double getLat() { - return lat; - } - - public void setLat(double lat) { - this.lat = lat; - } - - public double getLon() { - return lon; - } - - public void setLon(double lon) { - this.lon = lon; - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/ch/progetto152/entity/UserEntity.java b/src/main/java/ch/progetto152/entity/UserEntity.java index b9d4848..f88d901 100644 --- a/src/main/java/ch/progetto152/entity/UserEntity.java +++ b/src/main/java/ch/progetto152/entity/UserEntity.java @@ -1,10 +1,16 @@ package ch.progetto152.entity; import jakarta.persistence.*; +import lombok.*; import java.util.Objects; @Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString @Table(name = "User", schema = "Progetto152", catalog = "") public class UserEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -21,35 +27,9 @@ public class UserEntity { @Column(name = "password") private String password; - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { + public UserEntity(String name, String username, String password) { this.name = name; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { this.password = password; } diff --git a/src/main/java/ch/progetto152/entity/WaypointsEntity.java b/src/main/java/ch/progetto152/entity/WaypointsEntity.java index 5e402fd..0358a2c 100644 --- a/src/main/java/ch/progetto152/entity/WaypointsEntity.java +++ b/src/main/java/ch/progetto152/entity/WaypointsEntity.java @@ -1,10 +1,16 @@ package ch.progetto152.entity; import jakarta.persistence.*; +import lombok.*; import java.util.Objects; @Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString @Table(name = "Waypoints", schema = "Progetto152", catalog = "") public class WaypointsEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -30,59 +36,12 @@ public class WaypointsEntity { @Column(name = "locationName") private String locationName; - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { + public WaypointsEntity(String name, double lat, double lon, String description, String img, String locationName) { this.name = name; - } - - public double getLat() { - return lat; - } - - public void setLat(double lat) { this.lat = lat; - } - - public double getLon() { - return lon; - } - - public void setLon(double lon) { this.lon = lon; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { this.description = description; - } - - public String getImg() { - return img; - } - - public void setImg(String img) { this.img = img; - } - - public String getLocationName() { - return locationName; - } - - public void setLocationName(String locationName) { this.locationName = locationName; } diff --git a/src/main/java/ch/progetto152/entity/WaypointsVisitedEntity.java b/src/main/java/ch/progetto152/entity/WaypointsVisitedEntity.java index 39fdea2..5329215 100644 --- a/src/main/java/ch/progetto152/entity/WaypointsVisitedEntity.java +++ b/src/main/java/ch/progetto152/entity/WaypointsVisitedEntity.java @@ -1,10 +1,16 @@ package ch.progetto152.entity; import jakarta.persistence.*; +import lombok.*; import java.util.Objects; @Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString @Table(name = "WaypointsVisited", schema = "Progetto152", catalog = "") public class WaypointsVisitedEntity { @Basic @@ -16,22 +22,6 @@ public class WaypointsVisitedEntity { @Column(name = "waypointId") private int waypointId; - public int getUserId() { - return userId; - } - - public void setUserId(int userId) { - this.userId = userId; - } - - public int getWaypointId() { - return waypointId; - } - - public void setWaypointId(int waypointId) { - this.waypointId = waypointId; - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/ch/progetto152/repository/WaypointVisitedRepository.java b/src/main/java/ch/progetto152/repository/WaypointVisitedRepository.java new file mode 100644 index 0000000..9f61413 --- /dev/null +++ b/src/main/java/ch/progetto152/repository/WaypointVisitedRepository.java @@ -0,0 +1,11 @@ +package ch.progetto152.repository; + +import ch.progetto152.entity.WaypointsVisitedEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface WaypointVisitedRepository extends JpaRepository { + Optional findWaypointsVisitedEntitiesByUserId(Long id); + Optional findWaypointsVisitedEntitiesByWaypointId(Long id); +} diff --git a/src/main/java/ch/progetto152/services/LocationService.java b/src/main/java/ch/progetto152/services/LocationService.java index 9077b76..96cae85 100644 --- a/src/main/java/ch/progetto152/services/LocationService.java +++ b/src/main/java/ch/progetto152/services/LocationService.java @@ -21,11 +21,11 @@ public class LocationService { return locationRepository.findAll(); } - public LocationEntity getLocationByIdService(Long id) { + public LocationEntity getLocationById(Long id) { return locationRepository.findById(id).orElse(null); } - public LocationEntity getLocationByNameService(String name){ + public LocationEntity getLocationByName(String name){ return locationRepository.findLocationByLocation(name).orElse(null); } @@ -34,7 +34,7 @@ public class LocationService { } public LocationEntity updateLocation(Long id, LocationEntity Location) { - LocationEntity location1 = getLocationByIdService(id); + LocationEntity location1 = getLocationById(id); if (location1 != null) { location1.setLocation(Location.getLocation()); location1.setRegion(Location.getRegion()); diff --git a/src/main/java/ch/progetto152/services/UserService.java b/src/main/java/ch/progetto152/services/UserService.java index 190db03..7b8bd17 100644 --- a/src/main/java/ch/progetto152/services/UserService.java +++ b/src/main/java/ch/progetto152/services/UserService.java @@ -22,15 +22,15 @@ public class UserService { return userRepository.findAll(); } - public UserEntity getUserByIdService(Long id) { + public UserEntity getUserById(Long id) { return userRepository.findById(id).orElse(null); } - public UserEntity getUserByNameService(String name){ + public UserEntity getUserByName(String name){ return userRepository.findUserByName(name).orElse(null); } - public UserEntity getUserByUsernameService(String username){ + public UserEntity getUserByUsername(String username){ return userRepository.findUserByUsername(username).orElse(null); } @@ -39,7 +39,7 @@ public class UserService { } public UserEntity updateUser(Long id, UserEntity user) { - UserEntity user1 = getUserByIdService(id); + UserEntity user1 = getUserById(id); if (user1 != null) { user1.setName(user.getName()); user1.setUsername(user.getUsername()); diff --git a/src/main/java/ch/progetto152/services/WaypointService.java b/src/main/java/ch/progetto152/services/WaypointService.java index 361f739..99b182b 100644 --- a/src/main/java/ch/progetto152/services/WaypointService.java +++ b/src/main/java/ch/progetto152/services/WaypointService.java @@ -20,11 +20,11 @@ public class WaypointService { return waypointRepository.findAll(); } - public WaypointsEntity getWaypointByIdService(Long id) { + public WaypointsEntity getWaypointById(Long id) { return waypointRepository.findById(id).orElse(null); } - public WaypointsEntity getWaypointByNameService(String name) { + public WaypointsEntity getWaypointByName(String name) { return waypointRepository.findWaypointByName(name).orElse(null); } @@ -33,7 +33,7 @@ public class WaypointService { } public WaypointsEntity updateWaypoint(Long id, WaypointsEntity waypoint) { - WaypointsEntity waypoint1 = getWaypointByIdService(id); + WaypointsEntity waypoint1 = getWaypointById(id); if (waypoint1 != null) { waypoint1.setName(waypoint.getName()); waypoint1.setLat(waypoint.getLat()); diff --git a/src/main/java/ch/progetto152/services/WaypointVisitedService.java b/src/main/java/ch/progetto152/services/WaypointVisitedService.java new file mode 100644 index 0000000..6a5d028 --- /dev/null +++ b/src/main/java/ch/progetto152/services/WaypointVisitedService.java @@ -0,0 +1,58 @@ +package ch.progetto152.services; + +import ch.progetto152.entity.WaypointsVisitedEntity; +import ch.progetto152.repository.WaypointVisitedRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class WaypointVisitedService { + private final WaypointVisitedRepository waypointVisitedRepository; + + @Autowired + public WaypointVisitedService(WaypointVisitedRepository waypointVisitedRepository) { + this.waypointVisitedRepository = waypointVisitedRepository; + } + + public List getAllWaypointsVisited() { + return waypointVisitedRepository.findAll(); + } + + public WaypointsVisitedEntity getWaypointById(Long id) { + return waypointVisitedRepository.findById(id).orElse(null); + } + + public WaypointsVisitedEntity getWaypointsVisitedByUserId(Long id) { + return waypointVisitedRepository.findWaypointsVisitedEntitiesByUserId(id).orElse(null); + } + + public WaypointsVisitedEntity getWaypointsVisitedByWaypointId(Long id) { + return waypointVisitedRepository.findWaypointsVisitedEntitiesByWaypointId(id).orElse(null); + } + + public WaypointsVisitedEntity createWaypoint(WaypointsVisitedEntity waypoint) { + return waypointVisitedRepository.save(waypoint); + } + + public WaypointsVisitedEntity updateWaypoint(Long id, WaypointsVisitedEntity waypoint) { + WaypointsVisitedEntity waypoint1 = getWaypointById(id); + if (waypoint1 != null) { + waypoint1.setWaypointId(waypoint.getWaypointId()); + waypoint1.setUserId(waypoint.getUserId()); + return waypointVisitedRepository.save(waypoint1); + } else { + return null; + } + } + + public boolean deleteWaypoint(Long id) { + boolean exists = waypointVisitedRepository.existsById(id); + if (!exists) { + return false; + } + waypointVisitedRepository.deleteById(id); + return true; + } +}