added comment

This commit is contained in:
2023-05-08 13:13:38 +02:00
parent f1ad83d3cc
commit 62ade1d80a
7 changed files with 40 additions and 12 deletions

View File

@@ -12,6 +12,8 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/progetto152/location") @RequestMapping("/progetto152/location")
public class LocationController { public class LocationController {
// Inject the LocationService
private final LocationService locationService; private final LocationService locationService;
@Autowired @Autowired
@@ -19,12 +21,14 @@ public class LocationController {
this.locationService = locationService; this.locationService = locationService;
} }
// Handle GET request to get all locations
@GetMapping("") @GetMapping("")
public ResponseEntity<List<LocationEntity>> getAllLocations() { public ResponseEntity<List<LocationEntity>> getAllLocations() {
List<LocationEntity> Locations = locationService.getAllLocations(); List<LocationEntity> Locations = locationService.getAllLocations();
return new ResponseEntity<>(Locations, HttpStatus.OK); return new ResponseEntity<>(Locations, HttpStatus.OK);
} }
// Handle GET request to get a specific location by name
@GetMapping("/{name}") @GetMapping("/{name}")
public ResponseEntity<LocationEntity> getLocationByName(@PathVariable("name") String name) { public ResponseEntity<LocationEntity> getLocationByName(@PathVariable("name") String name) {
LocationEntity location = locationService.getLocationByName(name); LocationEntity location = locationService.getLocationByName(name);
@@ -35,6 +39,7 @@ public class LocationController {
} }
} }
// Handle POST request to create a new location
@PostMapping("") @PostMapping("")
public ResponseEntity<LocationEntity> createLocation(@RequestBody LocationEntity location) { public ResponseEntity<LocationEntity> createLocation(@RequestBody LocationEntity location) {
LocationEntity createdLocation = locationService.createLocation(location); LocationEntity createdLocation = locationService.createLocation(location);
@@ -44,6 +49,7 @@ public class LocationController {
return new ResponseEntity<>(createdLocation, HttpStatus.CREATED); return new ResponseEntity<>(createdLocation, HttpStatus.CREATED);
} }
// Handle PUT request to update an existing location
@PutMapping("/{name}") @PutMapping("/{name}")
public ResponseEntity<LocationEntity> updateLocation(@PathVariable("name") String name, @RequestBody LocationEntity location) { public ResponseEntity<LocationEntity> updateLocation(@PathVariable("name") String name, @RequestBody LocationEntity location) {
LocationEntity location1 = locationService.updateLocation(name, location); LocationEntity location1 = locationService.updateLocation(name, location);
@@ -54,6 +60,7 @@ public class LocationController {
} }
} }
// Handle DELETE request to delete an existing location by name
@DeleteMapping("/{name}") @DeleteMapping("/{name}")
public ResponseEntity<Void> deleteLocation(@PathVariable("name") String name) { public ResponseEntity<Void> deleteLocation(@PathVariable("name") String name) {
boolean deleted = locationService.deleteLocation(name); boolean deleted = locationService.deleteLocation(name);

View File

@@ -14,6 +14,8 @@ import java.util.List;
@RequestMapping("/progetto152/user") @RequestMapping("/progetto152/user")
public class UserController { public class UserController {
// Inject the UserService
private final UserService userService; private final UserService userService;
@Autowired @Autowired
@@ -21,12 +23,14 @@ public class UserController {
this.userService = userService; this.userService = userService;
} }
// Handle GET request to get all users
@GetMapping("") @GetMapping("")
public ResponseEntity<List<UserEntity>> getAllUsers() { public ResponseEntity<List<UserEntity>> getAllUsers() {
List<UserEntity> users = userService.getAllUsers(); List<UserEntity> users = userService.getAllUsers();
return new ResponseEntity<>(users, HttpStatus.OK); return new ResponseEntity<>(users, HttpStatus.OK);
} }
// Handle GET request to get user by id
@GetMapping("/id/{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);
@@ -37,6 +41,7 @@ public class UserController {
} }
} }
// Handle GET request to get user by username
@GetMapping("/{username}") @GetMapping("/{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);
@@ -47,6 +52,7 @@ public class UserController {
} }
} }
// Handle POST request to create a new user
@PostMapping("") @PostMapping("")
public ResponseEntity<UserEntity> createUser(@RequestBody UserEntity user) { public ResponseEntity<UserEntity> createUser(@RequestBody UserEntity user) {
UserEntity createdUser = userService.createUser(user); UserEntity createdUser = userService.createUser(user);
@@ -57,6 +63,7 @@ public class UserController {
return new ResponseEntity<>(createdUser, HttpStatus.CREATED); return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
} }
// Handle PUT request to update an existing user
@PutMapping("/{id}") @PutMapping("/{id}")
public ResponseEntity<UserEntity> updateUser(@PathVariable("id") Long id, @RequestBody UserEntity user) { public ResponseEntity<UserEntity> updateUser(@PathVariable("id") Long id, @RequestBody UserEntity user) {
UserEntity updatedUser = userService.updateUser(id, user); UserEntity updatedUser = userService.updateUser(id, user);
@@ -67,6 +74,7 @@ public class UserController {
} }
} }
// Handle DELETE request to delete an existing user
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id) { public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id) {
boolean deleted = userService.deleteUser(id); boolean deleted = userService.deleteUser(id);

View File

@@ -12,6 +12,8 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/progetto152/waypoint") @RequestMapping("/progetto152/waypoint")
public class WaypointController { public class WaypointController {
// Iject the WaypointService instance
private final WaypointService waypointService; private final WaypointService waypointService;
@Autowired @Autowired
@@ -19,12 +21,14 @@ public class WaypointController {
this.waypointService = waypointService; this.waypointService = waypointService;
} }
// Handle the GET request to get all waypoints
@GetMapping("") @GetMapping("")
public ResponseEntity<List<WaypointsEntity>> getAllWaypoints() { public ResponseEntity<List<WaypointsEntity>> getAllWaypoints() {
List<WaypointsEntity> waypoint = waypointService.getAllWaypoints(); List<WaypointsEntity> waypoint = waypointService.getAllWaypoints();
return new ResponseEntity<>(waypoint, HttpStatus.OK); return new ResponseEntity<>(waypoint, HttpStatus.OK);
} }
// Handle the GET request to get all waypoints by location
@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)); System.out.println(waypointService.getAllWaypointsByLocation(location));
@@ -32,6 +36,8 @@ public class WaypointController {
return new ResponseEntity<>(waypoint, HttpStatus.OK); return new ResponseEntity<>(waypoint, HttpStatus.OK);
} }
// Handle the GET request to get a specific waypoint by id
@GetMapping("/{location}/{id}") @GetMapping("/{location}/{id}")
public ResponseEntity<WaypointsEntity> getWaypoint(@PathVariable("location") String location, @PathVariable("id") Long id) { public ResponseEntity<WaypointsEntity> getWaypoint(@PathVariable("location") String location, @PathVariable("id") Long id) {
WaypointsEntity waypoint = waypointService.getWaypointById(id); WaypointsEntity waypoint = waypointService.getWaypointById(id);
@@ -46,6 +52,7 @@ public class WaypointController {
} }
} }
// Handle the GET request to get a specific waypoint by id
@GetMapping("/id/{id}") @GetMapping("/id/{id}")
public ResponseEntity<WaypointsEntity> getWaypointById(@PathVariable("id") Long id) { public ResponseEntity<WaypointsEntity> getWaypointById(@PathVariable("id") Long id) {
WaypointsEntity waypoint = waypointService.getWaypointById(id); WaypointsEntity waypoint = waypointService.getWaypointById(id);
@@ -56,6 +63,7 @@ public class WaypointController {
} }
} }
// Handle the POST request to create a new waypoint
@PostMapping("") @PostMapping("")
public ResponseEntity<WaypointsEntity> createWaypoint(@RequestBody WaypointsEntity waypoint) { public ResponseEntity<WaypointsEntity> createWaypoint(@RequestBody WaypointsEntity waypoint) {
WaypointsEntity createdWaypoint = waypointService.createWaypoint(waypoint); WaypointsEntity createdWaypoint = waypointService.createWaypoint(waypoint);
@@ -65,6 +73,7 @@ public class WaypointController {
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
} }
// Handle the PUT request to update an existing waypoint
@PutMapping("/{id}") @PutMapping("/{id}")
public ResponseEntity<WaypointsEntity> updateWaypoint(@PathVariable("id") Long id, @RequestBody WaypointsEntity waypoint) { public ResponseEntity<WaypointsEntity> updateWaypoint(@PathVariable("id") Long id, @RequestBody WaypointsEntity waypoint) {
WaypointsEntity updatedWaypoint = waypointService.updateWaypoint(id, waypoint); WaypointsEntity updatedWaypoint = waypointService.updateWaypoint(id, waypoint);
@@ -75,6 +84,7 @@ public class WaypointController {
} }
} }
// Handle the DELETE request to delete an existing waypoint
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ResponseEntity<Void> deleteWaypoint(@PathVariable("id") Long id) { public ResponseEntity<Void> deleteWaypoint(@PathVariable("id") Long id) {
boolean deleted = waypointService.deleteWaypoint(id); boolean deleted = waypointService.deleteWaypoint(id);

View File

@@ -14,6 +14,8 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/progetto152/waypoint/visited") @RequestMapping("/progetto152/waypoint/visited")
public class WaypointVisitedController { public class WaypointVisitedController {
// Iject the WaypointVisitedService and UserService
private final WaypointVisitedService waypointVisitedService; private final WaypointVisitedService waypointVisitedService;
private final UserService userService; private final UserService userService;
@@ -24,14 +26,14 @@ public class WaypointVisitedController {
this.userService = userService; this.userService = userService;
} }
// Handle GET request to get all waypoints visited
@GetMapping("") @GetMapping("")
public ResponseEntity<List<WaypointsVisitedEntity>> getAllWaypointsVisited() { public ResponseEntity<List<WaypointsVisitedEntity>> getAllWaypointsVisited() {
List<WaypointsVisitedEntity> waypointVisited = waypointVisitedService.getAllWaypointsVisited(); List<WaypointsVisitedEntity> waypointVisited = waypointVisitedService.getAllWaypointsVisited();
return new ResponseEntity<>(waypointVisited, HttpStatus.OK); return new ResponseEntity<>(waypointVisited, HttpStatus.OK);
} }
// Handle GET request to get all waypoints visited by user
@GetMapping("/{id}") @GetMapping("/{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);
@@ -42,6 +44,7 @@ public class WaypointVisitedController {
} }
} }
// Handle GET request to get all waypoints visited by user
@GetMapping("/{user}/{id}") @GetMapping("/{user}/{id}")
public ResponseEntity<Boolean> getWaypointVisitedByWaypointIdAndUserId(@PathVariable("user") String username, @PathVariable("id") Long waypointId) { public ResponseEntity<Boolean> getWaypointVisitedByWaypointIdAndUserId(@PathVariable("user") String username, @PathVariable("id") Long waypointId) {
UserEntity user = userService.getUserByUsername(username); UserEntity user = userService.getUserByUsername(username);
@@ -55,6 +58,7 @@ public class WaypointVisitedController {
} }
} }
// Handle GET request to get all waypoints visited by user
@GetMapping("/user/{id}") @GetMapping("/user/{id}")
public ResponseEntity<WaypointsVisitedEntity> getWaypointVisitedByUserId(@PathVariable("id") Long id) { public ResponseEntity<WaypointsVisitedEntity> getWaypointVisitedByUserId(@PathVariable("id") Long id) {
WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByUserId(id); WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByUserId(id);
@@ -65,6 +69,7 @@ public class WaypointVisitedController {
} }
} }
// Handle POST request to create a new waypoint visited
@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);
@@ -74,6 +79,7 @@ public class WaypointVisitedController {
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
} }
// Handle PUT request to update a waypoint visited
@PutMapping("/{id}") @PutMapping("/{id}")
public ResponseEntity<WaypointsVisitedEntity> updateWaypointVisited(@PathVariable("id") Long id, @RequestBody WaypointsVisitedEntity waypointVisited) { public ResponseEntity<WaypointsVisitedEntity> updateWaypointVisited(@PathVariable("id") Long id, @RequestBody WaypointsVisitedEntity waypointVisited) {
WaypointsVisitedEntity updatedWaypoint = waypointVisitedService.updateWaypoint(id, waypointVisited); WaypointsVisitedEntity updatedWaypoint = waypointVisitedService.updateWaypoint(id, waypointVisited);
@@ -84,6 +90,7 @@ public class WaypointVisitedController {
} }
} }
// Handle DELETE request to delete a waypoint visited
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ResponseEntity<Void> deleteWaypointVisited(@PathVariable("id") Long id) { public ResponseEntity<Void> deleteWaypointVisited(@PathVariable("id") Long id) {
boolean deleted = waypointVisitedService.deleteWaypoint(id); boolean deleted = waypointVisitedService.deleteWaypoint(id);

View File

@@ -11,7 +11,7 @@ import java.util.List;
@Service @Service
public class LocationService { public class LocationService {
private ErrorChecking errorChecking = new ErrorChecking(); private final ErrorChecking errorChecking = new ErrorChecking();
private final LocationRepository locationRepository; private final LocationRepository locationRepository;
@Autowired @Autowired

View File

@@ -1,6 +1,5 @@
package ch.progetto152.services; package ch.progetto152.services;
import ch.progetto152.entity.UserEntity;
import ch.progetto152.entity.WaypointsVisitedEntity; import ch.progetto152.entity.WaypointsVisitedEntity;
import ch.progetto152.repository.WaypointVisitedRepository; import ch.progetto152.repository.WaypointVisitedRepository;
import ch.progetto152.utility.ErrorChecking; import ch.progetto152.utility.ErrorChecking;
@@ -63,13 +62,8 @@ public class WaypointVisitedService {
return true; return true;
} }
public Boolean getWaypointsVisitedByWaypointIdAndUserId(Long waypointId, Long userId) { public Boolean getWaypointsVisitedByWaypointIdAndUserId(Long waypointId, Long userId) {
WaypointsVisitedEntity waypointsVisited = waypointVisitedRepository.findWaypointsVisitedEntitiesByUserIdAndWaypointId(userId, waypointId).orElse(null); WaypointsVisitedEntity waypointsVisited = waypointVisitedRepository.findWaypointsVisitedEntitiesByUserIdAndWaypointId(userId, waypointId).orElse(null);
if (waypointsVisited != null) { return waypointsVisited != null;
return true;
} else {
return false;
}
} }
} }

View File

@@ -6,6 +6,8 @@ import ch.progetto152.entity.UserEntity;
import ch.progetto152.entity.WaypointsEntity; import ch.progetto152.entity.WaypointsEntity;
import ch.progetto152.entity.WaypointsVisitedEntity; import ch.progetto152.entity.WaypointsVisitedEntity;
// This class is used to check if the data received from the client is valid
public class ErrorChecking { public class ErrorChecking {
public boolean checkUser(UserEntity user){ public boolean checkUser(UserEntity user){