-
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
feat: product tag 생성 #72
base: week7
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package poomasi.domain.product.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.annotation.Secured; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import poomasi.domain.product.dto.ProductTagRequest; | ||
import poomasi.domain.product.service.ProductTagService; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
public class ProductTagController { | ||
|
||
private final ProductTagService productTagService; | ||
|
||
@Secured("ROLE_ADMIN") | ||
@PostMapping("/api/products/tag") | ||
public ResponseEntity<?> addTag(@RequestBody ProductTagRequest productTagRequest) { | ||
productTagService.addTag(productTagRequest); | ||
return new ResponseEntity<>(HttpStatus.CREATED); | ||
} | ||
|
||
@Secured("ROLE_ADMIN") | ||
@DeleteMapping("/api/products/tag") | ||
public ResponseEntity<?> deleteTag(@RequestBody ProductTagRequest productTagRequest) { | ||
productTagService.deleteTag(productTagRequest); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package poomasi.domain.product.dto; | ||
|
||
public record ProductTagRequest( | ||
Long productId, | ||
String tagEnum | ||
) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package poomasi.domain.product.entity; | ||
|
||
public enum ProductTagEnum { | ||
ORGANIC("유기농"), | ||
NonPesticide("무농약"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum이니깐 |
||
|
||
private final String value; | ||
|
||
private ProductTagEnum(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getKoreanName() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package poomasi.domain.product.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import poomasi.domain.product.dto.ProductTagRequest; | ||
import poomasi.domain.product.entity.Product; | ||
import poomasi.domain.product.entity.ProductTagEnum; | ||
import poomasi.domain.product.repository.ProductRepository; | ||
import poomasi.global.error.BusinessError; | ||
import poomasi.global.error.BusinessException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProductTagService { | ||
|
||
private final ProductRepository productRepository; | ||
|
||
@Transactional | ||
public void addTag(ProductTagRequest productTagRequest) { | ||
Product product = CheckProduct(productTagRequest); | ||
|
||
ProductTagEnum productTagEnum = null; | ||
try { | ||
productTagEnum = ProductTagEnum.valueOf(productTagRequest.tagEnum()); | ||
} catch (IllegalArgumentException e) { | ||
throw new BusinessException(BusinessError.INVALID_TAG_NAME); | ||
} | ||
|
||
product.getTags().add(productTagEnum); | ||
} | ||
|
||
private Product CheckProduct(ProductTagRequest productTagRequest) { | ||
return productRepository.findById(productTagRequest.productId()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. findByIdAndDeletedAtIsNull로 바꾸는 것은 어떨까요? |
||
.orElseThrow(() -> new BusinessException(BusinessError.PRODUCT_NOT_FOUND)); | ||
} | ||
|
||
@Transactional | ||
public void deleteTag(ProductTagRequest productTagRequest) { | ||
Product product = CheckProduct(productTagRequest); | ||
|
||
ProductTagEnum productTagEnum = null; | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 addTag에 있는 로직과 동일해서 공통화시키면 좋을 것 같다는 생각이 듭니당 |
||
productTagEnum = ProductTagEnum.valueOf(productTagRequest.tagEnum()); | ||
} catch (IllegalArgumentException e) { | ||
throw new BusinessException(BusinessError.INVALID_TAG_NAME); | ||
} | ||
|
||
product.getTags().remove(productTagEnum); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
public record ReviewResponse | ||
(Long id, | ||
Long productId, | ||
Long entityId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. product, farm 둘다 쓸 ㅇ예정이라 Entity로 네이밍을 수정하신건지 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 product랑 farm이랑 id값이 중복될 수 있으니 따로 type을 받아야겠다 생각했는데 다만 리뷰를 가지는 엔티티가 많을 경우에는 type을 받으면 하나의 로직으로 공통화하기 좋을 것 같기는 하네여 |
||
//Long reviewerId, | ||
Float rating, | ||
String content | ||
|
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.
혹시 Lazy대신 Eager로 설정하신 이유가 궁금합니다!!!