diff --git a/db.mv.db b/db.mv.db index 85b512f..ba83a79 100644 Binary files a/db.mv.db and b/db.mv.db differ diff --git a/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionController.java b/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionController.java index 416c428..a96a4d0 100644 --- a/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionController.java +++ b/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionController.java @@ -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> getAllConnections() { + List connections = connectionService.findAll(); + + ArrayList connectionsDtos = new ArrayList<>(); + for (Connection connection : connections) { + connectionsDtos.add(connectionMapper.map(connection)); + } + return ResponseEntity.ok(connectionsDtos); + } @ApiOperation(value = "Add new connection") @PostMapping - ResponseEntity addNewAirline(@RequestBody ConnectionDto connectionDto) { + ResponseEntity 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 updateConnection(@RequestBody ConnectionDto connectionDto) { + + if (!connectionService.existsById(connectionDto.getId())) { + throw new ConnectionNotExistsException("Connection with that id not exist!"); + } + + Optional connectionOptional = connectionService.findById(connectionDto.getId()); + Optional airline = airlineService.findById(connectionDto.getAirlineDto().getId()); + Optional srcAirport = airportService.findById(connectionDto.getSrcAirportDto().getId()); + Optional 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 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(); + } } diff --git a/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionDto.java b/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionDto.java index 44222be..0b669be 100644 --- a/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionDto.java +++ b/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionDto.java @@ -4,18 +4,19 @@ import lombok.Builder; import lombok.Getter; import lombok.Setter; +import pl.edu.pk.siwz.backend.controllers.AirlineController.AirlineDto; +import pl.edu.pk.siwz.backend.controllers.AirportController.AirportDto; -import java.time.LocalDate; -import java.time.LocalTime; @Getter @Setter @Builder @AllArgsConstructor public class ConnectionDto { - private Long srcAirportId; - private Long dstAirportId; - private Long airlineId; + private Long id; + private AirportDto srcAirportDto; + private AirportDto dstAirportDto; + private AirlineDto airlineDto; private int numberSeats; private String departureDate; private String arrivalDate; diff --git a/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionMapper.java b/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionMapper.java new file mode 100644 index 0000000..c767999 --- /dev/null +++ b/src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionMapper.java @@ -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(); + } +} diff --git a/src/main/java/pl/edu/pk/siwz/backend/exception/ConnectionNotExistsException.java b/src/main/java/pl/edu/pk/siwz/backend/exception/ConnectionNotExistsException.java new file mode 100644 index 0000000..a9578dc --- /dev/null +++ b/src/main/java/pl/edu/pk/siwz/backend/exception/ConnectionNotExistsException.java @@ -0,0 +1,8 @@ +package pl.edu.pk.siwz.backend.exception; + +public class ConnectionNotExistsException extends RuntimeException { + public ConnectionNotExistsException(String errorMessage) { + super(errorMessage); + } + } + diff --git a/src/main/java/pl/edu/pk/siwz/backend/models/Connection/Connection.java b/src/main/java/pl/edu/pk/siwz/backend/models/Connection/Connection.java index 91e58c3..e0cfd68 100644 --- a/src/main/java/pl/edu/pk/siwz/backend/models/Connection/Connection.java +++ b/src/main/java/pl/edu/pk/siwz/backend/models/Connection/Connection.java @@ -3,9 +3,18 @@ import javax.persistence.*; import lombok.*; +import org.hibernate.annotations.GenericGenerator; +import org.springframework.beans.factory.annotation.Autowired; +import pl.edu.pk.siwz.backend.controllers.AirlineController.AirlineMapper; +import pl.edu.pk.siwz.backend.controllers.AirportController.AirportMapper; +import pl.edu.pk.siwz.backend.controllers.ConnectionController.ConnectionDto; 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.Times; +import pl.edu.pk.siwz.backend.service.ConnectionService; + +import java.time.LocalDate; +import java.time.LocalTime; @Entity @Builder @@ -17,7 +26,8 @@ public class Connection { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) + @GeneratedValue(generator = "inc") + @GenericGenerator(name = "inc", strategy = "increment") @Column(name = "connection_id") private Long id; @OneToOne @@ -33,4 +43,29 @@ public class Connection { @Embedded private Times times; private double price; + + public void updateForm(Long id, + Airport srcAirport, + Airport dstAirport, + Airline airline, + int numberSeats, + String arrivalDate, + String arrivalTime, + String departureDate, + String departureTime, + double price + ) { + + this.id = id; + this.srcAirport = srcAirport; + this.dstAirport = dstAirport; + this.airline = airline; + this.numberSeats = numberSeats; + this.getTimes().setDepartureDate(LocalDate.parse(departureDate)); + this.getTimes().setArrivalDate(LocalDate.parse(arrivalDate)); + this.getTimes().setDepartureTime(LocalTime.parse(departureTime)); + this.getTimes().setArrivalTime(LocalTime.parse(arrivalTime)); + this.price = price; + + } } diff --git a/src/main/java/pl/edu/pk/siwz/backend/models/Connection/ConnectionRepository.java b/src/main/java/pl/edu/pk/siwz/backend/models/Connection/ConnectionRepository.java index 289aa6b..2ddbf83 100644 --- a/src/main/java/pl/edu/pk/siwz/backend/models/Connection/ConnectionRepository.java +++ b/src/main/java/pl/edu/pk/siwz/backend/models/Connection/ConnectionRepository.java @@ -1,9 +1,13 @@ 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 findAll(); + Connection save(Connection entity); int amountOfRows(); @@ -11,4 +15,10 @@ public interface ConnectionRepository { void deleteAllConnectionWithAirlineId(@Param("id") Long id); void deleteAllConnectionWithAirportId(@Param("id") Long id); + + boolean existsById(Long id); + + void deleteById(Long id); + + Optional findById(Long id); } diff --git a/src/main/java/pl/edu/pk/siwz/backend/service/ConnectionService.java b/src/main/java/pl/edu/pk/siwz/backend/service/ConnectionService.java index c539caa..cc4dc79 100644 --- a/src/main/java/pl/edu/pk/siwz/backend/service/ConnectionService.java +++ b/src/main/java/pl/edu/pk/siwz/backend/service/ConnectionService.java @@ -12,6 +12,7 @@ import java.time.LocalDate; import java.time.LocalTime; +import java.util.List; import java.util.Optional; @@ -33,9 +34,9 @@ public ConnectionService(ConnectionRepository connectionRepository, public Connection addNewConnection(ConnectionDto connectionDto) { - Optional airline1 = airlineRepository.findById(connectionDto.getAirlineId()); - Optional srcAirport1 = airportRepository.findById(connectionDto.getSrcAirportId()); - Optional dstAirport1 = airportRepository.findById(connectionDto.getDstAirportId()); + Optional airline1 = airlineRepository.findById(connectionDto.getAirlineDto().getId()); + Optional srcAirport1 = airportRepository.findById(connectionDto.getSrcAirportDto().getId()); + Optional dstAirport1 = airportRepository.findById(connectionDto.getDstAirportDto().getId()); Connection connection = Connection.builder() .srcAirport(srcAirport1.get()) @@ -62,5 +63,25 @@ public void deleteConnectionWithAirportId(Long id) { connectionRepository.deleteAllConnectionWithAirportId(id); } + public List findAll() { + return connectionRepository.findAll(); + } + + public boolean existsById(Long id) { + return connectionRepository.existsById(id); + } + + public void deleteConnection(Long id) { + connectionRepository.deleteById(id); + } + + public Optional findById(Long id){ + return connectionRepository.findById(id); + } + + public void save(Connection connection){ + connectionRepository.save(connection); + } + }