diff --git a/build.gradle b/build.gradle index c182c70..305665b 100644 --- a/build.gradle +++ b/build.gradle @@ -13,8 +13,11 @@ repositories { } dependencies { - // implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'io.springfox:springfox-swagger2:2.9.2' + implementation 'io.springfox:springfox-swagger-ui:2.9.2' + runtimeOnly 'org.postgresql:postgresql' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/com/cubetiqs/demo/config/SpringFoxConfig.java b/src/main/java/com/cubetiqs/demo/config/SpringFoxConfig.java new file mode 100644 index 0000000..dd3ff52 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/config/SpringFoxConfig.java @@ -0,0 +1,22 @@ +package com.cubetiqs.demo.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SpringFoxConfig { + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } +} diff --git a/src/main/java/com/cubetiqs/demo/domain/Product.java b/src/main/java/com/cubetiqs/demo/domain/Product.java index e6f4336..0d24b98 100644 --- a/src/main/java/com/cubetiqs/demo/domain/Product.java +++ b/src/main/java/com/cubetiqs/demo/domain/Product.java @@ -2,12 +2,17 @@ package com.cubetiqs.demo.domain; import lombok.*; +import javax.persistence.*; import java.math.BigDecimal; +@Entity +@Table(name = "products") @Data @NoArgsConstructor @AllArgsConstructor public class Product { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private BigDecimal price; diff --git a/src/main/java/com/cubetiqs/demo/repository/ProductRepository.java b/src/main/java/com/cubetiqs/demo/repository/ProductRepository.java new file mode 100644 index 0000000..0817ecf --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/repository/ProductRepository.java @@ -0,0 +1,8 @@ +package com.cubetiqs.demo.repository; + +import com.cubetiqs.demo.domain.Product; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProductRepository extends JpaRepository { + +} diff --git a/src/main/java/com/cubetiqs/demo/rest/ProductController.java b/src/main/java/com/cubetiqs/demo/rest/ProductController.java index 0bca7ef..b3ffe27 100644 --- a/src/main/java/com/cubetiqs/demo/rest/ProductController.java +++ b/src/main/java/com/cubetiqs/demo/rest/ProductController.java @@ -1,32 +1,35 @@ package com.cubetiqs.demo.rest; import com.cubetiqs.demo.domain.Product; +import com.cubetiqs.demo.repository.ProductRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; -import java.util.ArrayList; import java.util.List; @RestController @RequestMapping(path = "/products") public class ProductController { - private final List items = new ArrayList<>(); + private final ProductRepository productRepository; + + @Autowired + public ProductController(ProductRepository productRepository) { + this.productRepository = productRepository; + } @RequestMapping(method = {RequestMethod.GET}) public List getAllProducts() { - items.add(new Product(1L, "Apple", BigDecimal.valueOf(1.0), false)); - items.add(new Product(2L, "Apple", BigDecimal.valueOf(0.5), true)); - return items; + return productRepository.findAll(); } @GetMapping("/{id}") - public Product getOneProduct(@PathVariable int id) { - return items.get(id); + public Product getOneProduct(@PathVariable Long id) { + return productRepository.findById(id).orElse(null); } @PostMapping public Product createProduct(@RequestBody Product item) { - items.add(item); - return item; + return productRepository.save(item); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index aa90a38..9e3e6e9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,3 +1,12 @@ server: port: 8080 +spring: + jpa: + hibernate: + ddl-auto: update + datasource: + driver-class-name: org.postgresql.Driver + username: cubetiq + password: Root$ + url: jdbc:postgresql://${POSTGRES_HOST:192.168.0.150}:5432/demo?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false \ No newline at end of file