inital checking for spring-boot-test example project
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
package io.reflectoring.booking;
|
||||||
|
|
||||||
|
import io.reflectoring.booking.business.BookingService;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import io.reflectoring.booking.data.BookingRepository;
|
||||||
|
import io.reflectoring.customer.CustomerRepository;
|
||||||
|
import io.reflectoring.flight.FlightRepository;
|
||||||
|
|
||||||
|
@EnableJpaRepositories("re")
|
||||||
|
public class BookingConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public BookingService bookingService(BookingRepository bookingRepository, CustomerRepository customerRepository, FlightRepository flightRepository) {
|
||||||
|
return new BookingService(bookingRepository, customerRepository, flightRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package io.reflectoring.booking.business;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import io.reflectoring.booking.data.Booking;
|
||||||
|
import io.reflectoring.booking.data.BookingRepository;
|
||||||
|
import io.reflectoring.customer.Customer;
|
||||||
|
import io.reflectoring.customer.CustomerRepository;
|
||||||
|
import io.reflectoring.flight.Flight;
|
||||||
|
import io.reflectoring.flight.FlightRepository;
|
||||||
|
|
||||||
|
public class BookingService {
|
||||||
|
|
||||||
|
private BookingRepository bookingRepository;
|
||||||
|
|
||||||
|
private CustomerRepository customerRepository;
|
||||||
|
|
||||||
|
private FlightRepository flightRepository;
|
||||||
|
|
||||||
|
public BookingService(
|
||||||
|
BookingRepository bookingRepository,
|
||||||
|
CustomerRepository customerRepository,
|
||||||
|
FlightRepository flightRepository) {
|
||||||
|
this.bookingRepository = bookingRepository;
|
||||||
|
this.customerRepository = customerRepository;
|
||||||
|
this.flightRepository = flightRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Books the given flight for the given customer.
|
||||||
|
*/
|
||||||
|
public Booking bookFlight(Long customerId, Long flightId) {
|
||||||
|
|
||||||
|
Optional<Customer> customer = customerRepository.findById(customerId);
|
||||||
|
if (!customer.isPresent()) {
|
||||||
|
throw new CustomerDoesNotExistException(customerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<Flight> flight = flightRepository.findById(flightId);
|
||||||
|
if (!flight.isPresent()) {
|
||||||
|
throw new FlightDoesNotExistException(flightId);
|
||||||
|
}
|
||||||
|
|
||||||
|
Booking booking = Booking.builder()
|
||||||
|
.customer(customer.get())
|
||||||
|
.flight(flight.get())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return this.bookingRepository.save(booking);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package io.reflectoring.booking.business;
|
||||||
|
|
||||||
|
class CustomerDoesNotExistException extends RuntimeException {
|
||||||
|
|
||||||
|
CustomerDoesNotExistException(Long customerId) {
|
||||||
|
super(String.format("A customer with ID '%d' doesn't exist!", customerId));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package io.reflectoring.booking.business;
|
||||||
|
|
||||||
|
class FlightDoesNotExistException extends RuntimeException {
|
||||||
|
|
||||||
|
FlightDoesNotExistException(Long flightId) {
|
||||||
|
super(String.format("A flight with ID '%d' doesn't exist!", flightId));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package io.reflectoring.booking.data;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
|
||||||
|
import io.reflectoring.customer.Customer;
|
||||||
|
import io.reflectoring.flight.Flight;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class Booking {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private Customer customer;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private Flight flight;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package io.reflectoring.booking.data;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
public interface BookingRepository extends CrudRepository<Booking, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package io.reflectoring.booking.web;
|
||||||
|
|
||||||
|
import io.reflectoring.booking.business.BookingService;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import io.reflectoring.booking.data.Booking;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class BookingController {
|
||||||
|
|
||||||
|
private BookingService bookingService;
|
||||||
|
|
||||||
|
public BookingController(BookingService bookingService) {
|
||||||
|
this.bookingService = bookingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/booking")
|
||||||
|
public ResponseEntity<BookingResultResource> bookFlight(
|
||||||
|
@RequestParam("customerId") Long customerId,
|
||||||
|
@RequestParam("flightId") Long flightId) {
|
||||||
|
try {
|
||||||
|
Booking booking = bookingService.bookFlight(customerId, flightId);
|
||||||
|
BookingResultResource bookingResult = BookingResultResource.builder()
|
||||||
|
.success(true)
|
||||||
|
.build();
|
||||||
|
return ResponseEntity.ok(bookingResult);
|
||||||
|
} catch (Exception e) {
|
||||||
|
BookingResultResource bookingResult = BookingResultResource.builder()
|
||||||
|
.success(false)
|
||||||
|
.build();
|
||||||
|
return ResponseEntity.badRequest().body(bookingResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package io.reflectoring.booking.web;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class BookingResultResource {
|
||||||
|
|
||||||
|
private boolean success;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package io.reflectoring.customer;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package io.reflectoring.customer;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
public interface CustomerRepository extends CrudRepository<Customer, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package io.reflectoring.flight;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class Flight {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String flightNumber;
|
||||||
|
|
||||||
|
private String airline;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package io.reflectoring.flight;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
public interface FlightRepository extends CrudRepository<Flight, Long> {
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user