Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WMS-1 | 상품 등록 기능 개발 #1

Merged
merged 15 commits into from
Jul 30, 2023
Prev Previous commit
Next Next commit
5.move inner classes to upper level.
  • Loading branch information
이중석/백엔드개발팀 committed Jul 30, 2023
commit f688c49ff0462b282c11d079673c497a33822ef3
10 changes: 10 additions & 0 deletions src/test/java/com/ejoongseok/wmslive/product/feature/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ejoongseok.wmslive.product.feature;

public enum Category {
ELECTRONICS("전자 제품");
private final String description;

Category(final String description) {
this.description = description;
}
}
81 changes: 81 additions & 0 deletions src/test/java/com/ejoongseok/wmslive/product/feature/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.ejoongseok.wmslive.product.feature;

import org.springframework.util.Assert;

public class Product {

private final String name;
private final String code;
private final String description;
private final String brand;
private final String maker;
private final String origin;
private final Category category;
private final TemperatureZone temperatureZone;
private final Long weightInGrams;
private final ProductSize productSize;
@lombok.Getter
private Long id;

public Product(
final String name,
final String code,
final String description,
final String brand,
final String maker,
final String origin,
final Category category,
final TemperatureZone temperatureZone,
final Long weightInGrams,
final ProductSize productSize) {
validateConstructor(
name,
code,
description,
brand,
maker,
origin,
category,
temperatureZone,
weightInGrams,
productSize);
this.name = name;
this.code = code;
this.description = description;
this.brand = brand;
this.maker = maker;
this.origin = origin;
this.category = category;
this.temperatureZone = temperatureZone;
this.weightInGrams = weightInGrams;
this.productSize = productSize;
}

private void validateConstructor(
final String name,
final String code,
final String description,
final String brand,
final String maker,
final String origin,
final Category category,
final TemperatureZone temperatureZone,
final Long weightInGrams,
final ProductSize productSize) {
Assert.hasText(name, "상품명은 필수입니다.");
Assert.hasText(code, "상품코드는 필수입니다.");
Assert.hasText(description, "상품설명은 필수입니다.");
Assert.hasText(brand, "브랜드는 필수입니다.");
Assert.hasText(maker, "제조사는 필수입니다.");
Assert.hasText(origin, "원산지는 필수입니다.");
Assert.notNull(category, "카테고리는 필수입니다.");
Assert.notNull(temperatureZone, "온도대는 필수입니다.");
Assert.notNull(weightInGrams, "무게는 필수입니다.");
Assert.notNull(productSize, "상품크기는 필수입니다.");
}

public void assignId(final Long id) {
this.id = id;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ejoongseok.wmslive.product.feature;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductRepository {
private final Map<Long, Product> products = new HashMap<>();
private Long nextId = 1L;

public void save(final Product product) {
product.assignId(nextId);
nextId++;
products.put(product.getId(), product);
}

public List<Product> findAll() {
return new ArrayList<>(products.values());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.ejoongseok.wmslive.product.feature;

import org.springframework.util.Assert;

public class ProductSize {
private final Long widthInMillimeters;
private final Long heightInMillimeters;
private final Long lengthInMillimeters;

public ProductSize(
final Long widthInMillimeters,
final Long heightInMillimeters,
final Long lengthInMillimeters) {
validateProductSize(
widthInMillimeters,
heightInMillimeters,
lengthInMillimeters);
this.widthInMillimeters = widthInMillimeters;
this.heightInMillimeters = heightInMillimeters;
this.lengthInMillimeters = lengthInMillimeters;
}

private void validateProductSize(
final Long widthInMillimeters,
final Long heightInMillimeters,
final Long lengthInMillimeters) {
Assert.notNull(widthInMillimeters, "가로길이는 필수입니다.");
if (0 > widthInMillimeters) {
throw new IllegalArgumentException("가로길이는 0보다 작을 수 없습니다.");
}
Assert.notNull(heightInMillimeters, "세로길이는 필수입니다.");
if (0 > heightInMillimeters) {
throw new IllegalArgumentException("세로길이는 0보다 작을 수 없습니다.");
}
Assert.notNull(lengthInMillimeters, "세로길이는 필수입니다.");
if (0 > lengthInMillimeters) {
throw new IllegalArgumentException("세로길이는 0보다 작을 수 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.ejoongseok.wmslive.product.feature;

import org.springframework.util.Assert;

public class RegisterProduct {
private final ProductRepository productRepository;

public RegisterProduct(final ProductRepository productRepository) {
this.productRepository = productRepository;
}

public void request(final Request request) {
final Product product = request.toDomain();
productRepository.save(product);
}

public record Request(
String name,
String code,
String description,
String brand,
String maker,
String origin,
Category category,
TemperatureZone temperatureZone,
Long weightInGrams,
Long widthInMillimeters,
Long heightInMillimeters,
Long lengthInMillimeters) {

public Request {
Assert.hasText(name, "상품명은 필수입니다.");
Assert.hasText(code, "상품코드는 필수입니다.");
Assert.hasText(description, "상품설명은 필수입니다.");
Assert.hasText(brand, "브랜드는 필수입니다.");
Assert.hasText(maker, "제조사는 필수입니다.");
Assert.hasText(origin, "원산지는 필수입니다.");
Assert.notNull(category, "카테고리는 필수입니다.");
Assert.notNull(temperatureZone, "온도대는 필수입니다.");
Assert.notNull(weightInGrams, "무게는 필수입니다.");
if (0 > weightInGrams) {
throw new IllegalArgumentException("무게는 0보다 작을 수 없습니다.");
}
Assert.notNull(widthInMillimeters, "가로길이는 필수입니다.");
if (0 > widthInMillimeters) {
throw new IllegalArgumentException("가로길이는 0보다 작을 수 없습니다.");
}
Assert.notNull(heightInMillimeters, "세로길이는 필수입니다.");
if (0 > heightInMillimeters) {
throw new IllegalArgumentException("세로길이는 0보다 작을 수 없습니다.");
}
Assert.notNull(lengthInMillimeters, "세로길이는 필수입니다.");
if (0 > lengthInMillimeters) {
throw new IllegalArgumentException("세로길이는 0보다 작을 수 없습니다.");
}
}

public Product toDomain() {
return new Product(
name,
code,
description,
brand,
maker,
origin,
category,
temperatureZone,
weightInGrams,
new ProductSize(
widthInMillimeters,
heightInMillimeters,
lengthInMillimeters)
);
}

}
}
Loading