From c89e287cf3290a8e7cf7c52a5edd0f864dd15b2e Mon Sep 17 00:00:00 2001 From: Zhdanov Anton Date: Mon, 2 Oct 2023 19:08:18 +0300 Subject: [PATCH 1/4] "added [Car] entity, added table in liquibase, added car repository" --- .github/workflows/ci.yml | 18 ++ checkstyle.xml | 251 ++++++++++++++++++ pom.xml | 55 +++- .../com/project/carsharingapp/model/Car.java | 45 ++++ .../repository/CarRepository.java | 9 + src/main/resources/application.properties | 8 +- .../changelog/changes/create-cars-table.yaml | 46 ++++ .../db/changelog/db.changelog-master.yaml | 3 + src/main/resources/liquibase.properties | 4 + src/test/resources/application.properties | 5 + 10 files changed, 433 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 checkstyle.xml create mode 100644 src/main/java/com/project/carsharingapp/model/Car.java create mode 100644 src/main/java/com/project/carsharingapp/repository/CarRepository.java create mode 100644 src/main/resources/db/changelog/changes/create-cars-table.yaml create mode 100644 src/main/resources/db/changelog/db.changelog-master.yaml create mode 100644 src/main/resources/liquibase.properties create mode 100644 src/test/resources/application.properties 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..f3fe4f7 --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,251 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index a57f66b..5ff5428 100644 --- a/pom.xml +++ b/pom.xml @@ -15,25 +15,27 @@ car-sharing-app 17 + checkstyle.xml org.springframework.boot spring-boot-starter-data-jpa - - org.springframework.boot - spring-boot-starter-security - org.springframework.boot spring-boot-starter-web - org.liquibase + org.liquibase liquibase-core - - + ${liquibase.version} + + + org.liquibase + liquibase-maven-plugin + ${liquibase.version} + mysql mysql-connector-java @@ -54,6 +56,17 @@ spring-security-test test + + com.h2database + h2 + test + + + jakarta.validation + jakarta.validation-api + 2.0.2 + + @@ -70,6 +83,34 @@ + + org.liquibase + liquibase-maven-plugin + ${liquibase.version} + + src/main/resources/liquibase.properties + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.3.0 + + + compile + + check + + + + + ${maven.checkstyle.plugin.configLocation} + true + true + false + src + + diff --git a/src/main/java/com/project/carsharingapp/model/Car.java b/src/main/java/com/project/carsharingapp/model/Car.java new file mode 100644 index 0000000..0f9fd1e --- /dev/null +++ b/src/main/java/com/project/carsharingapp/model/Car.java @@ -0,0 +1,45 @@ +package com.project.carsharingapp.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.math.BigDecimal; +import javax.validation.constraints.PositiveOrZero; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; + +@Getter +@Setter +@Entity +@SQLDelete(sql = "UPDATE cars SET is_deleted = true WHERE id = ?") +@Where(clause = "is_deleted=false") +@Table(name = "cars") +public class Car { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String model; + private String brand; + @PositiveOrZero + private Integer inventory; + @Enumerated(value = EnumType.STRING) + private CarType carType; + @PositiveOrZero + private BigDecimal dailyFree; + @Column(nullable = false) + private boolean isDeleted; + + public enum CarType { + SEDAN, + SUV, + HATCHBACK, + UNIVERSAL + } +} diff --git a/src/main/java/com/project/carsharingapp/repository/CarRepository.java b/src/main/java/com/project/carsharingapp/repository/CarRepository.java new file mode 100644 index 0000000..41dad99 --- /dev/null +++ b/src/main/java/com/project/carsharingapp/repository/CarRepository.java @@ -0,0 +1,9 @@ +package com.project.carsharingapp.repository; + +import com.project.carsharingapp.model.Car; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CarRepository extends JpaRepository { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ce36863..2532435 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ -spring.datasource.url=jdbc:mysql://localhost:3306/car_sharing_db?serverTimezone=UTC -spring.datasource.username=root -spring.datasource.password=root123 -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +#spring.datasource.url=jdbc:mysql://localhost:3306/car_sharing_db?serverTimezone=UTC +#spring.datasource.username=root +#spring.datasource.password=anton7570857 +#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver server.servlet.context-path=/api spring.jpa.hibernate.ddl-auto=validate diff --git a/src/main/resources/db/changelog/changes/create-cars-table.yaml b/src/main/resources/db/changelog/changes/create-cars-table.yaml new file mode 100644 index 0000000..25c495c --- /dev/null +++ b/src/main/resources/db/changelog/changes/create-cars-table.yaml @@ -0,0 +1,46 @@ +databaseChangeLog: + - changeSet: + id: create-cars-table + author: Anton Zhdanov + changes: + - createTable: + tableName: cars + columns: + - column: + name: id + type: bigint + autoIncrement: true + constraints: + primaryKey: true + nullable: false + - column: + name: model + type: varchar(255) + constraints: + nullable: true + - column: + name: brand + type: varchar(255) + constraints: + nullable: true + - column: + name: inventory + type: int + constraints: + nullable: true + - column: + name: car_type + type: varchar(255) + constraints: + nullable: true + - column: + name: daily_free + type: decimal(38, 2) + constraints: + nullable: true + - column: + name: is_deleted + type: boolean + defaultValueBoolean: false + constraints: + nullable: false diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml new file mode 100644 index 0000000..4866ca2 --- /dev/null +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -0,0 +1,3 @@ +databaseChangeLog: + - include: + file: db/changelog/changes/create-cars-table.yaml diff --git a/src/main/resources/liquibase.properties b/src/main/resources/liquibase.properties new file mode 100644 index 0000000..34f4355 --- /dev/null +++ b/src/main/resources/liquibase.properties @@ -0,0 +1,4 @@ +#url=jdbc:mysql://localhost:3306/car_sharing_db?serverTimezone=UTC +#username=root +#password=anton7570857 +#changeLogFile=/db/changelog/db.changelog-master.yaml diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..bc2fdde --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,5 @@ +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect From 4d751c4d8a6a657564404d41df7a046c4d86f3e8 Mon Sep 17 00:00:00 2001 From: Zhdanov Anton Date: Mon, 2 Oct 2023 20:08:54 +0300 Subject: [PATCH 2/4] "[Rental] entity, added rental repository" --- .../project/carsharingapp/model/Rental.java | 34 +++++++++++++++++++ .../repository/RentalRepository.java | 9 +++++ 2 files changed, 43 insertions(+) create mode 100644 src/main/java/com/project/carsharingapp/model/Rental.java create mode 100644 src/main/java/com/project/carsharingapp/repository/RentalRepository.java diff --git a/src/main/java/com/project/carsharingapp/model/Rental.java b/src/main/java/com/project/carsharingapp/model/Rental.java new file mode 100644 index 0000000..3e26465 --- /dev/null +++ b/src/main/java/com/project/carsharingapp/model/Rental.java @@ -0,0 +1,34 @@ +package com.project.carsharingapp.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.time.LocalDateTime; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; + +@Getter +@Setter +@SQLDelete(sql = "UPDATE rentals SET is_deleted = true WHERE id = ?") +@Where(clause = "is_deleted=false") +@Entity +@Table(name = "rentals") +public class Rental { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private LocalDateTime rentalDate; + private LocalDateTime returnDate; + private LocalDateTime actualReturnDate; + //@OneToOne(fetch = FetchType.LAZY) + //private Car car; + // @ManyToOne(fetch = FetchType.LAZY) + // private User user; + @Column(nullable = false) + private boolean isDeleted; +} diff --git a/src/main/java/com/project/carsharingapp/repository/RentalRepository.java b/src/main/java/com/project/carsharingapp/repository/RentalRepository.java new file mode 100644 index 0000000..306d34e --- /dev/null +++ b/src/main/java/com/project/carsharingapp/repository/RentalRepository.java @@ -0,0 +1,9 @@ +package com.project.carsharingapp.repository; + +import com.project.carsharingapp.model.Rental; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface RentalRepository extends JpaRepository { +} From 6bba46bfa00d8817b9a5e815cb17cfbf845705d7 Mon Sep 17 00:00:00 2001 From: Zhdanov Anton Date: Mon, 2 Oct 2023 20:31:24 +0300 Subject: [PATCH 3/4] "[Car] added improvements after review" --- src/main/java/com/project/carsharingapp/model/Car.java | 7 +++++-- src/main/resources/application.properties | 1 - .../resources/db/changelog/changes/create-cars-table.yaml | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/project/carsharingapp/model/Car.java b/src/main/java/com/project/carsharingapp/model/Car.java index 0f9fd1e..a09f6c5 100644 --- a/src/main/java/com/project/carsharingapp/model/Car.java +++ b/src/main/java/com/project/carsharingapp/model/Car.java @@ -15,9 +15,9 @@ import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.Where; +@Entity @Getter @Setter -@Entity @SQLDelete(sql = "UPDATE cars SET is_deleted = true WHERE id = ?") @Where(clause = "is_deleted=false") @Table(name = "cars") @@ -25,14 +25,17 @@ public class Car { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(nullable = false) private String model; + @Column(nullable = false) private String brand; @PositiveOrZero private Integer inventory; @Enumerated(value = EnumType.STRING) private CarType carType; @PositiveOrZero - private BigDecimal dailyFree; + @Column(nullable = false) + private BigDecimal dailyFee; @Column(nullable = false) private boolean isDeleted; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2532435..20c3fc9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,6 @@ #spring.datasource.username=root #spring.datasource.password=anton7570857 #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver - server.servlet.context-path=/api spring.jpa.hibernate.ddl-auto=validate spring.jpa.show-sql=true diff --git a/src/main/resources/db/changelog/changes/create-cars-table.yaml b/src/main/resources/db/changelog/changes/create-cars-table.yaml index 25c495c..94aea1c 100644 --- a/src/main/resources/db/changelog/changes/create-cars-table.yaml +++ b/src/main/resources/db/changelog/changes/create-cars-table.yaml @@ -17,12 +17,12 @@ databaseChangeLog: name: model type: varchar(255) constraints: - nullable: true + nullable: false - column: name: brand type: varchar(255) constraints: - nullable: true + nullable: false - column: name: inventory type: int @@ -34,10 +34,10 @@ databaseChangeLog: constraints: nullable: true - column: - name: daily_free + name: daily_fee type: decimal(38, 2) constraints: - nullable: true + nullable: false - column: name: is_deleted type: boolean From 247d6068145e7a5101d5564eb0ba4185581449ee Mon Sep 17 00:00:00 2001 From: Dmytro Varukha <132067460+DmytroV95@users.noreply.github.com> Date: Mon, 2 Oct 2023 20:38:47 +0300 Subject: [PATCH 4/4] created mapstruch configuration --- pom.xml | 32 +++++++++++++++++++ .../carsharingapp/config/MapperConfig.java | 13 ++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/com/project/carsharingapp/config/MapperConfig.java diff --git a/pom.xml b/pom.xml index 7e47f59..8d498f6 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,8 @@ 17 checkstyle.xml + 0.2.0 + 1.5.5.Final @@ -62,6 +64,11 @@ h2 test + + org.mapstruct + mapstruct + ${mapstruct.version} + @@ -107,6 +114,31 @@ src + + org.apache.maven.plugins + maven-compiler-plugin + + ${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/java/com/project/carsharingapp/config/MapperConfig.java b/src/main/java/com/project/carsharingapp/config/MapperConfig.java new file mode 100644 index 0000000..65168f0 --- /dev/null +++ b/src/main/java/com/project/carsharingapp/config/MapperConfig.java @@ -0,0 +1,13 @@ +package com.project.carsharingapp.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 { +}