+spring furnature example
This commit is contained in:
@ -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<String, String> 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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.furniture;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface FurnitureRepository extends JpaRepository<FurnitureApplication.Furniture, Long> {}
|
@ -0,0 +1,3 @@
|
||||
spring.jpa.defer-datasource-initialization=true
|
||||
spring.sql.init.mode=always
|
||||
# server.error.path=/error
|
11
Java/spring/furniture/src/main/resources/data.sql
Normal file
11
Java/spring/furniture/src/main/resources/data.sql
Normal file
@ -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';
|
3
Java/spring/furniture/src/main/resources/templates/e
Normal file
3
Java/spring/furniture/src/main/resources/templates/e
Normal file
@ -0,0 +1,3 @@
|
||||
div (style: 'color:red') {
|
||||
h1 { yield 'ERROR' }
|
||||
}
|
23
Java/spring/furniture/src/main/resources/templates/index.tpl
Normal file
23
Java/spring/furniture/src/main/resources/templates/index.tpl
Normal file
@ -0,0 +1,23 @@
|
||||
yieldUnescaped '<!DOCTYPE html>'
|
||||
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' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user