From 6cb4847bc8d1dad8c40da0660adc188d5b1ae581 Mon Sep 17 00:00:00 2001 From: anon Date: Tue, 10 Dec 2024 20:31:49 +0100 Subject: [PATCH] +spring furnature example --- Java/spring/furniture/build.gradle | 29 +++++++ Java/spring/furniture/dump.sql | 17 ++++ Java/spring/furniture/settings.gradle | 1 + .../furniture/FurnitureApplication.java | 84 +++++++++++++++++++ .../furniture/FurnitureRepository.java | 5 ++ .../src/main/resources/application.properties | 3 + .../furniture/src/main/resources/data.sql | 11 +++ .../furniture/src/main/resources/templates/e | 3 + .../src/main/resources/templates/index.tpl | 23 +++++ .../furniture/FurnitureApplicationTests.java | 13 +++ 10 files changed, 189 insertions(+) create mode 100644 Java/spring/furniture/build.gradle create mode 100644 Java/spring/furniture/dump.sql create mode 100644 Java/spring/furniture/settings.gradle create mode 100644 Java/spring/furniture/src/main/java/com/example/furniture/FurnitureApplication.java create mode 100644 Java/spring/furniture/src/main/java/com/example/furniture/FurnitureRepository.java create mode 100644 Java/spring/furniture/src/main/resources/application.properties create mode 100644 Java/spring/furniture/src/main/resources/data.sql create mode 100644 Java/spring/furniture/src/main/resources/templates/e create mode 100644 Java/spring/furniture/src/main/resources/templates/index.tpl create mode 100644 Java/spring/furniture/src/test/java/com/example/furniture/FurnitureApplicationTests.java diff --git a/Java/spring/furniture/build.gradle b/Java/spring/furniture/build.gradle new file mode 100644 index 0000000..ad3552d --- /dev/null +++ b/Java/spring/furniture/build.gradle @@ -0,0 +1,29 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.1.5' + id 'io.spring.dependency-management' version '1.1.3' +} + +group = 'com.example' +version = '0.0.1-SNAPSHOT' + +java { + sourceCompatibility = '17' +} + +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-groovy-templates' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0' + runtimeOnly 'com.h2database:h2' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/Java/spring/furniture/dump.sql b/Java/spring/furniture/dump.sql new file mode 100644 index 0000000..8223999 --- /dev/null +++ b/Java/spring/furniture/dump.sql @@ -0,0 +1,17 @@ +-- H2 2.1.214; +SET DB_CLOSE_DELAY -1; +; +CREATE USER IF NOT EXISTS "SA" SALT 'bb38af8fed566570' HASH '21b7a0b7822e49c228946e75eda19d36c360fca82e91726ea7d82a657a249d38' ADMIN; +CREATE MEMORY TABLE "PUBLIC"."furniture_application$furniture"( + "PRICE" INTEGER NOT NULL, + "ID" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1 RESTART WITH 2) NOT NULL, + "NAME" CHARACTER VARYING(255) +); +ALTER TABLE "PUBLIC"."furniture_application$furniture" ADD CONSTRAINT "PUBLIC"."CONSTRAINT_C" PRIMARY KEY("ID"); +-- 1 +/- SELECT COUNT(*) FROM PUBLIC."furniture_application$furniture"; +INSERT INTO "PUBLIC"."furniture_application$furniture" VALUES +(500, 1, 'Sofa'); +CREATE MEMORY TABLE "PUBLIC"."NIGGER"( + "I" INTEGER +); +-- 0 +/- SELECT COUNT(*) FROM PUBLIC.NIGGER; diff --git a/Java/spring/furniture/settings.gradle b/Java/spring/furniture/settings.gradle new file mode 100644 index 0000000..3b92b0a --- /dev/null +++ b/Java/spring/furniture/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'furniture' diff --git a/Java/spring/furniture/src/main/java/com/example/furniture/FurnitureApplication.java b/Java/spring/furniture/src/main/java/com/example/furniture/FurnitureApplication.java new file mode 100644 index 0000000..d218ebe --- /dev/null +++ b/Java/spring/furniture/src/main/java/com/example/furniture/FurnitureApplication.java @@ -0,0 +1,84 @@ +package com.example.furniture; + +import java.util.List; +import jakarta.persistence.*; +import org.springframework.boot.*; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.*; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; +import org.slf4j.LoggerFactory; +import org.slf4j.Logger; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.util.MultiValueMap; + +@SpringBootApplication +public class FurnitureApplication { + @Entity + public static + class Furniture { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public + Long id; + + public + String name; + + private + int price; + + Furniture(String name, int price) { + this.name = name; + this.price = price; + } + Furniture() { ; } + } + + @RestController + static + class Controller { + private final + FurnitureRepository repository; + + static private + Logger logger = LoggerFactory.getLogger("connectionLogger"); + + Controller(FurnitureRepository repository) { + this.repository = repository; + } + + @GetMapping("/") + ModelAndView index(){ + logger.info("connection"); + ModelAndView mav = new ModelAndView(); + mav.addObject("title", "Furniture list:"); + mav.addObject("furnitures", this.repository); + mav.setViewName("inde"); + return mav; + } + + @PostMapping("/") + ModelAndView indexPost(@RequestBody MultiValueMap data) { + ObjectMapper objectMapper = new ObjectMapper(); + try { + logger.info("post: " + objectMapper.writeValueAsString(data)); + repository.save(new Furniture(data.getFirst("name"), 0)); + } catch(Exception e) { ; } + return index(); + } + + //@GetMapping("/error") + //ModelAndView error(){ + // ModelAndView mav = new ModelAndView(); + // mav.setViewName("error"); + // return mav; + //} + } + + public static void main(String[] args) { + SpringApplication.run(FurnitureApplication.class, args); + } + +} diff --git a/Java/spring/furniture/src/main/java/com/example/furniture/FurnitureRepository.java b/Java/spring/furniture/src/main/java/com/example/furniture/FurnitureRepository.java new file mode 100644 index 0000000..d912002 --- /dev/null +++ b/Java/spring/furniture/src/main/java/com/example/furniture/FurnitureRepository.java @@ -0,0 +1,5 @@ +package com.example.furniture; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FurnitureRepository extends JpaRepository {} diff --git a/Java/spring/furniture/src/main/resources/application.properties b/Java/spring/furniture/src/main/resources/application.properties new file mode 100644 index 0000000..28e4f51 --- /dev/null +++ b/Java/spring/furniture/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.jpa.defer-datasource-initialization=true +spring.sql.init.mode=always +# server.error.path=/error diff --git a/Java/spring/furniture/src/main/resources/data.sql b/Java/spring/furniture/src/main/resources/data.sql new file mode 100644 index 0000000..7a69194 --- /dev/null +++ b/Java/spring/furniture/src/main/resources/data.sql @@ -0,0 +1,11 @@ +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Sofa', 500); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Dining Table', 350); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Bed', 700); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Wardrobe', 300); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Coffee Table', 150); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Bookshelf', 200); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Office Chair', 100); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Recliner', 450); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Dresser', 280); +INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('End Table', 80); +-- SCRIPT TO 'dump.sql'; diff --git a/Java/spring/furniture/src/main/resources/templates/e b/Java/spring/furniture/src/main/resources/templates/e new file mode 100644 index 0000000..b6b1a48 --- /dev/null +++ b/Java/spring/furniture/src/main/resources/templates/e @@ -0,0 +1,3 @@ +div (style: 'color:red') { + h1 { yield 'ERROR' } +} diff --git a/Java/spring/furniture/src/main/resources/templates/index.tpl b/Java/spring/furniture/src/main/resources/templates/index.tpl new file mode 100644 index 0000000..1640e73 --- /dev/null +++ b/Java/spring/furniture/src/main/resources/templates/index.tpl @@ -0,0 +1,23 @@ +yieldUnescaped '' +html(lang:'en') { + body { + div { + h1 { yield(title) } + } + ul { + furnitures.findAll().each { furniture -> + li { + yield "${furniture.name}" + } + } + } + + div { + h4 { yield 'Add new' } + form (method: 'post', enctype: 'application/json') { + input (name: "name", placeholder: "name") { } br {} + button (type: 'submit') { yield 'Submit' } + } + } + } +} diff --git a/Java/spring/furniture/src/test/java/com/example/furniture/FurnitureApplicationTests.java b/Java/spring/furniture/src/test/java/com/example/furniture/FurnitureApplicationTests.java new file mode 100644 index 0000000..809b1b5 --- /dev/null +++ b/Java/spring/furniture/src/test/java/com/example/furniture/FurnitureApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.furniture; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class FurnitureApplicationTests { + + @Test + void contextLoads() { + } + +}