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

[10기 이병재] 11주차 과제 - 장바구니 파일 업로드 기능 추가하기 #12

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public CreateProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}

public Product createProduct(String name, String imageUrl, Money price) {
Product product = Product.create(name, imageUrl, price);

public Product createProduct(String name, Money price, String imageUrl) {
Product product = Product.create(name, price, imageUrl);
productRepository.save(product);

return product;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class ProductController {
private final CreateProductService createProductService;
private final ImageStorage imageStorage;


public ProductController(GetProductListService getProductListService,
CreateProductService createProductService, ImageStorage imageStorage) {
CreateProductService createProductService,
ImageStorage imageStorage) {
this.getProductListService = getProductListService;
this.createProductService = createProductService;
this.imageStorage = imageStorage;
Expand All @@ -40,7 +40,7 @@ public void create(@ModelAttribute CreateProductDto dto)
String name = dto.name().strip();
String imageUrl = imageStorage.save(dto.image());
Money price = new Money(dto.price());

createProductService.createProduct(name, imageUrl, price);
String imageUrl = imageStorage.save(dto.image());
createProductService.createProduct(name, price, imageUrl);
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/example/demo/dtos/CartDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public List<LineItemDto> getLineItems() {
}

public record LineItemDto(
String id,
String productName,
long unitPrice,
int quantity,
long totalPrice
String id,
String productName,
long unitPrice,
int quantity,
long totalPrice
) {
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/example/demo/dtos/CreateProductDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.springframework.web.multipart.MultipartFile;

public record CreateProductDto(
String name,
Long price,
MultipartFile image
String name,
Long price,
MultipartFile image
) {
}
9 changes: 5 additions & 4 deletions src/main/java/com/example/demo/dtos/ProductListDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public List<ProductDto> getProducts() {
return products;
}

// 오류 수정 커밋용 주석
public record ProductDto(
String id,
String name,
String imageUrl,
Long price
String id,
String name,
Long price,
String image
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public ProductDtoFetcher(JdbcTemplate jdbcTemplate) {

public ProductListDto fetchProductListDto() {
String sql = """
SELECT *
FROM products
ORDER BY products.id DESC
""";
SELECT *
FROM products
ORDER BY products.id DESC
""";

List<ProductListDto.ProductDto> productDtos = jdbcTemplate.query(
sql,
(ResultSet resultSet, int rowNum) ->
new ProductListDto.ProductDto(
resultSet.getString("id"),
resultSet.getString("name"),
resultSet.getString("image_url"),
resultSet.getLong("price")
)
sql,
(ResultSet resultSet, int rowNum) ->
new ProductListDto.ProductDto(
resultSet.getString("id"),
resultSet.getString("name"),
resultSet.getLong("price"),
resultSet.getString("image_url")
)
);

return new ProductListDto(productDtos);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/example/demo/models/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ public class Product {
public Product() {
}

public Product(ProductId id, String name, String imageUrl, Money price) {
public Product(ProductId id, String name, Money price, String imageUrl) {
this.id = id;
this.name = name;
this.imageUrl = imageUrl;
this.price = price;
this.imageUrl = imageUrl;
}

public static Product create(String name, String imageUrl, Money price) {
return new Product(ProductId.generate(), name, imageUrl, price);
public static Product create(String name, Money price, String imageUrl) {
return new Product(ProductId.generate(), name, price, imageUrl);
}

public ProductId id() {
Expand All @@ -56,4 +57,8 @@ public String imageUrl() {
public Money price() {
return price;
}

public String imageUrl() {
return imageUrl;
}
}
12 changes: 5 additions & 7 deletions src/main/java/com/example/demo/utils/ImageStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@

@Component
public class ImageStorage {
public String save(MultipartFile multipartFile) {
if (multipartFile == null || multipartFile.isEmpty()) {
return "No image";
}
public String save(MultipartFile multipartFile){
if(multipartFile == null || multipartFile.isEmpty()) return "No Image";

String id = TSID.Factory.getTsid().toString();
String filename = "data/" + id + ".jpg";

File file = new File(filename);

try (FileOutputStream output = new FileOutputStream(file)) {
output.write(multipartFile.getBytes());
try (FileOutputStream outputStream = new FileOutputStream(file)){
outputStream.write(multipartFile.getBytes());
return filename;
} catch (IOException e) {
} catch (IOException e){
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ void createProduct() {
String name = "제-품";
String imageUrl = "IMAGE_URL";
Money price = new Money(100_000L);
String imageUrl = "IMAGE_URL";

Product product = createProductService
.createProduct(name, imageUrl, price);
Product product = createProductService.createProduct(name, price, imageUrl);

assertThat(product.name()).isEqualTo(name);
assertThat(product.imageUrl()).isEqualTo(imageUrl);
assertThat(product.price()).isEqualTo(price);
assertThat(product.imageUrl()).isEqualTo(imageUrl);

verify(productRepository).save(product);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class ProductControllerTest {
@DisplayName("GET /products")
void list() throws Exception {
ProductListDto.ProductDto productDto =
new ProductListDto
.ProductDto("test-id", "제품", "IMAGE_URL", 100_000L);
new ProductListDto.ProductDto("test-id", "제품", 100_000L, "IMAGE_URL");

given(getProductListService.getProductListDto()).willReturn(
new ProductListDto(List.of(productDto)));
Expand All @@ -58,7 +57,6 @@ void list() throws Exception {
@DisplayName("POST /products")
void create() throws Exception {
String filename = "src/test/resources/files/test.jpg";

MockMultipartFile file = new MockMultipartFile(
"image", "test.jpg", "image/jpeg",
new FileInputStream(filename));
Expand All @@ -72,6 +70,6 @@ void create() throws Exception {
.andExpect(status().isCreated());

verify(createProductService)
.createProduct("신제품", "data/image.jpg", new Money(100_000L));
.createProduct("신제품", new Money(100_000L), "data/image.jpg");
}
}
Loading