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

feat: product tag 생성 #72

Open
wants to merge 3 commits into
base: week7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,33 @@
package poomasi.domain.product._tag.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._tag.dto.TagRequest;
import poomasi.domain.product._tag.service.TagService;

@Controller
@RequiredArgsConstructor
public class TagController {

private final TagService tagService;

@Secured("ROLE_ADMIN")
@PostMapping("/api/products/tag")
public ResponseEntity<?> addTag(@RequestBody TagRequest tagRequest) {
Long tagId = tagService.addTag(tagRequest);
return new ResponseEntity<>(tagId, HttpStatus.CREATED);
}

@Secured("ROLE_ADMIN")
@DeleteMapping("/api/products/tag")
public ResponseEntity<?> deleteTag(@RequestBody TagRequest tagRequest) {
tagService.deleteTag(tagRequest);
return ResponseEntity.ok().build();
}
}
27 changes: 27 additions & 0 deletions src/main/java/poomasi/domain/product/_tag/dto/TagRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package poomasi.domain.product._tag.dto;

import poomasi.domain.product._tag.entity.ProductTag;
import poomasi.domain.product._tag.entity.ProductTagEnum;
import poomasi.global.error.BusinessError;
import poomasi.global.error.BusinessException;

public record TagRequest(
Long productId,
String tagEnum
) {

public ProductTag toEntity() {
ProductTagEnum productTagEnum = null;
try {
productTagEnum = ProductTagEnum.valueOf(tagEnum);
} catch (IllegalArgumentException e) {
throw new BusinessException(BusinessError.INVALID_TAG_NAME);
}

return ProductTag.builder()
.productId(productId)
.productTagEnum(productTagEnum)
.build();

}
}
32 changes: 32 additions & 0 deletions src/main/java/poomasi/domain/product/_tag/entity/ProductTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package poomasi.domain.product._tag.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
public class ProductTag {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

Long productId;

@Enumerated(EnumType.STRING)
ProductTagEnum productTagEnum;

@Builder
public ProductTag(Long productId, ProductTagEnum productTagEnum) {
this.productId = productId;
this.productTagEnum = productTagEnum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package poomasi.domain.product._tag.entity;

public enum ProductTagEnum {
ORGANIC("유기농"),
NonPesticide("무농약");

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,14 @@
package poomasi.domain.product._tag.repository;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import poomasi.domain.product._tag.entity.ProductTag;

@Repository
public interface TagRepository extends JpaRepository<ProductTag, Long> {

@Query(value = "select * from product_tag t where t.product_id=:productId and t.product_tag_enum = :tagEnum", nativeQuery = true)
Optional<ProductTag> findByProductIdAndTagName(Long productId, String tagEnum);
}
44 changes: 44 additions & 0 deletions src/main/java/poomasi/domain/product/_tag/service/TagService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package poomasi.domain.product._tag.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import poomasi.domain.product._tag.dto.TagRequest;
import poomasi.domain.product._tag.entity.ProductTag;
import poomasi.domain.product._tag.repository.TagRepository;
import poomasi.domain.product.entity.Product;
import poomasi.domain.product.repository.ProductRepository;
import poomasi.global.error.BusinessError;
import poomasi.global.error.BusinessException;

@Service
@RequiredArgsConstructor
public class TagService {

private final TagRepository tagRepository;
private final ProductRepository productRepository;

@Transactional
public Long addTag(TagRequest tagRequest) {
Product product = CheckProduct(tagRequest);
ProductTag productTag = tagRequest.toEntity();
productTag = tagRepository.save(productTag);
product.getTags().add(productTag);
return productTag.getId();
}

private Product CheckProduct(TagRequest tagRequest) {
return productRepository.findById(tagRequest.productId())
.orElseThrow(() -> new BusinessException(BusinessError.PRODUCT_NOT_FOUND));
}

@Transactional
public void deleteTag(TagRequest tagRequest) {
Product product = CheckProduct(tagRequest);
ProductTag productTag = tagRepository.findByProductIdAndTagName(tagRequest.productId(),
tagRequest.tagEnum())
.orElseThrow(() -> new BusinessException(BusinessError.TAG_NOT_FOUND));
product.getTags().remove(productTag);
tagRepository.delete(productTag);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package poomasi.domain.product.dto;

import java.util.List;
import lombok.Builder;
import poomasi.domain.product.entity.Product;

Expand All @@ -11,10 +12,15 @@ public record ProductResponse(
Integer stock,
String description,
String imageUrl,
Long categoryId
Long categoryId,
List<String> tags
) {

public static ProductResponse fromEntity(Product product) {
List<String> tags = product.getTags().stream()
.map(a -> a.getProductTagEnum().getKoreanName())
.toList();

return ProductResponse.builder()
.id(product.getId())
.name(product.getName())
Expand All @@ -23,6 +29,7 @@ public static ProductResponse fromEntity(Product product) {
.description(product.getDescription())
.imageUrl(product.getImageUrl())
.categoryId(product.getCategoryId())
.tags(tags)
.build();
}
}
5 changes: 5 additions & 0 deletions src/main/java/poomasi/domain/product/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.UpdateTimestamp;
import poomasi.domain.product._tag.entity.ProductTag;
import poomasi.domain.product.dto.ProductRegisterRequest;
import poomasi.domain.review.entity.Review;

Expand Down Expand Up @@ -64,6 +65,10 @@ public class Product {
@JoinColumn(name = "entityId")
List<Review> reviewList = new ArrayList<>();

@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true)
@JoinColumn(name = "productId")
List<ProductTag> tags = new ArrayList<>();

@Comment("평균 평점")
private double averageRating = 0.0;

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/poomasi/global/error/BusinessError.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package poomasi.global.error;

import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties.Http;
import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -48,6 +49,10 @@ public enum BusinessError {
RESERVATION_ALREADY_CANCELED(HttpStatus.BAD_REQUEST, "이미 취소된 예약입니다."),
RESERVATION_CANCELLATION_PERIOD_EXPIRED(HttpStatus.BAD_REQUEST, "예약 취소 기간이 지났습니다."),

//ProductTag
INVALID_TAG_NAME(HttpStatus.BAD_REQUEST, "존재하지 않는 태그명입니다."),
TAG_NOT_FOUND(HttpStatus.NOT_FOUND, "태그가 존재하지 않습니다."),

// ETC
START_DATE_SHOULD_BE_BEFORE_END_DATE(HttpStatus.BAD_REQUEST, "시작 날짜는 종료 날짜보다 이전이어야 합니다.");

Expand Down