-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
197 additions
and
13 deletions.
There are no files selected for viewing
88 changes: 85 additions & 3 deletions
88
...in/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,108 @@ | ||
package pl.edu.pk.siwz.backend.controllers.ConnectionController; | ||
|
||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import pl.edu.pk.siwz.backend.controllers.AirlineController.AirlineMapper; | ||
import pl.edu.pk.siwz.backend.exception.ConnectionNotExistsException; | ||
import pl.edu.pk.siwz.backend.models.Airline.Airline; | ||
import pl.edu.pk.siwz.backend.models.Airport.Airport; | ||
import pl.edu.pk.siwz.backend.models.Connection.Connection; | ||
import pl.edu.pk.siwz.backend.service.AirlineService; | ||
import pl.edu.pk.siwz.backend.service.AirportService; | ||
import pl.edu.pk.siwz.backend.service.ConnectionService; | ||
|
||
import javax.transaction.Transactional; | ||
import java.net.URI; | ||
import java.util.*; | ||
|
||
|
||
@RestController | ||
@RequestMapping("/connections") | ||
public class ConnectionController { | ||
|
||
@Autowired | ||
private ConnectionService connectionService; | ||
private AirportService airportService; | ||
private AirlineService airlineService; | ||
private ConnectionMapper connectionMapper = new ConnectionMapper(); | ||
|
||
ConnectionController(ConnectionService connectionService, AirportService airportService, AirlineService airlineService) { | ||
this.connectionService = connectionService; | ||
this.airportService = airportService; | ||
this.airlineService = airlineService; | ||
} | ||
|
||
@ApiOperation(value = "Get all connections") | ||
@GetMapping | ||
ResponseEntity<List<ConnectionDto>> getAllConnections() { | ||
List<Connection> connections = connectionService.findAll(); | ||
|
||
ArrayList<ConnectionDto> connectionsDtos = new ArrayList<>(); | ||
for (Connection connection : connections) { | ||
connectionsDtos.add(connectionMapper.map(connection)); | ||
} | ||
return ResponseEntity.ok(connectionsDtos); | ||
} | ||
|
||
@ApiOperation(value = "Add new connection") | ||
@PostMapping | ||
ResponseEntity<Connection> addNewAirline(@RequestBody ConnectionDto connectionDto) { | ||
ResponseEntity<Connection> addNewConnection(@RequestBody ConnectionDto connectionDto) { | ||
|
||
/* | ||
* linia lotnicza moze miec kilka samolotow i je wkorszystywac bez problemu, wiec moze byc | ||
* teoretycznie kilka takich samych polaczen z tego samego lotniska zrodlowego do tego samego lotniska | ||
* docelowe w tym samym czasie | ||
*/ | ||
|
||
Connection connection = connectionService.addNewConnection(connectionDto); | ||
return ResponseEntity.created(URI.create("/" + connection.getId())).body(connection); | ||
} | ||
|
||
@ApiOperation(value = "Update connection") | ||
@Transactional | ||
@PutMapping | ||
ResponseEntity<Void> updateConnection(@RequestBody ConnectionDto connectionDto) { | ||
|
||
if (!connectionService.existsById(connectionDto.getId())) { | ||
throw new ConnectionNotExistsException("Connection with that id not exist!"); | ||
} | ||
|
||
Optional<Connection> connectionOptional = connectionService.findById(connectionDto.getId()); | ||
Optional<Airline> airline = airlineService.findById(connectionDto.getAirlineDto().getId()); | ||
Optional<Airport> srcAirport = airportService.findById(connectionDto.getSrcAirportDto().getId()); | ||
Optional<Airport> dstAirport = airportService.findById(connectionDto.getDstAirportDto().getId()); | ||
|
||
connectionOptional.get().updateForm(connectionDto.getId(), | ||
srcAirport.get(), | ||
dstAirport.get(), | ||
airline.get(), | ||
connectionDto.getNumberSeats(), | ||
connectionDto.getArrivalDate(), | ||
connectionDto.getArrivalTime(), | ||
connectionDto.getDepartureDate(), | ||
connectionDto.getDepartureTime(), | ||
connectionDto.getPrice()); | ||
connectionService.save(connectionOptional.get()); | ||
|
||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@ApiOperation(value = "Delete connection") | ||
@Transactional | ||
@DeleteMapping("/delete/{id}") | ||
public ResponseEntity<Long> deleteConnection(@PathVariable Long id) { | ||
|
||
if (!connectionService.existsById(id)) { | ||
throw new ConnectionNotExistsException("Connection with that id not exist!"); | ||
} | ||
|
||
connectionService.deleteConnection(id); | ||
return ResponseEntity.ok(id); | ||
} | ||
|
||
|
||
@ExceptionHandler(ConnectionNotExistsException.class) | ||
ResponseEntity<?> handleConnectionNotExistsException(ConnectionNotExistsException e) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pl.edu.pk.siwz.backend.controllers.ConnectionController; | ||
|
||
import pl.edu.pk.siwz.backend.controllers.AirlineController.AirlineMapper; | ||
import pl.edu.pk.siwz.backend.controllers.AirportController.AirportDto; | ||
import pl.edu.pk.siwz.backend.controllers.AirportController.AirportMapper; | ||
import pl.edu.pk.siwz.backend.models.Airport.Airport; | ||
import pl.edu.pk.siwz.backend.models.Connection.Connection; | ||
|
||
public class ConnectionMapper { | ||
|
||
private final AirlineMapper airlineMapper = new AirlineMapper(); | ||
private final AirportMapper airportMapper = new AirportMapper(); | ||
|
||
public ConnectionDto map(Connection connection) { | ||
return ConnectionDto.builder() | ||
.id(connection.getId()) | ||
.srcAirportDto(airportMapper.map(connection.getSrcAirport())) | ||
.dstAirportDto(airportMapper.map(connection.getDstAirport())) | ||
.airlineDto(airlineMapper.map(connection.getAirline())) | ||
.numberSeats(connection.getNumberSeats()) | ||
.departureDate(connection.getTimes().getDepartureDate().toString()) | ||
.arrivalDate(connection.getTimes().getArrivalDate().toString()) | ||
.departureTime(connection.getTimes().getDepartureTime().toString()) | ||
.arrivalTime(connection.getTimes().getArrivalTime().toString()) | ||
.build(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/pl/edu/pk/siwz/backend/exception/ConnectionNotExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package pl.edu.pk.siwz.backend.exception; | ||
|
||
public class ConnectionNotExistsException extends RuntimeException { | ||
public ConnectionNotExistsException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
src/main/java/pl/edu/pk/siwz/backend/models/Connection/ConnectionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
package pl.edu.pk.siwz.backend.models.Connection; | ||
|
||
import org.springframework.data.repository.query.Param; | ||
import pl.edu.pk.siwz.backend.models.Connection.Connection; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface ConnectionRepository { | ||
List<Connection> findAll(); | ||
|
||
Connection save(Connection entity); | ||
|
||
int amountOfRows(); | ||
|
||
void deleteAllConnectionWithAirlineId(@Param("id") Long id); | ||
|
||
void deleteAllConnectionWithAirportId(@Param("id") Long id); | ||
|
||
boolean existsById(Long id); | ||
|
||
void deleteById(Long id); | ||
|
||
Optional<Connection> findById(Long id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters