Add domain model and repository and swagger

This commit is contained in:
Sambo Chea 2020-06-09 19:12:33 +07:00
parent 121dd53752
commit 9d72f02c56
6 changed files with 60 additions and 10 deletions

View File

@ -13,8 +13,11 @@ repositories {
} }
dependencies { 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 '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' runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'

View File

@ -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();
}
}

View File

@ -2,12 +2,17 @@ package com.cubetiqs.demo.domain;
import lombok.*; import lombok.*;
import javax.persistence.*;
import java.math.BigDecimal; import java.math.BigDecimal;
@Entity
@Table(name = "products")
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class Product { public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
private String name; private String name;
private BigDecimal price; private BigDecimal price;

View File

@ -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<Product, Long> {
}

View File

@ -1,32 +1,35 @@
package com.cubetiqs.demo.rest; package com.cubetiqs.demo.rest;
import com.cubetiqs.demo.domain.Product; 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 org.springframework.web.bind.annotation.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping(path = "/products") @RequestMapping(path = "/products")
public class ProductController { public class ProductController {
private final List<Product> items = new ArrayList<>(); private final ProductRepository productRepository;
@Autowired
public ProductController(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@RequestMapping(method = {RequestMethod.GET}) @RequestMapping(method = {RequestMethod.GET})
public List<Product> getAllProducts() { public List<Product> getAllProducts() {
items.add(new Product(1L, "Apple", BigDecimal.valueOf(1.0), false)); return productRepository.findAll();
items.add(new Product(2L, "Apple", BigDecimal.valueOf(0.5), true));
return items;
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public Product getOneProduct(@PathVariable int id) { public Product getOneProduct(@PathVariable Long id) {
return items.get(id); return productRepository.findById(id).orElse(null);
} }
@PostMapping @PostMapping
public Product createProduct(@RequestBody Product item) { public Product createProduct(@RequestBody Product item) {
items.add(item); return productRepository.save(item);
return item;
} }
} }

View File

@ -1,3 +1,12 @@
server: server:
port: 8080 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