From a522a90cd8e96cd634037e6eca1ecb8fb746d34c Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Wed, 27 Mar 2024 10:08:28 +0200 Subject: [PATCH 01/11] configurated pom file --- .github/workflows/ci.yml | 18 ++ checkstyle.xml | 250 ++++++++++++++++++++++ pom.xml | 70 ++++++ src/main/resources/application.properties | 1 + 4 files changed, 339 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 checkstyle.xml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..282f50a --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,18 @@ +name: Java CI + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'adopt' + cache: maven + - name: Build with Maven + run: mvn --batch-mode --update-snapshots verify diff --git a/checkstyle.xml b/checkstyle.xml new file mode 100644 index 0000000..ea0a529 --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,250 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index c595abd..2014c55 100644 --- a/pom.xml +++ b/pom.xml @@ -15,12 +15,35 @@ Demo project for Spring Boot 11 + 2.2.13.RELEASE + 3.3.0 + 3.8.1 + checkstyle.xml + 0.2.0 + 1.5.5.Final + 1.18.32 org.springframework.boot spring-boot-starter + + org.projectlombok + lombok + ${lombok.version} + true + + + org.mapstruct + mapstruct + ${mapstruct.version} + + + org.projectlombok + lombok-mapstruct-binding + ${lombok.mapstruct.binding.version} + org.springframework.boot @@ -34,6 +57,53 @@ org.springframework.boot spring-boot-maven-plugin + ${spring.boot.version} + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven.checkstyle.plugin.version} + + + compile + + check + + + + + ${maven.checkstyle.plugin.configLocation} + true + true + false + src + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + ${java.version} + ${java.version} + + + org.projectlombok + lombok + ${lombok.version} + + + org.projectlombok + lombok-mapstruct-binding + ${lombok.mapstruct.binding.version} + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 18f22cd..5a8df30 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ spring.application.name=air-companies-manager +server.servlet.context-path=/api From f767c57ac0c141e227ddc5864d6d81e153fa2e4a Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Wed, 27 Mar 2024 10:34:24 +0200 Subject: [PATCH 02/11] created entities --- .../AirCompaniesManagerApplication.java | 8 +++--- .../aircompaniesmanager/model/AirCompany.java | 10 ++++++++ .../aircompaniesmanager/model/Airplane.java | 14 +++++++++++ .../aircompaniesmanager/model/Flight.java | 25 +++++++++++++++++++ .../AirCompaniesManagerApplicationTests.java | 8 +++--- 5 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/company/aircompaniesmanager/model/AirCompany.java create mode 100644 src/main/java/org/company/aircompaniesmanager/model/Airplane.java create mode 100644 src/main/java/org/company/aircompaniesmanager/model/Flight.java diff --git a/src/main/java/org/company/aircompaniesmanager/AirCompaniesManagerApplication.java b/src/main/java/org/company/aircompaniesmanager/AirCompaniesManagerApplication.java index 986a453..8690485 100644 --- a/src/main/java/org/company/aircompaniesmanager/AirCompaniesManagerApplication.java +++ b/src/main/java/org/company/aircompaniesmanager/AirCompaniesManagerApplication.java @@ -5,9 +5,7 @@ @SpringBootApplication public class AirCompaniesManagerApplication { - - public static void main(String[] args) { - SpringApplication.run(AirCompaniesManagerApplication.class, args); - } - + public static void main(String[] args) { + SpringApplication.run(AirCompaniesManagerApplication.class, args); + } } diff --git a/src/main/java/org/company/aircompaniesmanager/model/AirCompany.java b/src/main/java/org/company/aircompaniesmanager/model/AirCompany.java new file mode 100644 index 0000000..69abfa6 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/model/AirCompany.java @@ -0,0 +1,10 @@ +package org.company.aircompaniesmanager.model; + +import java.time.LocalDate; + +public class AirCompany { + private Long id; + private String name; + private String companyType; + private LocalDate foundationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/model/Airplane.java b/src/main/java/org/company/aircompaniesmanager/model/Airplane.java new file mode 100644 index 0000000..2d61013 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/model/Airplane.java @@ -0,0 +1,14 @@ +package org.company.aircompaniesmanager.model; + +import java.time.LocalDate; + +public class Airplane { + private Long id; + private String factorySerialNumber; + private Long airCompanyId; + private int numberOfFlights; + private double flightDistance; + private double fuelCapacity; + private String type; + private LocalDate creationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/model/Flight.java b/src/main/java/org/company/aircompaniesmanager/model/Flight.java new file mode 100644 index 0000000..c7a30e6 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/model/Flight.java @@ -0,0 +1,25 @@ +package org.company.aircompaniesmanager.model; + +import java.time.LocalDateTime; + +public class Flight { + private Long id; + private Status status; + private Long airCompanyId; + private Long airplaneId; + private String departureCountry; + private String destinationCountry; + private double distance; + private double estimatedTime; + private LocalDateTime startTime; + private LocalDateTime endTime; + private LocalDateTime delayStartTime; + private LocalDateTime creationDate; + + public enum Status { + ACTIVE, + COMPLETED, + DELAYED, + PENDING + } +} diff --git a/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java b/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java index c9be274..b2a812b 100644 --- a/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java +++ b/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java @@ -5,9 +5,7 @@ @SpringBootTest class AirCompaniesManagerApplicationTests { - - @Test - void contextLoads() { - } - + @Test + void contextLoads() { + } } From dc2a5c127073723800db23d4f8e0c40cab264cac Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Thu, 28 Mar 2024 19:31:59 +0200 Subject: [PATCH 03/11] realized main structure --- pom.xml | 24 ++++++ .../config/MapperConfig.java | 13 ++++ .../controller/AirCompanyController.java | 69 ++++++++++++++++++ .../controller/AirplaneController.java | 59 +++++++++++++++ .../controller/FlightController.java | 59 +++++++++++++++ .../controller/HealthController.java | 12 +++ .../dto/air/company/AirCompanyRequestDto.java | 16 ++++ .../air/company/AirCompanyResponseDto.java | 12 +++ .../company/AirCompanyUpdateRequestDto.java | 10 +++ .../dto/airplane/AirplaneRequestDto.java | 28 +++++++ .../dto/airplane/AirplaneResponseDto.java | 16 ++++ .../airplane/AirplaneUpdateRequestDto.java | 12 +++ .../dto/flight/FlightRequestDto.java | 31 ++++++++ .../dto/flight/FlightResponseDto.java | 20 +++++ .../dto/flight/FlightUpdateRequestDto.java | 11 +++ .../exception/EntityNotFoundException.java | 7 ++ .../mapper/AirCompanyMapper.java | 14 ++++ .../mapper/AirplaneMapper.java | 16 ++++ .../mapper/FlightMapper.java | 16 ++++ .../aircompaniesmanager/model/AirCompany.java | 28 +++++++ .../aircompaniesmanager/model/Airplane.java | 38 +++++++++- .../aircompaniesmanager/model/Flight.java | 43 ++++++++++- .../repository/AirCompanyRepository.java | 13 ++++ .../repository/AirplaneRepository.java | 7 ++ .../repository/FlightRepository.java | 7 ++ .../air/company/AirCompanyService.java | 18 +++++ .../air/company/AirCompanyServiceImpl.java | 61 ++++++++++++++++ .../service/airplane/AirplaneService.java | 16 ++++ .../service/airplane/AirplaneServiceImpl.java | 56 ++++++++++++++ .../service/flight/FlightService.java | 16 ++++ .../service/flight/FlightServiceImpl.java | 73 +++++++++++++++++++ src/main/resources/application.properties | 11 +++ src/main/resources/data/data.sql | 35 +++++++++ .../AirCompaniesManagerApplicationTests.java | 4 - 34 files changed, 864 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/company/aircompaniesmanager/config/MapperConfig.java create mode 100644 src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java create mode 100644 src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java create mode 100644 src/main/java/org/company/aircompaniesmanager/controller/FlightController.java create mode 100644 src/main/java/org/company/aircompaniesmanager/controller/HealthController.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyRequestDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyResponseDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyUpdateRequestDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneRequestDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneResponseDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneUpdateRequestDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/dto/flight/FlightUpdateRequestDto.java create mode 100644 src/main/java/org/company/aircompaniesmanager/exception/EntityNotFoundException.java create mode 100644 src/main/java/org/company/aircompaniesmanager/mapper/AirCompanyMapper.java create mode 100644 src/main/java/org/company/aircompaniesmanager/mapper/AirplaneMapper.java create mode 100644 src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java create mode 100644 src/main/java/org/company/aircompaniesmanager/repository/AirCompanyRepository.java create mode 100644 src/main/java/org/company/aircompaniesmanager/repository/AirplaneRepository.java create mode 100644 src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java create mode 100644 src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java create mode 100644 src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java create mode 100644 src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java create mode 100644 src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java create mode 100644 src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java create mode 100644 src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java create mode 100644 src/main/resources/data/data.sql diff --git a/pom.xml b/pom.xml index 2014c55..5be0219 100644 --- a/pom.xml +++ b/pom.xml @@ -19,15 +19,34 @@ 3.3.0 3.8.1 checkstyle.xml + 8.0.33 0.2.0 1.5.5.Final 1.18.32 + 2.3.0 org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-validation + + + mysql + mysql-connector-java + ${mysql.version} + org.projectlombok lombok @@ -44,6 +63,11 @@ lombok-mapstruct-binding ${lombok.mapstruct.binding.version} + + org.springdoc + springdoc-openapi-starter-webmvc-ui + ${openapi.version} + org.springframework.boot diff --git a/src/main/java/org/company/aircompaniesmanager/config/MapperConfig.java b/src/main/java/org/company/aircompaniesmanager/config/MapperConfig.java new file mode 100644 index 0000000..650b446 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/config/MapperConfig.java @@ -0,0 +1,13 @@ +package org.company.aircompaniesmanager.config; + +import org.mapstruct.InjectionStrategy; +import org.mapstruct.NullValueCheckStrategy; + +@org.mapstruct.MapperConfig( + componentModel = "spring", + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + implementationPackage = ".impl" +) +public class MapperConfig { +} diff --git a/src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java b/src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java new file mode 100644 index 0000000..06b7bb2 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java @@ -0,0 +1,69 @@ +package org.company.aircompaniesmanager.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.List; +import javax.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyRequestDto; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyResponseDto; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyUpdateRequestDto; +import org.company.aircompaniesmanager.service.air.company.AirCompanyService; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/air-companies") +@Tag( + name = "Air companies management", + description = "Endpoints for managing air companies" +) +public class AirCompanyController { + private final AirCompanyService airCompanyService; + + @GetMapping + @Operation(summary = "Find all air companies", + description = "Retrieves detailed information about all companies") + private List findAll() { + return airCompanyService.findAll(); + } + + @GetMapping("/{id}") + @Operation(summary = "Find air company by id", + description = "Retrieves detailed information about specified company") + private AirCompanyResponseDto findById(@PathVariable Long id) { + return airCompanyService.findById(id); + } + + @PostMapping + @Operation(summary = "Save air company", + description = "Creates new company") + private AirCompanyResponseDto save(@RequestBody @Valid AirCompanyRequestDto requestDto) { + return airCompanyService.save(requestDto); + } + + @PutMapping("/{id}") + @Operation(summary = "Update air company", + description = "Updates information about company type") + private AirCompanyResponseDto updateById( + @PathVariable Long id, + @RequestBody @Valid AirCompanyUpdateRequestDto requestDto + ) { + return airCompanyService.updateById(id, requestDto); + } + + @DeleteMapping("/{id}") + @Operation(summary = "Delete air company", + description = "Removes air company") + private String deleteById(@PathVariable Long id) { + airCompanyService.deleteById(id); + return String.format("Company with id: '%s' deleted successfully.", id); + } +} diff --git a/src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java b/src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java new file mode 100644 index 0000000..23be439 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java @@ -0,0 +1,59 @@ +package org.company.aircompaniesmanager.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.company.aircompaniesmanager.dto.airplane.AirplaneRequestDto; +import org.company.aircompaniesmanager.dto.airplane.AirplaneResponseDto; +import org.company.aircompaniesmanager.dto.airplane.AirplaneUpdateRequestDto; +import org.company.aircompaniesmanager.service.airplane.AirplaneService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/airplanes") +@Tag( + name = "Airplanes management", + description = "Endpoints for managing airplanes" +) +public class AirplaneController { + private final AirplaneService airplaneService; + + @GetMapping + @Operation(summary = "Find all airplanes", + description = "Retrieves detailed information about all airplanes") + public List findAll() { + return airplaneService.findAll(); + } + + @GetMapping("/{id}") + @Operation(summary = "Find airplane by id", + description = "Retrieves detailed information about specified airplane") + public AirplaneResponseDto findById(@PathVariable Long id) { + return airplaneService.findById(id); + } + + @PostMapping + @Operation(summary = "Save new airplane", + description = "Creates new airplane") + public AirplaneResponseDto save(@RequestBody AirplaneRequestDto requestDto) { + return airplaneService.save(requestDto); + } + + @PutMapping("/{id}") + @Operation(summary = "Reassign to another company", + description = "Updates airplane company") + public AirplaneResponseDto updateCompany( + @PathVariable Long id, + @RequestBody AirplaneUpdateRequestDto requestDto + ) { + return airplaneService.updateCompany(id, requestDto); + } +} diff --git a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java new file mode 100644 index 0000000..965b48f --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java @@ -0,0 +1,59 @@ +package org.company.aircompaniesmanager.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.company.aircompaniesmanager.dto.flight.FlightRequestDto; +import org.company.aircompaniesmanager.dto.flight.FlightResponseDto; +import org.company.aircompaniesmanager.dto.flight.FlightUpdateRequestDto; +import org.company.aircompaniesmanager.service.flight.FlightService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/flights") +@Tag( + name = "Flights management", + description = "Endpoints for managing flights" +) +public class FlightController { + private final FlightService flightService; + + @GetMapping + @Operation(summary = "Find all flights", + description = "Retrieves detailed information about all flights") + List findAll() { + return flightService.findAll(); + } + + @GetMapping("/{id}") + @Operation(summary = "Find flight by id", + description = "Retrieves detailed information about specified flight") + FlightResponseDto findById(@PathVariable Long id) { + return flightService.findById(id); + } + + @PostMapping + @Operation(summary = "Save new flight", + description = "Creates new flight") + FlightResponseDto save(@RequestBody FlightRequestDto requestDto) { + return flightService.save(requestDto); + } + + @PutMapping("/{id}") + @Operation(summary = "Update flight status", + description = "Updates status in specified flight") + FlightResponseDto updateStatus( + @PathVariable Long id, + @RequestBody FlightUpdateRequestDto requestDto + ) { + return flightService.updateStatus(id, requestDto); + } +} diff --git a/src/main/java/org/company/aircompaniesmanager/controller/HealthController.java b/src/main/java/org/company/aircompaniesmanager/controller/HealthController.java new file mode 100644 index 0000000..76d8c42 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/controller/HealthController.java @@ -0,0 +1,12 @@ +package org.company.aircompaniesmanager.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HealthController { + @GetMapping("/health") + public String getHealth() { + return "Server is working..."; + } +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyRequestDto.java b/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyRequestDto.java new file mode 100644 index 0000000..dce63b7 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyRequestDto.java @@ -0,0 +1,16 @@ +package org.company.aircompaniesmanager.dto.air.company; + +import java.time.LocalDate; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import lombok.Data; + +@Data +public class AirCompanyRequestDto { + @NotBlank + private String name; + @NotBlank + private String companyType; + @NotNull + private LocalDate foundationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyResponseDto.java b/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyResponseDto.java new file mode 100644 index 0000000..8993676 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyResponseDto.java @@ -0,0 +1,12 @@ +package org.company.aircompaniesmanager.dto.air.company; + +import java.time.LocalDate; +import lombok.Data; + +@Data +public class AirCompanyResponseDto { + private Long id; + private String name; + private String companyType; + private LocalDate foundationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyUpdateRequestDto.java b/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyUpdateRequestDto.java new file mode 100644 index 0000000..837b097 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/air/company/AirCompanyUpdateRequestDto.java @@ -0,0 +1,10 @@ +package org.company.aircompaniesmanager.dto.air.company; + +import javax.validation.constraints.NotBlank; +import lombok.Data; + +@Data +public class AirCompanyUpdateRequestDto { + @NotBlank + private String companyType; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneRequestDto.java b/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneRequestDto.java new file mode 100644 index 0000000..88ee116 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneRequestDto.java @@ -0,0 +1,28 @@ +package org.company.aircompaniesmanager.dto.airplane; + +import java.time.LocalDate; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Positive; +import lombok.Data; + +@Data +public class AirplaneRequestDto { + @NotBlank + private String factorySerialNumber; + @Positive + private Long airCompanyId; + @Positive + @NotNull + private int numberOfFlights; + @Positive + @NotNull + private double flightDistance; + @Positive + @NotNull + private double fuelCapacity; + @NotBlank + private String type; + @NotNull + private LocalDate creationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneResponseDto.java b/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneResponseDto.java new file mode 100644 index 0000000..0ff9a85 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneResponseDto.java @@ -0,0 +1,16 @@ +package org.company.aircompaniesmanager.dto.airplane; + +import java.time.LocalDate; +import lombok.Data; + +@Data +public class AirplaneResponseDto { + private Long id; + private String factorySerialNumber; + private Long airCompanyId; + private int numberOfFlights; + private double flightDistance; + private double fuelCapacity; + private String type; + private LocalDate creationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneUpdateRequestDto.java b/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneUpdateRequestDto.java new file mode 100644 index 0000000..ea8f354 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/airplane/AirplaneUpdateRequestDto.java @@ -0,0 +1,12 @@ +package org.company.aircompaniesmanager.dto.airplane; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Positive; +import lombok.Data; + +@Data +public class AirplaneUpdateRequestDto { + @NotNull + @Positive + private Long airCompanyId; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java new file mode 100644 index 0000000..f1953dd --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java @@ -0,0 +1,31 @@ +package org.company.aircompaniesmanager.dto.flight; + +import java.time.LocalDateTime; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Positive; +import lombok.Data; + +@Data +public class FlightRequestDto { + @NotNull + @Positive + private Long airCompanyId; + @NotNull + @Positive + private Long airplaneId; + @NotBlank + private String departureCountry; + @NotBlank + private String destinationCountry; + @NotNull + @Positive + private double distance; + @NotNull + @Positive + private double estimatedTime; + private LocalDateTime startTime; + private LocalDateTime endTime; + private LocalDateTime delayStartTime; + private LocalDateTime creationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java new file mode 100644 index 0000000..23f253b --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java @@ -0,0 +1,20 @@ +package org.company.aircompaniesmanager.dto.flight; + +import java.time.LocalDateTime; +import lombok.Data; +import org.company.aircompaniesmanager.model.Flight.Status; + +@Data +public class FlightResponseDto { + private Long id; + private Status status; + private Long airplaneId; + private String departureCountry; + private String destinationCountry; + private double distance; + private double estimatedTime; + private LocalDateTime startTime; + private LocalDateTime endTime; + private LocalDateTime delayStartTime; + private LocalDateTime creationDate; +} diff --git a/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightUpdateRequestDto.java b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightUpdateRequestDto.java new file mode 100644 index 0000000..f68b2ba --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightUpdateRequestDto.java @@ -0,0 +1,11 @@ +package org.company.aircompaniesmanager.dto.flight; + +import javax.validation.constraints.NotBlank; +import lombok.Data; +import org.company.aircompaniesmanager.model.Flight.Status; + +@Data +public class FlightUpdateRequestDto { + @NotBlank + private Status status; +} diff --git a/src/main/java/org/company/aircompaniesmanager/exception/EntityNotFoundException.java b/src/main/java/org/company/aircompaniesmanager/exception/EntityNotFoundException.java new file mode 100644 index 0000000..49d20c8 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/exception/EntityNotFoundException.java @@ -0,0 +1,7 @@ +package org.company.aircompaniesmanager.exception; + +public class EntityNotFoundException extends RuntimeException { + public EntityNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/org/company/aircompaniesmanager/mapper/AirCompanyMapper.java b/src/main/java/org/company/aircompaniesmanager/mapper/AirCompanyMapper.java new file mode 100644 index 0000000..f9a326d --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/mapper/AirCompanyMapper.java @@ -0,0 +1,14 @@ +package org.company.aircompaniesmanager.mapper; + +import org.company.aircompaniesmanager.config.MapperConfig; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyRequestDto; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyResponseDto; +import org.company.aircompaniesmanager.model.AirCompany; +import org.mapstruct.Mapper; + +@Mapper(config = MapperConfig.class) +public interface AirCompanyMapper { + AirCompanyResponseDto toDto(AirCompany airCompany); + + AirCompany toEntity(AirCompanyRequestDto requestDto); +} diff --git a/src/main/java/org/company/aircompaniesmanager/mapper/AirplaneMapper.java b/src/main/java/org/company/aircompaniesmanager/mapper/AirplaneMapper.java new file mode 100644 index 0000000..9a4b866 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/mapper/AirplaneMapper.java @@ -0,0 +1,16 @@ +package org.company.aircompaniesmanager.mapper; + +import org.company.aircompaniesmanager.config.MapperConfig; +import org.company.aircompaniesmanager.dto.airplane.AirplaneRequestDto; +import org.company.aircompaniesmanager.dto.airplane.AirplaneResponseDto; +import org.company.aircompaniesmanager.model.Airplane; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper(config = MapperConfig.class) +public interface AirplaneMapper { + @Mapping(target = "airCompanyId", source = "airCompany.id") + AirplaneResponseDto toDto(Airplane airplane); + + Airplane toEntity(AirplaneRequestDto requestDto); +} diff --git a/src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java b/src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java new file mode 100644 index 0000000..5199854 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java @@ -0,0 +1,16 @@ +package org.company.aircompaniesmanager.mapper; + +import org.company.aircompaniesmanager.config.MapperConfig; +import org.company.aircompaniesmanager.dto.flight.FlightRequestDto; +import org.company.aircompaniesmanager.dto.flight.FlightResponseDto; +import org.company.aircompaniesmanager.model.Flight; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper(config = MapperConfig.class) +public interface FlightMapper { + @Mapping(target = "airplaneId", source = "airplane.id") + FlightResponseDto toDto(Flight flight); + + Flight toEntity(FlightRequestDto requestDto); +} diff --git a/src/main/java/org/company/aircompaniesmanager/model/AirCompany.java b/src/main/java/org/company/aircompaniesmanager/model/AirCompany.java index 69abfa6..1b0e842 100644 --- a/src/main/java/org/company/aircompaniesmanager/model/AirCompany.java +++ b/src/main/java/org/company/aircompaniesmanager/model/AirCompany.java @@ -1,10 +1,38 @@ package org.company.aircompaniesmanager.model; import java.time.LocalDate; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import lombok.Data; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; +@Data +@Entity +@Table(name = "air_companies") +@SQLDelete(sql = "UPDATE air_companies SET is_deleted = true WHERE id=?") +@Where(clause = "is_deleted=false") public class AirCompany { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(nullable = false, unique = true, length = 72) private String name; + @Column(nullable = false, length = 56) private String companyType; + @Column(nullable = false) private LocalDate foundationDate; + @Column(name = "is_deleted", nullable = false, columnDefinition = "boolean default false") + private boolean isDeleted = false; + + public AirCompany() { + } + + public AirCompany(Long id) { + this.id = id; + } } diff --git a/src/main/java/org/company/aircompaniesmanager/model/Airplane.java b/src/main/java/org/company/aircompaniesmanager/model/Airplane.java index 2d61013..e5d0d6d 100644 --- a/src/main/java/org/company/aircompaniesmanager/model/Airplane.java +++ b/src/main/java/org/company/aircompaniesmanager/model/Airplane.java @@ -1,14 +1,50 @@ package org.company.aircompaniesmanager.model; import java.time.LocalDate; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; +@Data +@Entity +@Table(name = "airplanes") +@SQLDelete(sql = "UPDATE airplanes SET is_deleted = true WHERE id=?") +@Where(clause = "is_deleted=false") public class Airplane { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(nullable = false, length = 12) private String factorySerialNumber; - private Long airCompanyId; + @ToString.Exclude + @EqualsAndHashCode.Exclude + @JoinColumn(name = "air_company_id") + @ManyToOne(fetch = FetchType.LAZY) + private AirCompany airCompany; private int numberOfFlights; private double flightDistance; private double fuelCapacity; + @Column(nullable = false, length = 56) private String type; private LocalDate creationDate; + @Column(name = "is_deleted", nullable = false, columnDefinition = "boolean default false") + private boolean isDeleted = false; + + public Airplane() { + } + + public Airplane(Long id) { + this.id = id; + } } diff --git a/src/main/java/org/company/aircompaniesmanager/model/Flight.java b/src/main/java/org/company/aircompaniesmanager/model/Flight.java index c7a30e6..8e3df0a 100644 --- a/src/main/java/org/company/aircompaniesmanager/model/Flight.java +++ b/src/main/java/org/company/aircompaniesmanager/model/Flight.java @@ -1,20 +1,59 @@ package org.company.aircompaniesmanager.model; import java.time.LocalDateTime; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; +@Data +@Entity +@Table(name = "flights") +@SQLDelete(sql = "UPDATE flights SET is_deleted = true WHERE id=?") +@Where(clause = "is_deleted=false") public class Flight { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Enumerated(EnumType.STRING) + @Column(nullable = false) private Status status; - private Long airCompanyId; - private Long airplaneId; + @ToString.Exclude + @EqualsAndHashCode.Exclude + @JoinColumn(name = "air_company_id", nullable = false) + @ManyToOne(fetch = FetchType.LAZY) + private AirCompany airCompany; + @ToString.Exclude + @EqualsAndHashCode.Exclude + @JoinColumn(name = "airplane_id", nullable = false) + @ManyToOne(fetch = FetchType.LAZY) + private Airplane airplane; + @Column(nullable = false) private String departureCountry; + @Column(nullable = false) private String destinationCountry; + @Column(nullable = false) private double distance; + @Column(nullable = false) private double estimatedTime; private LocalDateTime startTime; private LocalDateTime endTime; private LocalDateTime delayStartTime; private LocalDateTime creationDate; + @Column(name = "is_deleted", nullable = false, columnDefinition = "boolean default false") + private boolean isDeleted = false; public enum Status { ACTIVE, diff --git a/src/main/java/org/company/aircompaniesmanager/repository/AirCompanyRepository.java b/src/main/java/org/company/aircompaniesmanager/repository/AirCompanyRepository.java new file mode 100644 index 0000000..aa3ac05 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/repository/AirCompanyRepository.java @@ -0,0 +1,13 @@ +package org.company.aircompaniesmanager.repository; + +import java.util.Optional; +import org.company.aircompaniesmanager.model.AirCompany; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AirCompanyRepository extends JpaRepository { + Optional findByName(String name); + + void deleteByName(String name); +} diff --git a/src/main/java/org/company/aircompaniesmanager/repository/AirplaneRepository.java b/src/main/java/org/company/aircompaniesmanager/repository/AirplaneRepository.java new file mode 100644 index 0000000..deedb95 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/repository/AirplaneRepository.java @@ -0,0 +1,7 @@ +package org.company.aircompaniesmanager.repository; + +import org.company.aircompaniesmanager.model.Airplane; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AirplaneRepository extends JpaRepository { +} diff --git a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java new file mode 100644 index 0000000..114423a --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java @@ -0,0 +1,7 @@ +package org.company.aircompaniesmanager.repository; + +import org.company.aircompaniesmanager.model.Flight; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FlightRepository extends JpaRepository { +} diff --git a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java new file mode 100644 index 0000000..7740473 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java @@ -0,0 +1,18 @@ +package org.company.aircompaniesmanager.service.air.company; + +import java.util.List; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyRequestDto; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyResponseDto; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyUpdateRequestDto; + +public interface AirCompanyService { + List findAll(); + + AirCompanyResponseDto findById(Long id); + + AirCompanyResponseDto save(AirCompanyRequestDto requestDto); + + AirCompanyResponseDto updateById(Long id, AirCompanyUpdateRequestDto requestDto); + + void deleteById(Long id); +} diff --git a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java new file mode 100644 index 0000000..bede1c5 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java @@ -0,0 +1,61 @@ +package org.company.aircompaniesmanager.service.air.company; + +import java.util.List; +import java.util.stream.Collectors; +import javax.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyRequestDto; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyResponseDto; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyUpdateRequestDto; +import org.company.aircompaniesmanager.mapper.AirCompanyMapper; +import org.company.aircompaniesmanager.model.AirCompany; +import org.company.aircompaniesmanager.repository.AirCompanyRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class AirCompanyServiceImpl implements AirCompanyService { + private final AirCompanyMapper airCompanyMapper; + private final AirCompanyRepository airCompanyRepository; + + @Override + public List findAll() { + return airCompanyRepository.findAll().stream() + .map(airCompanyMapper::toDto) + .collect(Collectors.toList()); + } + + @Override + public AirCompanyResponseDto findById(Long id) { + AirCompany airCompany = airCompanyRepository.findById(id).orElseThrow( + () -> new EntityNotFoundException("Can't find air company by id: " + id) + ); + return airCompanyMapper.toDto(airCompany); + } + + @Override + public AirCompanyResponseDto save(AirCompanyRequestDto requestDto) { + AirCompany airCompany = airCompanyRepository.save(airCompanyMapper.toEntity(requestDto)); + return airCompanyMapper.toDto(airCompany); + } + + @Override + @Transactional + public AirCompanyResponseDto updateById( + Long id, + AirCompanyUpdateRequestDto requestDto + ) { + AirCompany airCompany = airCompanyRepository.findById(id).orElseThrow( + () -> new EntityNotFoundException("Can't find air company by id: " + id) + ); + airCompany.setCompanyType(requestDto.getCompanyType()); + return airCompanyMapper.toDto(airCompanyRepository.save(airCompany)); + } + + @Override + @Transactional + public void deleteById(Long id) { + airCompanyRepository.deleteById(id); + } +} diff --git a/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java new file mode 100644 index 0000000..951f820 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java @@ -0,0 +1,16 @@ +package org.company.aircompaniesmanager.service.airplane; + +import java.util.List; +import org.company.aircompaniesmanager.dto.airplane.AirplaneRequestDto; +import org.company.aircompaniesmanager.dto.airplane.AirplaneResponseDto; +import org.company.aircompaniesmanager.dto.airplane.AirplaneUpdateRequestDto; + +public interface AirplaneService { + AirplaneResponseDto findById(Long id); + + List findAll(); + + AirplaneResponseDto save(AirplaneRequestDto requestDto); + + AirplaneResponseDto updateCompany(Long id, AirplaneUpdateRequestDto requestDto); +} diff --git a/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java new file mode 100644 index 0000000..3e4c0c4 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java @@ -0,0 +1,56 @@ +package org.company.aircompaniesmanager.service.airplane; + +import java.util.List; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.company.aircompaniesmanager.dto.airplane.AirplaneRequestDto; +import org.company.aircompaniesmanager.dto.airplane.AirplaneResponseDto; +import org.company.aircompaniesmanager.dto.airplane.AirplaneUpdateRequestDto; +import org.company.aircompaniesmanager.exception.EntityNotFoundException; +import org.company.aircompaniesmanager.mapper.AirplaneMapper; +import org.company.aircompaniesmanager.model.AirCompany; +import org.company.aircompaniesmanager.model.Airplane; +import org.company.aircompaniesmanager.repository.AirplaneRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class AirplaneServiceImpl implements AirplaneService { + private final AirplaneMapper airplaneMapper; + private final AirplaneRepository airplaneRepository; + + @Override + public AirplaneResponseDto findById(Long id) { + Airplane airplane = airplaneRepository.findById(id).orElseThrow(() -> + new EntityNotFoundException("Can't find airplane with id: " + id) + ); + return airplaneMapper.toDto(airplane); + } + + @Override + public List findAll() { + return airplaneRepository.findAll().stream() + .map(airplaneMapper::toDto) + .collect(Collectors.toList()); + } + + @Override + public AirplaneResponseDto save(AirplaneRequestDto requestDto) { + Airplane airplane = airplaneMapper.toEntity(requestDto); + if (requestDto.getAirCompanyId() != null) { + airplane.setAirCompany(new AirCompany(requestDto.getAirCompanyId())); + } + return airplaneMapper.toDto(airplaneRepository.save(airplane)); + } + + @Override + @Transactional + public AirplaneResponseDto updateCompany(Long id, AirplaneUpdateRequestDto requestDto) { + Airplane airplane = airplaneRepository.findById(id).orElseThrow(() -> + new EntityNotFoundException("Can't find airplane with id: " + id) + ); + airplane.setAirCompany(new AirCompany(id)); + return airplaneMapper.toDto(airplaneRepository.save(airplane)); + } +} diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java new file mode 100644 index 0000000..0ba0d68 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java @@ -0,0 +1,16 @@ +package org.company.aircompaniesmanager.service.flight; + +import java.util.List; +import org.company.aircompaniesmanager.dto.flight.FlightRequestDto; +import org.company.aircompaniesmanager.dto.flight.FlightResponseDto; +import org.company.aircompaniesmanager.dto.flight.FlightUpdateRequestDto; + +public interface FlightService { + List findAll(); + + FlightResponseDto findById(Long id); + + FlightResponseDto save(FlightRequestDto requestDto); + + FlightResponseDto updateStatus(Long id, FlightUpdateRequestDto requestDto); +} diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java new file mode 100644 index 0000000..564ba05 --- /dev/null +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java @@ -0,0 +1,73 @@ +package org.company.aircompaniesmanager.service.flight; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.company.aircompaniesmanager.dto.flight.FlightRequestDto; +import org.company.aircompaniesmanager.dto.flight.FlightResponseDto; +import org.company.aircompaniesmanager.dto.flight.FlightUpdateRequestDto; +import org.company.aircompaniesmanager.exception.EntityNotFoundException; +import org.company.aircompaniesmanager.mapper.FlightMapper; +import org.company.aircompaniesmanager.model.AirCompany; +import org.company.aircompaniesmanager.model.Airplane; +import org.company.aircompaniesmanager.model.Flight; +import org.company.aircompaniesmanager.model.Flight.Status; +import org.company.aircompaniesmanager.repository.FlightRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class FlightServiceImpl implements FlightService { + private final FlightMapper flightMapper; + private final FlightRepository flightRepository; + + @Override + public List findAll() { + return flightRepository.findAll().stream() + .map(flightMapper::toDto) + .collect(Collectors.toList()); + } + + @Override + public FlightResponseDto findById(Long id) { + Flight flight = flightRepository.findById(id).orElseThrow(() -> + new EntityNotFoundException("Can't find flight with id: " + id) + ); + return flightMapper.toDto(flight); + } + + @Override + public FlightResponseDto save(FlightRequestDto requestDto) { + Flight flight = flightMapper.toEntity(requestDto); + flight.setAirCompany(new AirCompany(requestDto.getAirCompanyId())); + flight.setAirplane(new Airplane(requestDto.getAirplaneId())); + flight.setStatus(Status.PENDING); + flight.setCreationDate(LocalDateTime.now()); + return flightMapper.toDto(flightRepository.save(flight)); + } + + @Override + @Transactional + public FlightResponseDto updateStatus(Long id, FlightUpdateRequestDto requestDto) { + Flight flight = flightRepository.findById(id).orElseThrow(() -> + new EntityNotFoundException("Can't find flight with id: " + id) + ); + Status status = requestDto.getStatus(); + flight.setStatus(status); + switch (status) { + case DELAYED: + flight.setDelayStartTime(LocalDateTime.now()); + break; + case ACTIVE: + flight.setStartTime(LocalDateTime.now()); + break; + case COMPLETED: + flight.setEndTime(LocalDateTime.now()); + break; + default: break; + } + return flightMapper.toDto(flightRepository.save(flight)); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5a8df30..f2338ce 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,13 @@ spring.application.name=air-companies-manager server.servlet.context-path=/api +spring.datasource.url=jdbc:mysql://localhost:3306/air_companies_management +spring.datasource.username=root +spring.datasource.password=${DB_PASSWORD} +spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect +spring.jpa.open-in-view=false +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.defer-datasource-initialization=true +spring.jackson.deserialization.fail-on-unknown-properties=true +spring.datasource.initialization-mode=always +spring.datasource.data=classpath:data/data.sql diff --git a/src/main/resources/data/data.sql b/src/main/resources/data/data.sql new file mode 100644 index 0000000..bdb0595 --- /dev/null +++ b/src/main/resources/data/data.sql @@ -0,0 +1,35 @@ +INSERT INTO air_companies (name, company_type, foundation_date) +VALUES ('name', 'type', '2024-01-01'), + ('Delta Air Lines', 'Airlines', '1928-12-03'), + ('American Airlines', 'Airlines', '1926-04-15'), + ('Southwest Airlines', 'Airlines', '1967-03-15'), + ('Spirit Airlines', 'Airlines', '1983-01-01'), + ('SpaceX', 'Astronautics', '2002-03-14'), + ('Lockheed Martin', 'Aerospace', '1995-03-01'), + ('Northrop Grumman Corporation', 'Aerospace', '1994-01-01'), + ('Boeing Co', 'Airplane', '1916-07-15'); + +INSERT INTO airplanes ( + factory_serial_number, + air_company_id, + number_of_flights, + flight_distance, + fuel_capacity, + type, + creation_date +) VALUES ('number', 1, 12, 3000000, 1200, 'Airbus', '2024-01-01'), + ('30020B', 1, 12, 2039, 1500, 'Airbus', '2024-01-01'), + ('27620B', 2, 49, 35735, 1800, 'Airbus', '2024-01-01'), + ('14920B', 3, 44, 38787, 3000, 'Airbus', '2024-01-01'), + ('14227B', 4, 35, 36219, 2500, 'Airbus', '2024-01-01'), + ('19027C', 5, 178, 452897, 3700, 'Airbus', '2024-01-01'), + ('90047E', 6, 16, 1783, 4500, 'Airbus', '2024-01-01'), + ('20954K', 7, 866, 900000, 5000, 'Airbus', '2024-01-01'), + ('27891B', 8, 136, 519697, 3444, 'Airbus', '2024-01-01'), + ('21987J', 9, 246, 37487, 1200, 'Airbus', '2024-01-01'), + ('91331L', 5, 468, 800653, 1200, 'Airbus', '2024-01-01'), + ('17979P', 5, 222, 652333, 1200, 'Airbus', '2024-01-01'), + ('18994O', 6, 290, 756678, 1200, 'Airbus', '2024-01-01'), + ('17987I', 6, 134, 123345, 1200, 'Airbus', '2024-01-01'), + ('79849S', 7, 65, 75632, 1200, 'Airbus', '2024-01-01'), + ('19894R', 7, 211, 253000, 1200, 'Airbus', '2024-01-01'); diff --git a/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java b/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java index b2a812b..1235fcd 100644 --- a/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java +++ b/src/test/java/org/company/aircompaniesmanager/AirCompaniesManagerApplicationTests.java @@ -1,11 +1,7 @@ package org.company.aircompaniesmanager; -import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class AirCompaniesManagerApplicationTests { - @Test - void contextLoads() { - } } From c155695bc63da2323c6f22c0e8d416a0eebe0218 Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Thu, 28 Mar 2024 19:45:48 +0200 Subject: [PATCH 04/11] pageable concept added --- .../aircompaniesmanager/controller/AirCompanyController.java | 5 +++-- .../aircompaniesmanager/controller/AirplaneController.java | 5 +++-- .../aircompaniesmanager/controller/FlightController.java | 5 +++-- .../service/air/company/AirCompanyService.java | 3 ++- .../service/air/company/AirCompanyServiceImpl.java | 5 +++-- .../service/airplane/AirplaneService.java | 3 ++- .../service/airplane/AirplaneServiceImpl.java | 5 +++-- .../aircompaniesmanager/service/flight/FlightService.java | 3 ++- .../service/flight/FlightServiceImpl.java | 5 +++-- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java b/src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java index 06b7bb2..0ffc47b 100644 --- a/src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java +++ b/src/main/java/org/company/aircompaniesmanager/controller/AirCompanyController.java @@ -9,6 +9,7 @@ import org.company.aircompaniesmanager.dto.air.company.AirCompanyResponseDto; import org.company.aircompaniesmanager.dto.air.company.AirCompanyUpdateRequestDto; import org.company.aircompaniesmanager.service.air.company.AirCompanyService; +import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -31,8 +32,8 @@ public class AirCompanyController { @GetMapping @Operation(summary = "Find all air companies", description = "Retrieves detailed information about all companies") - private List findAll() { - return airCompanyService.findAll(); + private List findAll(Pageable pageable) { + return airCompanyService.findAll(pageable); } @GetMapping("/{id}") diff --git a/src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java b/src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java index 23be439..57ae203 100644 --- a/src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java +++ b/src/main/java/org/company/aircompaniesmanager/controller/AirplaneController.java @@ -8,6 +8,7 @@ import org.company.aircompaniesmanager.dto.airplane.AirplaneResponseDto; import org.company.aircompaniesmanager.dto.airplane.AirplaneUpdateRequestDto; import org.company.aircompaniesmanager.service.airplane.AirplaneService; +import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -29,8 +30,8 @@ public class AirplaneController { @GetMapping @Operation(summary = "Find all airplanes", description = "Retrieves detailed information about all airplanes") - public List findAll() { - return airplaneService.findAll(); + public List findAll(Pageable pageable) { + return airplaneService.findAll(pageable); } @GetMapping("/{id}") diff --git a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java index 965b48f..bb9fbf8 100644 --- a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java +++ b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java @@ -8,6 +8,7 @@ import org.company.aircompaniesmanager.dto.flight.FlightResponseDto; import org.company.aircompaniesmanager.dto.flight.FlightUpdateRequestDto; import org.company.aircompaniesmanager.service.flight.FlightService; +import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -29,8 +30,8 @@ public class FlightController { @GetMapping @Operation(summary = "Find all flights", description = "Retrieves detailed information about all flights") - List findAll() { - return flightService.findAll(); + List findAll(Pageable pageable) { + return flightService.findAll(pageable); } @GetMapping("/{id}") diff --git a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java index 7740473..bcbe687 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java +++ b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java @@ -4,9 +4,10 @@ import org.company.aircompaniesmanager.dto.air.company.AirCompanyRequestDto; import org.company.aircompaniesmanager.dto.air.company.AirCompanyResponseDto; import org.company.aircompaniesmanager.dto.air.company.AirCompanyUpdateRequestDto; +import org.springframework.data.domain.Pageable; public interface AirCompanyService { - List findAll(); + List findAll(Pageable pageable); AirCompanyResponseDto findById(Long id); diff --git a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java index bede1c5..1973aca 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java +++ b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java @@ -10,6 +10,7 @@ import org.company.aircompaniesmanager.mapper.AirCompanyMapper; import org.company.aircompaniesmanager.model.AirCompany; import org.company.aircompaniesmanager.repository.AirCompanyRepository; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,8 +21,8 @@ public class AirCompanyServiceImpl implements AirCompanyService { private final AirCompanyRepository airCompanyRepository; @Override - public List findAll() { - return airCompanyRepository.findAll().stream() + public List findAll(Pageable pageable) { + return airCompanyRepository.findAll(pageable).stream() .map(airCompanyMapper::toDto) .collect(Collectors.toList()); } diff --git a/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java index 951f820..c8166b9 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java +++ b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneService.java @@ -4,11 +4,12 @@ import org.company.aircompaniesmanager.dto.airplane.AirplaneRequestDto; import org.company.aircompaniesmanager.dto.airplane.AirplaneResponseDto; import org.company.aircompaniesmanager.dto.airplane.AirplaneUpdateRequestDto; +import org.springframework.data.domain.Pageable; public interface AirplaneService { AirplaneResponseDto findById(Long id); - List findAll(); + List findAll(Pageable pageable); AirplaneResponseDto save(AirplaneRequestDto requestDto); diff --git a/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java index 3e4c0c4..54aad90 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java +++ b/src/main/java/org/company/aircompaniesmanager/service/airplane/AirplaneServiceImpl.java @@ -11,6 +11,7 @@ import org.company.aircompaniesmanager.model.AirCompany; import org.company.aircompaniesmanager.model.Airplane; import org.company.aircompaniesmanager.repository.AirplaneRepository; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -29,8 +30,8 @@ public AirplaneResponseDto findById(Long id) { } @Override - public List findAll() { - return airplaneRepository.findAll().stream() + public List findAll(Pageable pageable) { + return airplaneRepository.findAll(pageable).stream() .map(airplaneMapper::toDto) .collect(Collectors.toList()); } diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java index 0ba0d68..6468623 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java @@ -4,9 +4,10 @@ import org.company.aircompaniesmanager.dto.flight.FlightRequestDto; import org.company.aircompaniesmanager.dto.flight.FlightResponseDto; import org.company.aircompaniesmanager.dto.flight.FlightUpdateRequestDto; +import org.springframework.data.domain.Pageable; public interface FlightService { - List findAll(); + List findAll(Pageable pageable); FlightResponseDto findById(Long id); diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java index 564ba05..fbbb8f0 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java @@ -14,6 +14,7 @@ import org.company.aircompaniesmanager.model.Flight; import org.company.aircompaniesmanager.model.Flight.Status; import org.company.aircompaniesmanager.repository.FlightRepository; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -24,8 +25,8 @@ public class FlightServiceImpl implements FlightService { private final FlightRepository flightRepository; @Override - public List findAll() { - return flightRepository.findAll().stream() + public List findAll(Pageable pageable) { + return flightRepository.findAll(pageable).stream() .map(flightMapper::toDto) .collect(Collectors.toList()); } From cddc76eba9ece11ea3b407ae7547c90b56cdc24b Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Thu, 28 Mar 2024 20:57:45 +0200 Subject: [PATCH 05/11] added endpoint to retrieve flights by status and company --- .../controller/FlightController.java | 15 +++++++++++++ .../dto/flight/FlightResponseDto.java | 1 + .../mapper/FlightMapper.java | 1 + .../repository/FlightRepository.java | 4 ++++ .../air/company/AirCompanyService.java | 2 ++ .../air/company/AirCompanyServiceImpl.java | 8 +++++++ .../service/flight/FlightService.java | 6 ++++++ .../service/flight/FlightServiceImpl.java | 21 +++++++++++++++++++ 8 files changed, 58 insertions(+) diff --git a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java index bb9fbf8..099838b 100644 --- a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java +++ b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -34,6 +35,20 @@ List findAll(Pageable pageable) { return flightService.findAll(pageable); } + @GetMapping("/{status}/company") + @Operation( + summary = "Find flight by company", + description = "Retrieves detailed information " + + "about flights by specified company" + ) + List findAllByCompanyNameAndStatus( + @RequestParam String name, + @PathVariable String status, + Pageable pageable + ) { + return flightService.findAllByCompanyName(name, status, pageable); + } + @GetMapping("/{id}") @Operation(summary = "Find flight by id", description = "Retrieves detailed information about specified flight") diff --git a/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java index 23f253b..8b9bc23 100644 --- a/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java +++ b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightResponseDto.java @@ -8,6 +8,7 @@ public class FlightResponseDto { private Long id; private Status status; + private Long airCompanyId; private Long airplaneId; private String departureCountry; private String destinationCountry; diff --git a/src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java b/src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java index 5199854..9ca83a7 100644 --- a/src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java +++ b/src/main/java/org/company/aircompaniesmanager/mapper/FlightMapper.java @@ -10,6 +10,7 @@ @Mapper(config = MapperConfig.class) public interface FlightMapper { @Mapping(target = "airplaneId", source = "airplane.id") + @Mapping(target = "airCompanyId", source = "airCompany.id") FlightResponseDto toDto(Flight flight); Flight toEntity(FlightRequestDto requestDto); diff --git a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java index 114423a..46428c4 100644 --- a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java +++ b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java @@ -1,7 +1,11 @@ package org.company.aircompaniesmanager.repository; +import java.util.List; import org.company.aircompaniesmanager.model.Flight; +import org.company.aircompaniesmanager.model.Flight.Status; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; public interface FlightRepository extends JpaRepository { + List findAllByAirCompanyIdAndStatus(Long id, Status status, Pageable pageable); } diff --git a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java index bcbe687..df95170 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java +++ b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyService.java @@ -11,6 +11,8 @@ public interface AirCompanyService { AirCompanyResponseDto findById(Long id); + AirCompanyResponseDto findByName(String name); + AirCompanyResponseDto save(AirCompanyRequestDto requestDto); AirCompanyResponseDto updateById(Long id, AirCompanyUpdateRequestDto requestDto); diff --git a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java index 1973aca..bdff0ca 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java +++ b/src/main/java/org/company/aircompaniesmanager/service/air/company/AirCompanyServiceImpl.java @@ -35,6 +35,14 @@ public AirCompanyResponseDto findById(Long id) { return airCompanyMapper.toDto(airCompany); } + @Override + public AirCompanyResponseDto findByName(String name) { + AirCompany airCompany = airCompanyRepository.findByName(name).orElseThrow( + () -> new EntityNotFoundException("Can't find air company by name: " + name) + ); + return airCompanyMapper.toDto(airCompany); + } + @Override public AirCompanyResponseDto save(AirCompanyRequestDto requestDto) { AirCompany airCompany = airCompanyRepository.save(airCompanyMapper.toEntity(requestDto)); diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java index 6468623..88abc4f 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java @@ -9,6 +9,12 @@ public interface FlightService { List findAll(Pageable pageable); + List findAllByCompanyName( + String companyName, + String statusString, + Pageable pageable + ); + FlightResponseDto findById(Long id); FlightResponseDto save(FlightRequestDto requestDto); diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java index fbbb8f0..07c2f72 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; +import org.company.aircompaniesmanager.dto.air.company.AirCompanyResponseDto; import org.company.aircompaniesmanager.dto.flight.FlightRequestDto; import org.company.aircompaniesmanager.dto.flight.FlightResponseDto; import org.company.aircompaniesmanager.dto.flight.FlightUpdateRequestDto; @@ -14,6 +15,7 @@ import org.company.aircompaniesmanager.model.Flight; import org.company.aircompaniesmanager.model.Flight.Status; import org.company.aircompaniesmanager.repository.FlightRepository; +import org.company.aircompaniesmanager.service.air.company.AirCompanyService; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,6 +24,7 @@ @RequiredArgsConstructor public class FlightServiceImpl implements FlightService { private final FlightMapper flightMapper; + private final AirCompanyService airCompanyService; private final FlightRepository flightRepository; @Override @@ -31,6 +34,24 @@ public List findAll(Pageable pageable) { .collect(Collectors.toList()); } + @Override + public List findAllByCompanyName( + String name, + String statusString, + Pageable pageable + ) { + Status status = Status.valueOf(statusString.toUpperCase()); + AirCompanyResponseDto airCompanyResponseDto = airCompanyService.findByName(name); + return flightRepository + .findAllByAirCompanyIdAndStatus( + airCompanyResponseDto.getId(), + status, + pageable) + .stream() + .map(flightMapper::toDto) + .collect(Collectors.toList()); + } + @Override public FlightResponseDto findById(Long id) { Flight flight = flightRepository.findById(id).orElseThrow(() -> From 00f33f7fc9ba5cced5c6608004b2c42e23f7fd43 Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Thu, 28 Mar 2024 20:58:33 +0200 Subject: [PATCH 06/11] changed text of operation --- .../aircompaniesmanager/controller/FlightController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java index 099838b..8aab4de 100644 --- a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java +++ b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java @@ -37,9 +37,9 @@ List findAll(Pageable pageable) { @GetMapping("/{status}/company") @Operation( - summary = "Find flight by company", + summary = "Find flight by status and company", description = "Retrieves detailed information " - + "about flights by specified company" + + "about flights by specified company with specified status" ) List findAllByCompanyNameAndStatus( @RequestParam String name, From 8c7b0cbbc0d4496320aec8f787dddd4ba3763d69 Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Thu, 28 Mar 2024 21:35:41 +0200 Subject: [PATCH 07/11] realized all required endpoints --- .../controller/FlightController.java | 10 ++++++++++ .../repository/FlightRepository.java | 5 +++++ .../service/flight/FlightService.java | 2 ++ .../service/flight/FlightServiceImpl.java | 10 ++++++++++ 4 files changed, 27 insertions(+) diff --git a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java index 8aab4de..d99af8d 100644 --- a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java +++ b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java @@ -35,6 +35,16 @@ List findAll(Pageable pageable) { return flightService.findAll(pageable); } + @GetMapping("/expired") + @Operation( + summary = "Find flight with active status and expired time", + description = "Retrieves detailed information " + + "about flights by specified status with specified status" + ) + List findAllExpired(Pageable pageable) { + return flightService.findAllExpired(pageable); + } + @GetMapping("/{status}/company") @Operation( summary = "Find flight by status and company", diff --git a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java index 46428c4..f7b7f3c 100644 --- a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java +++ b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java @@ -1,11 +1,16 @@ package org.company.aircompaniesmanager.repository; +import java.time.LocalDateTime; import java.util.List; import org.company.aircompaniesmanager.model.Flight; import org.company.aircompaniesmanager.model.Flight.Status; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; public interface FlightRepository extends JpaRepository { List findAllByAirCompanyIdAndStatus(Long id, Status status, Pageable pageable); + + @Query("SELECT f FROM Flight f WHERE (f.startTime > :localDateTime) AND (f.status = :status)") + List findAllExpired(LocalDateTime localDateTime, Status status, Pageable pageable); } diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java index 88abc4f..b5b77f1 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java @@ -15,6 +15,8 @@ List findAllByCompanyName( Pageable pageable ); + List findAllExpired(Pageable pageable); + FlightResponseDto findById(Long id); FlightResponseDto save(FlightRequestDto requestDto); diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java index 07c2f72..8f08862 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java @@ -23,6 +23,8 @@ @Service @RequiredArgsConstructor public class FlightServiceImpl implements FlightService { + private static final int EXPIRATION_LIMIT_IN_HOURS = 24; + private final FlightMapper flightMapper; private final AirCompanyService airCompanyService; private final FlightRepository flightRepository; @@ -52,6 +54,14 @@ public List findAllByCompanyName( .collect(Collectors.toList()); } + @Override + public List findAllExpired(Pageable pageable) { + LocalDateTime expirationLimit = LocalDateTime.now().minusHours(EXPIRATION_LIMIT_IN_HOURS); + return flightRepository.findAllExpired(expirationLimit, Status.ACTIVE, pageable).stream() + .map(flightMapper::toDto) + .collect(Collectors.toList()); + } + @Override public FlightResponseDto findById(Long id) { Flight flight = flightRepository.findById(id).orElseThrow(() -> From 09eeba64ca608085e0634b8e9183f9295d9d6a58 Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Thu, 28 Mar 2024 22:18:18 +0200 Subject: [PATCH 08/11] added endpoint without realization --- .../controller/FlightController.java | 12 +++++++++++- .../repository/FlightRepository.java | 7 +++++++ .../service/flight/FlightService.java | 2 ++ .../service/flight/FlightServiceImpl.java | 7 +++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java index d99af8d..2d861f6 100644 --- a/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java +++ b/src/main/java/org/company/aircompaniesmanager/controller/FlightController.java @@ -37,7 +37,7 @@ List findAll(Pageable pageable) { @GetMapping("/expired") @Operation( - summary = "Find flight with active status and expired time", + summary = "Find flights with active status and expired time", description = "Retrieves detailed information " + "about flights by specified status with specified status" ) @@ -45,6 +45,16 @@ List findAllExpired(Pageable pageable) { return flightService.findAllExpired(pageable); } + @GetMapping("/overdue") + @Operation( + summary = "Find flights with completed status and overdue time", + description = "Retrieves detailed information " + + "about flights by completed status with overdue time" + ) + List findAllOverdue(Pageable pageable) { + return flightService.findAllOverdue(pageable); + } + @GetMapping("/{status}/company") @Operation( summary = "Find flight by status and company", diff --git a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java index f7b7f3c..5b1f58c 100644 --- a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java +++ b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java @@ -13,4 +13,11 @@ public interface FlightRepository extends JpaRepository { @Query("SELECT f FROM Flight f WHERE (f.startTime > :localDateTime) AND (f.status = :status)") List findAllExpired(LocalDateTime localDateTime, Status status, Pageable pageable); + + @Query( + value = "SELECT * FROM `air_companies_management`.`flights` " + + "WHERE TIMESTAMPDIFF(second, '2016/06/30 12:00', '2018/10/18 12:45') > 10", + nativeQuery = true + ) + List findAllOverdue(Pageable pageable); } diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java index b5b77f1..ad455c7 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightService.java @@ -17,6 +17,8 @@ List findAllByCompanyName( List findAllExpired(Pageable pageable); + List findAllOverdue(Pageable pageable); + FlightResponseDto findById(Long id); FlightResponseDto save(FlightRequestDto requestDto); diff --git a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java index 8f08862..b47698a 100644 --- a/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java +++ b/src/main/java/org/company/aircompaniesmanager/service/flight/FlightServiceImpl.java @@ -62,6 +62,13 @@ public List findAllExpired(Pageable pageable) { .collect(Collectors.toList()); } + @Override + public List findAllOverdue(Pageable pageable) { + return flightRepository.findAllOverdue(pageable).stream() + .map(flightMapper::toDto) + .collect(Collectors.toList()); + } + @Override public FlightResponseDto findById(Long id) { Flight flight = flightRepository.findById(id).orElseThrow(() -> From 58475bab00341a24440fb71f54b65235c64c4ea5 Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Fri, 29 Mar 2024 08:55:16 +0200 Subject: [PATCH 09/11] added optional endpoint --- .../aircompaniesmanager/dto/flight/FlightRequestDto.java | 1 + .../aircompaniesmanager/repository/FlightRepository.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java index f1953dd..b07871d 100644 --- a/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java +++ b/src/main/java/org/company/aircompaniesmanager/dto/flight/FlightRequestDto.java @@ -21,6 +21,7 @@ public class FlightRequestDto { @NotNull @Positive private double distance; + // Estimated time in hours @NotNull @Positive private double estimatedTime; diff --git a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java index 5b1f58c..2b63424 100644 --- a/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java +++ b/src/main/java/org/company/aircompaniesmanager/repository/FlightRepository.java @@ -16,7 +16,7 @@ public interface FlightRepository extends JpaRepository { @Query( value = "SELECT * FROM `air_companies_management`.`flights` " - + "WHERE TIMESTAMPDIFF(second, '2016/06/30 12:00', '2018/10/18 12:45') > 10", + + "WHERE TIMESTAMPDIFF(minute, start_time, end_time) >= estimated_time", nativeQuery = true ) List findAllOverdue(Pageable pageable); From 9011b8e230d9295fe45acd13c91efc5f9b791a38 Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Fri, 29 Mar 2024 09:17:46 +0200 Subject: [PATCH 10/11] fixed swagger --- pom.xml | 4 ++-- src/main/resources/application.properties | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 5be0219..71a2f77 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ 0.2.0 1.5.5.Final 1.18.32 - 2.3.0 + 1.8.0 @@ -65,7 +65,7 @@ org.springdoc - springdoc-openapi-starter-webmvc-ui + springdoc-openapi-ui ${openapi.version} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f2338ce..70683f6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,3 +11,4 @@ spring.jpa.defer-datasource-initialization=true spring.jackson.deserialization.fail-on-unknown-properties=true spring.datasource.initialization-mode=always spring.datasource.data=classpath:data/data.sql +springdoc.swagger-ui.path=/swagger-ui.html From 9c32ccec7977c9f6a0b054b0ef122403f47e586e Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Fri, 29 Mar 2024 13:04:32 +0200 Subject: [PATCH 11/11] added docker --- .env | 8 +++++ Dockerfile | 14 +++++++++ docker-compose.yml | 37 +++++++++++++++++++++++ src/main/resources/application.properties | 5 +-- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.env b/.env new file mode 100644 index 0000000..18edda9 --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +MYSQLDB_DATABASE=air_companies_management +MYSQLDB_USER=root +MYSQLDB_ROOT_PASSWORD=default +MYSQLDB_LOCAL_PORT=3308 +MYSQLDB_DOCKER_PORT=3306 +SPRING_LOCAL_PORT=8088 +SPRING_DOCKER_PORT=8080 +DEBUG_PORT=5005 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5c8ebe8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM openjdk:11 as builder +WORKDIR application +ARG JAR_FILE=target/*.jar +COPY ${JAR_FILE} application.jar +RUN java -Djarmode=layertools -jar application.jar extract + +FROM openjdk:11 +WORKDIR application +COPY --from=builder application/dependencies/ ./ +COPY --from=builder application/spring-boot-loader/ ./ +COPY --from=builder application/snapshot-dependencies/ ./ +COPY --from=builder application/application/ ./ +ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"] +EXPOSE 8080 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b0bd32a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: "3.8" + +services: + mysqldb: + platform: linux/arm64 + image: mysql:latest + restart: unless-stopped + env_file: ./.env + environment: + - MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD + - MYSQL_DATABASE=$MYSQLDB_DATABASE + ports: + - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT + healthcheck: + test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ] + timeout: 30s + retries: 3 + app: + depends_on: + mysqldb: + condition: service_healthy + restart: on-failure + image: air_companies_management + build: . + env_file: ./.env + ports: + - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT + - $DEBUG_PORT:$DEBUG_PORT + environment: + SPRING_APPLICATION_JSON: '{ + "spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE", + "spring.datasource.username" : "$MYSQLDB_USER", + "spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD", + "spring.datasource.driver-class-name" : "com.mysql.cj.jdbc.Driver", + "spring.jpa.hibernate.ddl-auto" : "create-drop" + }' + JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 70683f6..bec7acd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,13 +1,14 @@ spring.application.name=air-companies-manager server.servlet.context-path=/api spring.datasource.url=jdbc:mysql://localhost:3306/air_companies_management +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root -spring.datasource.password=${DB_PASSWORD} +spring.datasource.password=default spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect spring.jpa.open-in-view=false spring.jpa.show-sql=true +spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=create-drop -spring.jpa.defer-datasource-initialization=true spring.jackson.deserialization.fail-on-unknown-properties=true spring.datasource.initialization-mode=always spring.datasource.data=classpath:data/data.sql