From 3d877d766ffdfe67005c7a6ebc8504cc928ca63b Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Wed, 28 Aug 2024 09:17:40 +0200 Subject: [PATCH] Setup --- .gitignore | 8 ++++ .idea/.gitignore | 2 + .idea/.name | 1 + .run/Postgres.run.xml | 11 +++++ .run/WebshopApplication.run.xml | 10 +++++ build.gradle.kts | 39 ++++++++++++++++ compose.yml | 16 +++++++ settings.gradle.kts | 1 + .../de/szut/store/WebshopApplication.java | 13 ++++++ .../java/de/szut/store/article/Article.java | 37 +++++++++++++++ .../szut/store/article/ArticleRepository.java | 6 +++ .../java/de/szut/store/contact/Contact.java | 31 +++++++++++++ .../java/de/szut/store/supplier/Supplier.java | 29 ++++++++++++ .../store/supplier/SupplierController.java | 4 ++ .../store/supplier/SupplierRepository.java | 6 +++ .../szut/store/supplier/SupplierService.java | 45 +++++++++++++++++++ src/main/resources/application.properties | 9 ++++ .../szut/webshop/WebshopApplicationTests.java | 13 ++++++ 18 files changed, 281 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .run/Postgres.run.xml create mode 100644 .run/WebshopApplication.run.xml create mode 100644 build.gradle.kts create mode 100644 compose.yml create mode 100644 settings.gradle.kts create mode 100644 src/main/java/de/szut/store/WebshopApplication.java create mode 100644 src/main/java/de/szut/store/article/Article.java create mode 100644 src/main/java/de/szut/store/article/ArticleRepository.java create mode 100644 src/main/java/de/szut/store/contact/Contact.java create mode 100644 src/main/java/de/szut/store/supplier/Supplier.java create mode 100644 src/main/java/de/szut/store/supplier/SupplierController.java create mode 100644 src/main/java/de/szut/store/supplier/SupplierRepository.java create mode 100644 src/main/java/de/szut/store/supplier/SupplierService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/test/java/de/szut/webshop/WebshopApplicationTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5706b12 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.gradle/ +gradle/ +gradlew +gradlew.bat +build/ + +.idea/**/* +!.idea/.gitignore \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..a8b7357 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +!.gitignore +!.name \ No newline at end of file diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..6ec6d53 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Webshop \ No newline at end of file diff --git a/.run/Postgres.run.xml b/.run/Postgres.run.xml new file mode 100644 index 0000000..e479381 --- /dev/null +++ b/.run/Postgres.run.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/.run/WebshopApplication.run.xml b/.run/WebshopApplication.run.xml new file mode 100644 index 0000000..95e89f9 --- /dev/null +++ b/.run/WebshopApplication.run.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..81a75ba --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,39 @@ +plugins { + java + id("org.springframework.boot") version "3.3.2" + id("io.spring.dependency-management") version "1.1.6" +} + +group = "de.szut" +version = "0.0.1-SNAPSHOT" + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(22) + } +} + +configurations { + compileOnly { + extendsFrom(configurations.annotationProcessor.get()) + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation("org.springframework.boot:spring-boot-starter-data-jpa") + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-validation") + compileOnly("org.projectlombok:lombok") + runtimeOnly("org.postgresql:postgresql") + annotationProcessor("org.projectlombok:lombok") + testImplementation("org.springframework.boot:spring-boot-starter-test") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..3bb71fb --- /dev/null +++ b/compose.yml @@ -0,0 +1,16 @@ +services: + postgres: + container_name: store_postgres + image: postgres:16 + environment: + - POSTGRES_DB=store + - POSTGRES_USER=store + - POSTGRES_PASSWORD=store123 + ports: + - "5432:5432" + volumes: + - "store_data:/var/lib/postgresql/data" + +volumes: + store_data: + name: store_data \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..32f199f --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "Webshop" diff --git a/src/main/java/de/szut/store/WebshopApplication.java b/src/main/java/de/szut/store/WebshopApplication.java new file mode 100644 index 0000000..d3761fc --- /dev/null +++ b/src/main/java/de/szut/store/WebshopApplication.java @@ -0,0 +1,13 @@ +package de.szut.store; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class WebshopApplication { + + public static void main(String[] args) { + SpringApplication.run(WebshopApplication.class, args); + } + +} diff --git a/src/main/java/de/szut/store/article/Article.java b/src/main/java/de/szut/store/article/Article.java new file mode 100644 index 0000000..277c2b2 --- /dev/null +++ b/src/main/java/de/szut/store/article/Article.java @@ -0,0 +1,37 @@ +package de.szut.store.article; + +import de.szut.store.supplier.Supplier; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.LocalDateTime; + +@Getter +@Setter +@NoArgsConstructor +@Entity +@Table(name="article") +public class Article { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank + private String destination; + + @NotBlank + private Double price; + + @NotNull + private LocalDateTime createDate; + + @NotNull + private LocalDateTime lastChangeDate; + + @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + private Supplier supplier; +} diff --git a/src/main/java/de/szut/store/article/ArticleRepository.java b/src/main/java/de/szut/store/article/ArticleRepository.java new file mode 100644 index 0000000..da467db --- /dev/null +++ b/src/main/java/de/szut/store/article/ArticleRepository.java @@ -0,0 +1,6 @@ +package de.szut.store.article; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ArticleRepository extends JpaRepository { +} diff --git a/src/main/java/de/szut/store/contact/Contact.java b/src/main/java/de/szut/store/contact/Contact.java new file mode 100644 index 0000000..7798673 --- /dev/null +++ b/src/main/java/de/szut/store/contact/Contact.java @@ -0,0 +1,31 @@ +package de.szut.store.contact; + +import de.szut.store.supplier.Supplier; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +@Entity +@Table(name="contact") +public class Contact { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank + private String street; + + @NotBlank + @Size(min = 3, max = 7) + private String zipcode; + + @NotBlank + private String city; + + @OneToOne(mappedBy = "contact", cascade = CascadeType.ALL) + private Supplier supplier; + + private String phone; +} diff --git a/src/main/java/de/szut/store/supplier/Supplier.java b/src/main/java/de/szut/store/supplier/Supplier.java new file mode 100644 index 0000000..d337bc5 --- /dev/null +++ b/src/main/java/de/szut/store/supplier/Supplier.java @@ -0,0 +1,29 @@ +package de.szut.store.supplier; + +import de.szut.store.article.Article; +import de.szut.store.contact.Contact; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; + +import java.util.Set; + +@Data +@Entity +@Table(name="supplier") +public class Supplier { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank + @Size(min = 3, max = 50) + private String name; + + @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + private Contact contact; + + @OneToMany(mappedBy = "supplier", fetch = FetchType.EAGER, cascade = CascadeType.ALL) + private Set
articles; +} diff --git a/src/main/java/de/szut/store/supplier/SupplierController.java b/src/main/java/de/szut/store/supplier/SupplierController.java new file mode 100644 index 0000000..7fc7f0f --- /dev/null +++ b/src/main/java/de/szut/store/supplier/SupplierController.java @@ -0,0 +1,4 @@ +package de.szut.store.supplier; + +public class SupplierController { +} diff --git a/src/main/java/de/szut/store/supplier/SupplierRepository.java b/src/main/java/de/szut/store/supplier/SupplierRepository.java new file mode 100644 index 0000000..9cdd6bc --- /dev/null +++ b/src/main/java/de/szut/store/supplier/SupplierRepository.java @@ -0,0 +1,6 @@ +package de.szut.store.supplier; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface SupplierRepository extends JpaRepository { +} diff --git a/src/main/java/de/szut/store/supplier/SupplierService.java b/src/main/java/de/szut/store/supplier/SupplierService.java new file mode 100644 index 0000000..7e66bb0 --- /dev/null +++ b/src/main/java/de/szut/store/supplier/SupplierService.java @@ -0,0 +1,45 @@ +package de.szut.store.supplier; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class SupplierService { + @Autowired + private final SupplierRepository repository; + + public SupplierService(SupplierRepository repository) { + this.repository = repository; + } + + public Supplier create(Supplier newSupplier) { + return repository.save(newSupplier); + } + + public Supplier update(Supplier newSupplier) { + return repository.findById(newSupplier.getId()) + .map(supplier -> { + supplier.setName(newSupplier.getName()); + supplier.getContact().setCity(newSupplier.getContact().getCity()); + supplier.getContact().setZipcode(newSupplier.getContact().getZipcode()); + supplier.getContact().setStreet(newSupplier.getContact().getStreet()); + supplier.getContact().setPhone(newSupplier.getContact().getPhone()); + supplier.setArticles(newSupplier.getArticles()); + return repository.save(supplier); + }).orElse(null); + } + + public List readAll() { + return repository.findAll(); + } + + public Supplier readById(Long id) { + return repository.findById(id).orElse(null); + } + + public void delete(Long id) { + repository.deleteById(id); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..752dec4 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,9 @@ +# General +spring.application.name=Webshop +server.port=8080 + +# DB +spring.datasource.url=jdbc:postgresql://localhost:5432/store +spring.datasource.username=store +spring.datasource.password=store123 +spring.jpa.hibernate.ddl-auto=create-drop \ No newline at end of file diff --git a/src/test/java/de/szut/webshop/WebshopApplicationTests.java b/src/test/java/de/szut/webshop/WebshopApplicationTests.java new file mode 100644 index 0000000..10aa34a --- /dev/null +++ b/src/test/java/de/szut/webshop/WebshopApplicationTests.java @@ -0,0 +1,13 @@ +package de.szut.webshop; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class WebshopApplicationTests { + + @Test + void contextLoads() { + } + +}