From fbe8121e9473d0121ee185770a7611d612db5540 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Jun 2020 11:42:46 +0200 Subject: [PATCH] finish Connection controller --- db.mv.db | Bin 1380352 -> 1388544 bytes .../ConnectionController.java | 88 +++++++++++++++++- .../ConnectionController/ConnectionDto.java | 11 ++- .../ConnectionMapper.java | 27 ++++++ .../ConnectionNotExistsException.java | 8 ++ .../backend/models/Connection/Connection.java | 37 +++++++- .../Connection/ConnectionRepository.java | 12 ++- .../backend/service/ConnectionService.java | 27 +++++- 8 files changed, 197 insertions(+), 13 deletions(-) create mode 100644 src/main/java/pl/edu/pk/siwz/backend/controllers/ConnectionController/ConnectionMapper.java create mode 100644 src/main/java/pl/edu/pk/siwz/backend/exception/ConnectionNotExistsException.java diff --git a/db.mv.db b/db.mv.db index 85b512fd94da7dcc2bc4120b59f39b31800b0984..ba83a793c637d00b4d1edefc759d37195a38e28d 100644 GIT binary patch delta 29429 zcmeI5e~=Vae!ypDf3dSaVA+Qt<&ObDaxlX5ubJ+RckU0c5D!5P#jMEMJ=4PxaCha$ z$zdw9;$<-ABH%@ycbF0@dV=R2;N{>dRf+sO{Ba;?c|j9Pu9C_hrV`=m$|QG5k?(u` zy5H;FUYLQM5plL^wqMWo_V-P{U!V8=H3K^q51d&%ZEl+!w^sF~SGGx-W=JDhh&#to9roU;gZp+BFoVMjebnnBPM{gZydU{O5b(eRC zjj?4r29e;ZlJ&(+L%Q-$W&pE!xjwhd@=T%dmgyVrwXv2h%G$Ry>v_DPXuUS4O9Bv51 zPlUdrc;OnfJy-k7!#ETTMQP*AOsi&!7j<4t^J0baKiM-Ihj^bF#(c+lhFuy?$+*q$XKK zB-N{lNn1T(Cv>4EX+4t_Ym(Bfz?w*6P4syGir%iib-iob65kJ}@%i7v<7-s7-_PdFxBB2JL--<)5d0ZLu(V%C+pP*?|mQL6k#KIK6EZCU?j^~NwI|{ zg>yBiN#VZ26UGmAHy~(3E8$7A@To2|*%jAECXS#s6K-E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exb)uB%r9vLKJah6crknr_OrDe-|4FbhW% zyw^Q!mCs)L^AHig|sm4g-bIj*ik}_A{ZfsG{o-EBrB6j0~#SvgBWIKgJIT5 zvI0@RU;A1p$x2ttNt0Zq0SQtbTxq~0fhuY+U{`=3<*HbF-yv=+ zlT0OF>Eurd(x?QL;mW5nz$-KV2?c?COtn&^>Tfit`89!;7jLMj|zOAVRI99z7Q4nnj6?!iA9jKz$a zTG6afCMs~}X|7r2=id3SoO`F3^gh>m?wz%X3aXhsch>KJ{(@ zm2G?6^5I&yeAz2MT;{Z^ntpe&Td`L=W%W4nKX$Gc&22kv*nk!JDKD2y(M4E< zr9~jDkhudtwr8}H>rcWUPXw7hhLlFJc&e;rm)fu(^b>a_#@$qex6A*HV7P_NU%1u}I*8pdiJfhlxNV;p9;Kyw{dgH&>C@)N^hUMXzZ> 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); + } + }