diff --git a/src/main/java/com/backend/soullive_a/dto/response/ProductResponse.java b/src/main/java/com/backend/soullive_a/dto/response/ProductResponse.java index 9d9b173..3c55b71 100644 --- a/src/main/java/com/backend/soullive_a/dto/response/ProductResponse.java +++ b/src/main/java/com/backend/soullive_a/dto/response/ProductResponse.java @@ -8,6 +8,7 @@ @Builder public record ProductResponse( + Long userId, Long productId, String company, String brand, @@ -47,6 +48,7 @@ public static ProductResponse fromProduct( .collect(Collectors.toList()); return ProductResponse.builder() + .userId(product.getUser().getId()) .productId(product.getId()) .company(product.getCompany()) .brand(product.getBrand()) diff --git a/src/main/java/com/backend/soullive_a/entity/Product.java b/src/main/java/com/backend/soullive_a/entity/Product.java index c592856..8208194 100644 --- a/src/main/java/com/backend/soullive_a/entity/Product.java +++ b/src/main/java/com/backend/soullive_a/entity/Product.java @@ -11,10 +11,14 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Product { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "PRODUCT_ID", nullable = false) private Long id; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "USER_ID", nullable = false) + private User user; + @Column(name = "COMPANY", nullable = false) private String company; diff --git a/src/main/java/com/backend/soullive_a/entity/User.java b/src/main/java/com/backend/soullive_a/entity/User.java index e419489..0d35a5a 100644 --- a/src/main/java/com/backend/soullive_a/entity/User.java +++ b/src/main/java/com/backend/soullive_a/entity/User.java @@ -14,12 +14,14 @@ @Entity @Table(name = "user") @Getter -@Builder @NoArgsConstructor @AllArgsConstructor +@Builder +@NoArgsConstructor +@AllArgsConstructor public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", nullable = false) + @Column(name = "USER_ID", nullable = false) private Long id; private String phoneNumber; diff --git a/src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java b/src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java index c9572ba..eb23c07 100644 --- a/src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java +++ b/src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java @@ -10,9 +10,14 @@ public enum ErrorCode { //실패 - 필요시 만들어 쓰세요 /** - * 2000 : User 오류 + * 2000 : User 관련 오류 */ - NOT_FOUND_USER(false, 2001, "사용자를 찾을 수 없습니다", HttpStatus.NOT_FOUND); + NOT_FOUND_USER(false, 2001, "사용자를 찾을 수 없습니다", HttpStatus.NOT_FOUND), + + /** + * 3000 : Product 관련 오류 + */ + NOT_FOUND_PRODUCT(false, 3001, "광고 상품을 찾을 수 없습니다",HttpStatus.NOT_FOUND); private final boolean isSuccess; private final int code; diff --git a/src/main/java/com/backend/soullive_a/exception/custom/NotFoundProductException.java b/src/main/java/com/backend/soullive_a/exception/custom/NotFoundProductException.java new file mode 100644 index 0000000..7e5c0da --- /dev/null +++ b/src/main/java/com/backend/soullive_a/exception/custom/NotFoundProductException.java @@ -0,0 +1,15 @@ +package com.backend.soullive_a.exception.custom; + +import com.backend.soullive_a.exception.base.BaseException; +import com.backend.soullive_a.exception.base.ErrorCode; + +public class NotFoundProductException extends BaseException { + + public NotFoundProductException() { + super(ErrorCode.NOT_FOUND_PRODUCT); + } + + public NotFoundProductException(String message) { + super(ErrorCode.NOT_FOUND_PRODUCT, message); + } +} diff --git a/src/main/java/com/backend/soullive_a/security/filter/JwtAuthenticationFilter.java b/src/main/java/com/backend/soullive_a/security/filter/JwtAuthenticationFilter.java index 4c12148..77261f2 100644 --- a/src/main/java/com/backend/soullive_a/security/filter/JwtAuthenticationFilter.java +++ b/src/main/java/com/backend/soullive_a/security/filter/JwtAuthenticationFilter.java @@ -47,8 +47,8 @@ private Authentication getAuthentication() { User user = User.builder() .id(1L) - .phoneNumber("010-3370-2740") - .password("1234") + .phoneNumber("010-0000-0000") + .password("password") .build(); return new UsernamePasswordAuthenticationToken(user, null, null); diff --git a/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java b/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java index 598702f..a9969eb 100644 --- a/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java +++ b/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java @@ -5,8 +5,11 @@ import com.backend.soullive_a.dto.request.CreateProductRequest; import com.backend.soullive_a.dto.response.ProductResponse; import com.backend.soullive_a.entity.*; +import com.backend.soullive_a.exception.custom.NotFoundProductException; import com.backend.soullive_a.repository.*; import com.backend.soullive_a.service.ProductService; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -27,10 +30,18 @@ public class ProductServiceImpl implements ProductService { private final AgeRepository ageRepository; private final RangeRepository rangeRepository; + @PersistenceContext + private EntityManager entityManager; + @Override @Transactional public ProductResponse createProduct(CreateProductRequest request) { + User user = entityManager.find(User.class, 1L); + + log.info(user.getId()+"서비스"); + Product product = Product.builder() + .user(user) .company(request.company()) .brand(request.brand()) .product(request.product()) @@ -45,8 +56,8 @@ public ProductResponse createProduct(CreateProductRequest request) { .brandImage(image) .build(); brandImages.add(brandImage); - brandImageRepository.save(brandImage); } + brandImageRepository.saveAll(brandImages); List productImages = new ArrayList<>(); for (String image : request.productImage()) { @@ -55,8 +66,8 @@ public ProductResponse createProduct(CreateProductRequest request) { .productImage(image) .build(); productImages.add(productImage); - productImageRepository.save(productImage); } + productImageRepository.saveAll(productImages); List genders = new ArrayList<>(); for (GenderType genderType : request.gender()) { @@ -65,8 +76,8 @@ public ProductResponse createProduct(CreateProductRequest request) { .gender(genderType) .build(); genders.add(gender); - genderRepository.save(gender); } + genderRepository.saveAll(genders); List ages = new ArrayList<>(); for (AgeType ageType : request.age()) { @@ -75,8 +86,8 @@ public ProductResponse createProduct(CreateProductRequest request) { .age(ageType) .build(); ages.add(age); - ageRepository.save(age); } + ageRepository.saveAll(ages); List ranges = new ArrayList<>(); for (String rangeType : request.range()) { @@ -85,8 +96,9 @@ public ProductResponse createProduct(CreateProductRequest request) { .range(rangeType) .build(); ranges.add(range); - rangeRepository.save(range); } + rangeRepository.saveAll(ranges); + return ProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges); } @@ -94,7 +106,7 @@ public ProductResponse createProduct(CreateProductRequest request) { @Transactional(readOnly = true) public ProductResponse getProduct(Long productId) { Product product = productRepository.findById(productId) - .orElseThrow(); + .orElseThrow(() -> new NotFoundProductException("pk = " + productId + "광고 상품을 찾을 수 없습니다")); List brandImages = brandImageRepository.findAllByProductId(productId); List productImages = productImageRepository.findAllByProductId(productId); List genders = genderRepository.findAllByProductId(productId); diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000..20e25ba --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1 @@ +INSERT INTO test_db.user (user_id, password, phone_number) VALUES (1, 'password', '010-0000-0000'); \ No newline at end of file