Added Lombok and created api fow WaypointsVisitedEntity

This commit is contained in:
grata
2023-04-27 14:02:50 +02:00
parent 69a0e6d271
commit 0bc69c9808
13 changed files with 195 additions and 140 deletions

View File

@@ -27,7 +27,7 @@ public class LocationController {
@GetMapping("/get/{id}") @GetMapping("/get/{id}")
public ResponseEntity<LocationEntity> getLocationById(@PathVariable("id") Long id) { public ResponseEntity<LocationEntity> getLocationById(@PathVariable("id") Long id) {
LocationEntity location = locationService.getLocationByIdService(id); 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 {
@@ -37,7 +37,7 @@ public class LocationController {
@GetMapping("/get/name/{name}") @GetMapping("/get/name/{name}")
public ResponseEntity<LocationEntity> getLocationByName(@PathVariable("name") String name) { public ResponseEntity<LocationEntity> getLocationByName(@PathVariable("name") String name) {
LocationEntity location = locationService.getLocationByNameService(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 {

View File

@@ -29,7 +29,7 @@ public class UserController {
@GetMapping("/get/{id}") @GetMapping("/get/{id}")
public ResponseEntity<UserEntity> getUserById(@PathVariable("id") Long id) { public ResponseEntity<UserEntity> getUserById(@PathVariable("id") Long id) {
UserEntity user = userService.getUserByIdService(id); UserEntity user = userService.getUserById(id);
if (user != null) { if (user != null) {
return new ResponseEntity<>(user, HttpStatus.OK); return new ResponseEntity<>(user, HttpStatus.OK);
} else { } else {
@@ -39,7 +39,7 @@ public class UserController {
@GetMapping("/get/name/{name}") @GetMapping("/get/name/{name}")
public ResponseEntity<UserEntity> getUserByName(@PathVariable("name") String name) { public ResponseEntity<UserEntity> getUserByName(@PathVariable("name") String name) {
UserEntity user = userService.getUserByNameService(name); UserEntity user = userService.getUserByName(name);
if (user != null) { if (user != null) {
return new ResponseEntity<>(user, HttpStatus.OK); return new ResponseEntity<>(user, HttpStatus.OK);
} else { } else {
@@ -49,7 +49,7 @@ public class UserController {
@GetMapping("/get/username/{username}") @GetMapping("/get/username/{username}")
public ResponseEntity<UserEntity> getUserByUsername(@PathVariable("username") String username) { public ResponseEntity<UserEntity> getUserByUsername(@PathVariable("username") String username) {
UserEntity user = userService.getUserByUsernameService(username); UserEntity user = userService.getUserByUsername(username);
if (user != null) { if (user != null) {
return new ResponseEntity<>(user, HttpStatus.OK); return new ResponseEntity<>(user, HttpStatus.OK);
} else { } else {

View File

@@ -27,7 +27,7 @@ public class WaypointController {
@GetMapping("/get/{id}") @GetMapping("/get/{id}")
public ResponseEntity<WaypointsEntity> getWaypointById(@PathVariable("id") Long id) { public ResponseEntity<WaypointsEntity> getWaypointById(@PathVariable("id") Long id) {
WaypointsEntity waypoint = waypointService.getWaypointByIdService(id); 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 {
@@ -37,7 +37,7 @@ public class WaypointController {
@GetMapping("/get/name/{name}") @GetMapping("/get/name/{name}")
public ResponseEntity<WaypointsEntity> getWaypointByName(@PathVariable("name") String name) { public ResponseEntity<WaypointsEntity> getWaypointByName(@PathVariable("name") String name) {
WaypointsEntity waypoint = waypointService.getWaypointByNameService(name); WaypointsEntity waypoint = waypointService.getWaypointByName(name);
if (waypoint != null) { if (waypoint != null) {
return new ResponseEntity<>(waypoint, HttpStatus.OK); return new ResponseEntity<>(waypoint, HttpStatus.OK);
} else { } else {

View File

@@ -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<List<WaypointsVisitedEntity>> getAllWaypointsVisited() {
List<WaypointsVisitedEntity> waypointVisited = waypointVisitedService.getAllWaypointsVisited();
return new ResponseEntity<>(waypointVisited, HttpStatus.OK);
}
@GetMapping("/get/{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("/get/name/{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("/get/name/{id}")
public ResponseEntity<WaypointsVisitedEntity> 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<WaypointsVisitedEntity> createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) {
WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited);
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
}
@PutMapping("/put/{id}")
public ResponseEntity<WaypointsVisitedEntity> 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<Void> 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);
}
}
}

View File

@@ -1,10 +1,16 @@
package ch.progetto152.entity; package ch.progetto152.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table(name = "Location", schema = "Progetto152", catalog = "") @Table(name = "Location", schema = "Progetto152", catalog = "")
public class LocationEntity { public class LocationEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -21,38 +27,6 @@ public class LocationEntity {
@Column(name = "lon") @Column(name = "lon")
private double 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {

View File

@@ -1,10 +1,16 @@
package ch.progetto152.entity; package ch.progetto152.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table(name = "User", schema = "Progetto152", catalog = "") @Table(name = "User", schema = "Progetto152", catalog = "")
public class UserEntity { public class UserEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -21,35 +27,9 @@ public class UserEntity {
@Column(name = "password") @Column(name = "password")
private String password; private String password;
public int getId() { public UserEntity(String name, String username, String password) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name; this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username; this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password; this.password = password;
} }

View File

@@ -1,10 +1,16 @@
package ch.progetto152.entity; package ch.progetto152.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table(name = "Waypoints", schema = "Progetto152", catalog = "") @Table(name = "Waypoints", schema = "Progetto152", catalog = "")
public class WaypointsEntity { public class WaypointsEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -30,59 +36,12 @@ public class WaypointsEntity {
@Column(name = "locationName") @Column(name = "locationName")
private String locationName; private String locationName;
public int getId() { public WaypointsEntity(String name, double lat, double lon, String description, String img, String locationName) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name; this.name = name;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat; this.lat = lat;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon; this.lon = lon;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description; this.description = description;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img; this.img = img;
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName; this.locationName = locationName;
} }

View File

@@ -1,10 +1,16 @@
package ch.progetto152.entity; package ch.progetto152.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table(name = "WaypointsVisited", schema = "Progetto152", catalog = "") @Table(name = "WaypointsVisited", schema = "Progetto152", catalog = "")
public class WaypointsVisitedEntity { public class WaypointsVisitedEntity {
@Basic @Basic
@@ -16,22 +22,6 @@ public class WaypointsVisitedEntity {
@Column(name = "waypointId") @Column(name = "waypointId")
private int 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {

View File

@@ -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<WaypointsVisitedEntity, Long> {
Optional<WaypointsVisitedEntity> findWaypointsVisitedEntitiesByUserId(Long id);
Optional<WaypointsVisitedEntity> findWaypointsVisitedEntitiesByWaypointId(Long id);
}

View File

@@ -21,11 +21,11 @@ public class LocationService {
return locationRepository.findAll(); return locationRepository.findAll();
} }
public LocationEntity getLocationByIdService(Long id) { public LocationEntity getLocationById(Long id) {
return locationRepository.findById(id).orElse(null); return locationRepository.findById(id).orElse(null);
} }
public LocationEntity getLocationByNameService(String name){ public LocationEntity getLocationByName(String name){
return locationRepository.findLocationByLocation(name).orElse(null); return locationRepository.findLocationByLocation(name).orElse(null);
} }
@@ -34,7 +34,7 @@ public class LocationService {
} }
public LocationEntity updateLocation(Long id, LocationEntity Location) { public LocationEntity updateLocation(Long id, LocationEntity Location) {
LocationEntity location1 = getLocationByIdService(id); LocationEntity location1 = getLocationById(id);
if (location1 != null) { if (location1 != null) {
location1.setLocation(Location.getLocation()); location1.setLocation(Location.getLocation());
location1.setRegion(Location.getRegion()); location1.setRegion(Location.getRegion());

View File

@@ -22,15 +22,15 @@ public class UserService {
return userRepository.findAll(); return userRepository.findAll();
} }
public UserEntity getUserByIdService(Long id) { public UserEntity getUserById(Long id) {
return userRepository.findById(id).orElse(null); return userRepository.findById(id).orElse(null);
} }
public UserEntity getUserByNameService(String name){ public UserEntity getUserByName(String name){
return userRepository.findUserByName(name).orElse(null); return userRepository.findUserByName(name).orElse(null);
} }
public UserEntity getUserByUsernameService(String username){ public UserEntity getUserByUsername(String username){
return userRepository.findUserByUsername(username).orElse(null); return userRepository.findUserByUsername(username).orElse(null);
} }
@@ -39,7 +39,7 @@ public class UserService {
} }
public UserEntity updateUser(Long id, UserEntity user) { public UserEntity updateUser(Long id, UserEntity user) {
UserEntity user1 = getUserByIdService(id); UserEntity user1 = getUserById(id);
if (user1 != null) { if (user1 != null) {
user1.setName(user.getName()); user1.setName(user.getName());
user1.setUsername(user.getUsername()); user1.setUsername(user.getUsername());

View File

@@ -20,11 +20,11 @@ public class WaypointService {
return waypointRepository.findAll(); return waypointRepository.findAll();
} }
public WaypointsEntity getWaypointByIdService(Long id) { public WaypointsEntity getWaypointById(Long id) {
return waypointRepository.findById(id).orElse(null); return waypointRepository.findById(id).orElse(null);
} }
public WaypointsEntity getWaypointByNameService(String name) { public WaypointsEntity getWaypointByName(String name) {
return waypointRepository.findWaypointByName(name).orElse(null); return waypointRepository.findWaypointByName(name).orElse(null);
} }
@@ -33,7 +33,7 @@ public class WaypointService {
} }
public WaypointsEntity updateWaypoint(Long id, WaypointsEntity waypoint) { public WaypointsEntity updateWaypoint(Long id, WaypointsEntity waypoint) {
WaypointsEntity waypoint1 = getWaypointByIdService(id); WaypointsEntity waypoint1 = getWaypointById(id);
if (waypoint1 != null) { if (waypoint1 != null) {
waypoint1.setName(waypoint.getName()); waypoint1.setName(waypoint.getName());
waypoint1.setLat(waypoint.getLat()); waypoint1.setLat(waypoint.getLat());

View File

@@ -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<WaypointsVisitedEntity> 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;
}
}