Catalog 모듈 코드 작성

This commit is contained in:
roy-zz
2022-04-21 00:18:09 +09:00
parent 11be0b010d
commit 3706cb48a4
12 changed files with 191 additions and 21 deletions

View File

@@ -3,13 +3,21 @@ ext {
}
dependencies {
implementation(project(":util"))
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2:1.3.176'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@@ -0,0 +1,40 @@
package com.roy.springcloud.catalogservice.controller;
import com.roy.springcloud.catalogservice.dto.CatalogDto;
import com.roy.springcloud.catalogservice.service.CatalogService;
import com.roy.springcloud.catalogservice.vo.response.CatalogResponse;
import com.roy.springcloud.util.mapper.MapperUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequiredArgsConstructor
@RequestMapping("/catalog-service")
public class CatalogController {
private final Environment environment;
private final CatalogService catalogService;
@GetMapping("/health-check")
public String healthCheck(HttpServletRequest request) {
return String.format("It's working in catalog service on Port: %s", request.getServerPort());
}
@GetMapping("/catalogs")
public ResponseEntity<List<CatalogResponse>> getCatalogs() {
List<CatalogDto> savedCatalogs = catalogService.getAllCatalogs();
List<CatalogResponse> response = savedCatalogs.stream()
.map(catalog -> MapperUtil.toObject(catalog, CatalogResponse.class))
.collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.OK).body(response);
}
}

View File

@@ -0,0 +1,30 @@
package com.roy.springcloud.catalogservice.domain;
import lombok.Data;
import org.hibernate.annotations.ColumnDefault;
import javax.persistence.*;
import java.time.LocalDateTime;
import static javax.persistence.GenerationType.IDENTITY;
@Data
@Entity
@Table(name = "catalog")
public class Catalog {
@Id @GeneratedValue(strategy = IDENTITY)
private Long id;
@Column(nullable = false, length = 120, unique = true)
private String productId;
@Column(nullable = false)
private String productName;
@Column(nullable = false)
private Integer stock;
@Column(nullable = false)
private Integer unitPrice;
@Column(nullable = false, updatable = false, insertable = false)
@ColumnDefault(value = "CURRENT_TIMESTAMP")
private LocalDateTime createdAt;
}

View File

@@ -0,0 +1,20 @@
package com.roy.springcloud.catalogservice.dto;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class CatalogDto implements Serializable {
private String productId;
private String productName;
private Integer stock;
private Integer quantity;
private Integer unitPrice;
private Integer totalPrice;
private LocalDateTime createdAt;
private String orderId;
private String userId;
}

View File

@@ -0,0 +1,10 @@
package com.roy.springcloud.catalogservice.repository;
import com.roy.springcloud.catalogservice.domain.Catalog;
import org.springframework.data.repository.CrudRepository;
import java.util.Optional;
public interface CatalogRepository extends CrudRepository<Catalog, Long> {
Optional<Catalog> findByProductId(String productId);
}

View File

@@ -0,0 +1,9 @@
package com.roy.springcloud.catalogservice.service;
import com.roy.springcloud.catalogservice.dto.CatalogDto;
import java.util.List;
public interface CatalogService {
List<CatalogDto> getAllCatalogs();
}

View File

@@ -0,0 +1,33 @@
package com.roy.springcloud.catalogservice.service.impl;
import com.roy.springcloud.catalogservice.domain.Catalog;
import com.roy.springcloud.catalogservice.dto.CatalogDto;
import com.roy.springcloud.catalogservice.repository.CatalogRepository;
import com.roy.springcloud.catalogservice.service.CatalogService;
import com.roy.springcloud.util.mapper.MapperUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class CatalogServiceImpl implements CatalogService {
private final Environment environment;
private final CatalogRepository catalogRepository;
@Override
public List<CatalogDto> getAllCatalogs() {
Iterable<Catalog> savedCatalogs = catalogRepository.findAll();
List<CatalogDto> response = new ArrayList<>();
savedCatalogs.forEach(catalog -> {
response.add(MapperUtil.toObject(catalog, CatalogDto.class));
});
return response;
}
}

View File

@@ -0,0 +1,16 @@
package com.roy.springcloud.catalogservice.vo.response;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CatalogResponse {
private String productId;
private String productName;
private Integer stock;
private Integer unitPrice;
private LocalDateTime createdAt;
}

View File

@@ -10,6 +10,11 @@ spring:
settings:
web-allow-others: true
path: /h2-console
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
generate-ddl: true
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
@@ -24,4 +29,8 @@ eureka:
defaultZone: http://localhost:8761/eureka
greeting:
message: Welcome to the Simple E-Commerce(Catalog Service).
message: Welcome to the Simple E-Commerce(Catalog Service).
logging:
level:
com.roy.springcloud.catalogservice: DEBUG

View File

@@ -0,0 +1,3 @@
INSERT INTO catalog(product_id, product_name, stock, unit_price) VALUES ('CATALOG-0001', 'Berlin', 100, 1500);
INSERT INTO catalog(product_id, product_name, stock, unit_price) VALUES ('CATALOG-0002', 'Tokyo', 100, 900);
INSERT INTO catalog(product_id, product_name, stock, unit_price) VALUES ('CATALOG-0003', 'Stockholm', 100, 1200);