Skip to content

Commit

Permalink
finish Connection controller
Browse files Browse the repository at this point in the history
  • Loading branch information
wydra98 committed Jun 27, 2020
1 parent 4c8f91a commit fbe8121
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 13 deletions.
Binary file modified db.mv.db
Binary file not shown.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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();
}
}
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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;

}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;


Expand All @@ -33,9 +34,9 @@ public ConnectionService(ConnectionRepository connectionRepository,

public Connection addNewConnection(ConnectionDto connectionDto) {

Optional<Airline> airline1 = airlineRepository.findById(connectionDto.getAirlineId());
Optional<Airport> srcAirport1 = airportRepository.findById(connectionDto.getSrcAirportId());
Optional<Airport> dstAirport1 = airportRepository.findById(connectionDto.getDstAirportId());
Optional<Airline> airline1 = airlineRepository.findById(connectionDto.getAirlineDto().getId());
Optional<Airport> srcAirport1 = airportRepository.findById(connectionDto.getSrcAirportDto().getId());
Optional<Airport> dstAirport1 = airportRepository.findById(connectionDto.getDstAirportDto().getId());

Connection connection = Connection.builder()
.srcAirport(srcAirport1.get())
Expand All @@ -62,5 +63,25 @@ public void deleteConnectionWithAirportId(Long id) {
connectionRepository.deleteAllConnectionWithAirportId(id);
}

public List<Connection> findAll() {
return connectionRepository.findAll();
}

public boolean existsById(Long id) {
return connectionRepository.existsById(id);
}

public void deleteConnection(Long id) {
connectionRepository.deleteById(id);
}

public Optional<Connection> findById(Long id){
return connectionRepository.findById(id);
}

public void save(Connection connection){
connectionRepository.save(connection);
}


}

0 comments on commit fbe8121

Please sign in to comment.