Skip to content

Commit

Permalink
feat : fileUpload add (MultiPart Form Data)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdj3261 committed Dec 25, 2023
1 parent e1ed3c9 commit b7a41a9
Show file tree
Hide file tree
Showing 27 changed files with 83 additions and 25 deletions.
Binary file added data/0EM4WYA2J5CQN.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM530M9P227X.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM54PYMJXCJB.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM54TXH2XF1X.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM56D51YF36H.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM57WG3AKNE0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM594V1Q3PQ6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM5AW6MB4KVR.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/0EM5HTN2XHN99.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public CreateProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}

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

productRepository.save(product);

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/example/demo/controllers/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,42 @@
import com.example.demo.dtos.CreateProductDto;
import com.example.demo.dtos.ProductListDto;
import com.example.demo.models.Money;
import com.example.demo.utils.ImageStorage;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("products")
@CrossOrigin
public class ProductController {
private final GetProductListService getProductListService;
private final CreateProductService createProductService;
private final ImageStorage imageStorage;


public ProductController(GetProductListService getProductListService,
CreateProductService createProductService) {
CreateProductService createProductService, ImageStorage imageStorage) {
this.getProductListService = getProductListService;
this.createProductService = createProductService;
this.imageStorage = imageStorage;
}

@GetMapping
public ProductListDto list() {
return getProductListService.getProductListDto();
}

@PostMapping
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody CreateProductDto dto) {
public void create(@ModelAttribute CreateProductDto dto)
throws IOException {
String name = dto.name().strip();
String imageUrl = imageStorage.save(dto.image());
Money price = new Money(dto.price());

createProductService.createProduct(name, price);
createProductService.createProduct(name, imageUrl, price);
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/example/demo/dtos/CreateProductDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.demo.dtos;

import org.springframework.web.multipart.MultipartFile;

public record CreateProductDto(
String name,
Long price
Long price,
MultipartFile image
) {
}
1 change: 1 addition & 0 deletions src/main/java/com/example/demo/dtos/ProductListDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public List<ProductDto> getProducts() {
public record ProductDto(
String id,
String name,
String imageUrl,
Long price
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CartDto fetchCartDto(CartId cartId) {
String sql = """
SELECT
*,
products.name AS product_name
products.name AS product_name2
FROM line_items
JOIN products ON line_items.product_id = products.id
WHERE line_items.cart_id = ?
Expand All @@ -31,7 +31,7 @@ public CartDto fetchCartDto(CartId cartId) {
sql,
(ResultSet resultSet, int rowNum) -> new CartDto.LineItemDto(
resultSet.getString("id"),
resultSet.getString("product_name"),
resultSet.getString("product_name2"),
resultSet.getLong("unit_price"),
resultSet.getInt("quantity"),
resultSet.getLong("total_price")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public ProductListDto fetchProductListDto() {
new ProductListDto.ProductDto(
resultSet.getString("id"),
resultSet.getString("name"),
resultSet.getString("image_url"),
resultSet.getLong("price")
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/demo/models/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Cart {
@UpdateTimestamp
private LocalDateTime updatedAt;

private Cart() {
protected Cart() {
}

public Cart(CartId cartId) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/demo/models/CartId.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class CartId extends EntityId {
// TODO: Delete this! (카트가 하나만 존재한다고 가정)
public static final CartId DEFAULT = new CartId("0BV000000CART");

private CartId() {
protected CartId() {
super();
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/example/demo/models/EntityId.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;

import java.io.Serializable;
import java.util.Objects;

@MappedSuperclass
public abstract class EntityId {
public abstract class EntityId implements Serializable {
@Column(name = "id")
private String value;

protected EntityId() {
public EntityId() {
}

protected EntityId(String value) {
public EntityId(String value) {
this.value = value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/demo/models/LineItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LineItem {
@UpdateTimestamp
private LocalDateTime updatedAt;

private LineItem() {
protected LineItem() {
}

public LineItem(LineItemId id, Product product, int quantity) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/example/demo/models/LineItemId.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.demo.models;

import jakarta.persistence.Embeddable;

@Embeddable
public class LineItemId extends EntityId {
private LineItemId() {
public LineItemId() {
super();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/demo/models/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Money {
@Column(name = "money")
private Long value;

private Money() {
public Money() {

}

Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/example/demo/models/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class Product {
@Column(name = "name")
private String name;

@Column(name = "image_url")
private String imageUrl;

@CreationTimestamp
private LocalDateTime createdAt;

Expand All @@ -25,17 +28,18 @@ public class Product {
@AttributeOverride(name = "value", column = @Column(name = "price"))
private Money price;

private Product() {
public Product() {
}

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

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

public ProductId id() {
Expand All @@ -46,6 +50,9 @@ public String name() {
return name;
}

public String imageUrl() {
return imageUrl;
}
public Money price() {
return price;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/example/demo/models/ProductId.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.demo.models;

import jakarta.persistence.Embeddable;

@Embeddable
public class ProductId extends EntityId {
private ProductId() {
public ProductId() {
super();
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/example/demo/utils/ImageStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.demo.utils;

import io.hypersistence.tsid.TSID;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@Component
public class ImageStorage {
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());
return filename;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void createProduct() {
String name = "제-품";
Money price = new Money(100_000L);

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

assertThat(product.name()).isEqualTo(name);
assertThat(product.price()).isEqualTo(price);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ void create() throws Exception {
.andExpect(status().isCreated());

verify(createProductService)
.createProduct("멋진 제품", new Money(100_000L));
.createProduct("멋진 제품", imageUrl, new Money(100_000L));
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/example/demo/models/ProductTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ProductTest {
@Test
void creation() {
Product product = Product.create("제품명", new Money(123_456L));
Product product = Product.create("제품명", imageUrl, new Money(123_456L));

assertThat(product.id()).isNotNull();
assertThat(product.name()).isEqualTo("제품명");
Expand Down

0 comments on commit b7a41a9

Please sign in to comment.