-
Notifications
You must be signed in to change notification settings - Fork 0
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
Open
11dlguswns
wants to merge
11
commits into
main
Choose a base branch
from
7-create-product
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
상품 판매 등록 기능 구현 #10
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
829b745
파일 위치 수정
11dlguswns 1e45d6e
refactor :: Product Entity 기본 값 추가, 자료형 수정
11dlguswns e4f03f6
feat :: 이미지 저장 기능 개발
11dlguswns 2d3dfa6
feat :: 태그 저장 기능 개발
11dlguswns a43d392
feat :: 상품 저장 기능 추가
11dlguswns 56b04f9
feat :: Validation 예외 처리 추가
11dlguswns 4a6021f
refactor :: ProductRepository 상속 변경
11dlguswns ea7fc42
refactor :: createProduct 상태코드 수정
11dlguswns de8ada2
Merge branch 'main' into 7-create-product
11dlguswns 60f49b0
refactor :: product Entity를 record로 수정
11dlguswns ba6a514
Merge branch '7-create-product' of https://github.com/Team-A-Mango/A-…
11dlguswns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/com/mango/amango/domain/image/ImageUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.mango.amango.domain.image; | ||
|
||
import com.mango.amango.global.exception.CustomErrorCode; | ||
import com.mango.amango.global.exception.CustomException; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
import java.util.UUID; | ||
|
||
@Component | ||
public class ImageUtil { | ||
|
||
public static String storeImage(MultipartFile file) { | ||
if (file.isEmpty()) { | ||
return null; | ||
} | ||
|
||
makeDir(); | ||
|
||
String originalFilename = file.getOriginalFilename(); | ||
String storeFilename = createStoreFilename(originalFilename); | ||
try { | ||
file.transferTo(new File(getFullPath(storeFilename))); | ||
} catch (IOException e) { | ||
throw new CustomException(CustomErrorCode.FILE_PROCESSING_ERROR); | ||
} | ||
|
||
return storeFilename; | ||
} | ||
|
||
public static String getFullPath(String fileName) { | ||
if (fileName.isEmpty()) { | ||
return null; | ||
} | ||
|
||
return Paths.get(getFilePath(), fileName).toString(); | ||
} | ||
|
||
private static String getFilePath() { | ||
return Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "static", "images").toString(); | ||
} | ||
|
||
private static void makeDir() { | ||
File file = new File(getFilePath()); | ||
if(!file.exists() && !file.mkdirs()) { | ||
throw new CustomException(CustomErrorCode.FILE_CREATE_ERROR); | ||
} | ||
} | ||
|
||
private static String createStoreFilename(String filename) { | ||
String ext = extractExt(filename); | ||
String uuid = UUID.randomUUID().toString(); | ||
return uuid + "." + ext; | ||
} | ||
|
||
private static String extractExt(String filename) { | ||
int pos = filename.lastIndexOf("."); | ||
return filename.substring(pos + 1); | ||
} | ||
|
||
|
||
} |
4 changes: 2 additions & 2 deletions
4
.../com/mango/amango/domain/image/Image.java → ...ngo/amango/domain/image/entity/Image.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/mango/amango/domain/image/repository/ImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mango.amango.domain.image.repository; | ||
|
||
import com.mango.amango.domain.image.entity.Image; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface ImageRepository extends CrudRepository<Image, Long> { | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/mango/amango/domain/image/service/ImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.mango.amango.domain.image.service; | ||
|
||
import com.mango.amango.domain.image.entity.Image; | ||
import com.mango.amango.domain.image.repository.ImageRepository; | ||
import com.mango.amango.domain.product.entity.Product; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
import static com.mango.amango.domain.image.ImageUtil.*; | ||
|
||
@Service | ||
@Transactional | ||
@AllArgsConstructor | ||
public class ImageService { | ||
|
||
private final ImageRepository imageRepository; | ||
|
||
public void saveImage(Product product, List<MultipartFile> files) { | ||
for (MultipartFile file : files) { | ||
imageRepository.save(Image.builder() | ||
.image(storeImage(file)) | ||
.product(product) | ||
.build() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/mango/amango/domain/product/controller/ProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.mango.amango.domain.product.controller; | ||
|
||
import com.mango.amango.domain.product.entity.dto.request.CreateProductReq; | ||
import com.mango.amango.domain.product.service.ProductService; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/product") | ||
public class ProductController { | ||
|
||
public final ProductService productService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> createProduct( | ||
@Valid @RequestPart("request") CreateProductReq request, | ||
@Size(max = 3, message = "이미지는 최대 3장까지 입니다.") @RequestPart("images") List<MultipartFile> images | ||
) { | ||
productService.createProduct(request, images); | ||
return ResponseEntity.status(CREATED).build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mango.amango.domain.product.entity.dto.request; | ||
|
||
import com.mango.amango.domain.tag.entity.Category; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public record CreateProductReq( | ||
@NotBlank(message = "제목은 비어 있을 수 없습니다.") | ||
String title, | ||
|
||
String description, | ||
|
||
Long price, | ||
|
||
@Size(min = 1, message = "태그는 최소 1개 이상 포함되어야 합니다.") | ||
List<Category> tags, | ||
|
||
Long auctionPrice, | ||
|
||
@NotNull(message = "시간은 필수 입력 값 입니다.") | ||
LocalDateTime expirTime | ||
) { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.mango.amango.domain.product.repository; | ||
|
||
import com.mango.amango.domain.product.entity.Product; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface ProductRepository extends JpaRepository<Product, Long> { | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/mango/amango/domain/product/service/ProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.mango.amango.domain.product.service; | ||
|
||
import com.mango.amango.domain.image.service.ImageService; | ||
import com.mango.amango.domain.product.entity.Product; | ||
import com.mango.amango.domain.product.entity.dto.request.CreateProductReq; | ||
import com.mango.amango.domain.product.repository.ProductRepository; | ||
import com.mango.amango.domain.tag.service.TagService; | ||
import com.mango.amango.domain.user.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ProductService { | ||
|
||
private final ProductRepository productRepository; | ||
private final UserService userService; | ||
private final TagService tagService; | ||
private final ImageService imageService; | ||
|
||
|
||
public void createProduct(CreateProductReq request, List<MultipartFile> images) { | ||
|
||
Product product = Product.builder() | ||
.title(request.title()) | ||
.description(request.description()) | ||
.price(request.price()) | ||
.user(userService.getCurrentUser()) | ||
.expirTime(request.expirTime()) | ||
.auctionPrice(request.auctionPrice()) | ||
.build(); | ||
|
||
productRepository.save(product); | ||
tagService.saveTag(product, request.tags()); | ||
imageService.saveImage(product, images); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...com/mango/amango/domain/tag/Category.java → ...go/amango/domain/tag/entity/Category.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...java/com/mango/amango/domain/tag/Tag.java → ...m/mango/amango/domain/tag/entity/Tag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/mango/amango/domain/tag/repository/TagRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mango.amango.domain.tag.repository; | ||
|
||
import com.mango.amango.domain.tag.entity.Tag; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface TagRepository extends CrudRepository<Tag, Long> { | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/mango/amango/domain/tag/service/TagService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.mango.amango.domain.tag.service; | ||
|
||
import com.mango.amango.domain.product.entity.Product; | ||
import com.mango.amango.domain.tag.entity.Category; | ||
import com.mango.amango.domain.tag.entity.Tag; | ||
import com.mango.amango.domain.tag.repository.TagRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class TagService { | ||
|
||
private final TagRepository tagRepository; | ||
|
||
public void saveTag(Product product, List<Category> categories) { | ||
categories.forEach(category -> { | ||
tagRepository.save(Tag.builder() | ||
.category(category) | ||
.product(product) | ||
.build()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다른 서비스 파일들과 같게 Impl 파일을 사용하는 형식으로 바꾸면 좋을거같아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auth 서비스는 로그인 방식이 바뀔 상황을 생각해서 전략 패턴으로 구현했는데 product 서비스 부분도 이와 같은 방법으로 구현하는게 좋을지 궁금합니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
인터페이스와 구현체를 분리하여 구현체를 독립적으로 확장할 수 있으며, 구현체 클래스를 변경하거나 확장해도 이를 사용하는 클라이언트의 코드에 영향을 주지 않습니다
이와 같이 구현을 하게 되면 객체지향의 5원칙중 OCP 원칙을 가장 잘 실행해주는 방법입니다.
또한 관습적으로 많이 사용하는 방법이기도 합니다!