88 lines
3.0 KiB
Java
88 lines
3.0 KiB
Java
package SuMoC.backend.trip;
|
|
|
|
|
|
import SuMoC.backend.suggestion.services.AdHocSuggestionService;
|
|
import SuMoC.backend.trip.dtos.NewTripDTO;
|
|
import SuMoC.backend.trip.dtos.TripDTO;
|
|
import SuMoC.backend.trip.models.Trip;
|
|
import SuMoC.backend.user.models.User;
|
|
import SuMoC.backend.user.service.CurrentUserService;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import jakarta.validation.Valid;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/trip")
|
|
@RequiredArgsConstructor
|
|
public class TripController {
|
|
|
|
private final TripService tripService;
|
|
|
|
private final CurrentUserService currentUser;
|
|
|
|
private final AdHocSuggestionService adHocSuggestionService;
|
|
|
|
@Operation(summary = "User uploads a new trip from the App.")
|
|
@PostMapping("/uploadTrip")
|
|
public ResponseEntity<Long> uploadTrip(@Valid @RequestBody NewTripDTO newTripDTO) {
|
|
User user = currentUser.get();
|
|
Trip savedTrip = tripService.addTrip(user, newTripDTO);
|
|
|
|
// let the ad hoc service suggest an alternative more ecofriendly route if appropriate
|
|
adHocSuggestionService.checkNewTripForNotifiedAlternativeSuggestion(savedTrip);
|
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(savedTrip.getId());
|
|
}
|
|
|
|
@Operation(summary = "Retrieve all trips of the authenticated user.")
|
|
@GetMapping("/getAllUserTrips")
|
|
public ResponseEntity<List<TripDTO>> getAllUserTrips() {
|
|
User user = currentUser.get();
|
|
|
|
List<TripDTO> userTrips = tripService.getAllTripsOfUser(user);
|
|
return ResponseEntity.ok(userTrips);
|
|
}
|
|
|
|
@Operation(summary = "Validate a trip for the authenticated user.")
|
|
@PutMapping("/validateTrip/{tripId}")
|
|
public ResponseEntity<Void> validateTrip(@PathVariable Long tripId) {
|
|
User user = currentUser.get();
|
|
|
|
tripService.validateTrip(user, tripId);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@Operation(summary = "Invalidate a trip for the authenticated user.")
|
|
@PutMapping("/invalidateTrip/{tripId}")
|
|
public ResponseEntity<Void> invalidateTrip(@PathVariable Long tripId) {
|
|
User user = currentUser.get();
|
|
|
|
tripService.invalidateTrip(user, tripId);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@Operation(summary = "Delete a trip for the authenticated user.")
|
|
@DeleteMapping("/deleteTrip/{tripId}")
|
|
public ResponseEntity<Void> deleteTrip(@PathVariable Long tripId) {
|
|
User user = currentUser.get();
|
|
|
|
tripService.deleteTrip(user, tripId);
|
|
return ResponseEntity.noContent().build();
|
|
}
|
|
|
|
@Operation(summary = "Delete all trips of the authenticated user.")
|
|
@DeleteMapping("/deleteAllTrips")
|
|
public ResponseEntity<Void> deleteAllTrips() {
|
|
User user = currentUser.get();
|
|
|
|
tripService.deleteAllTripsOfUser(user);
|
|
return ResponseEntity.noContent().build();
|
|
}
|
|
|
|
}
|