Skip to content

Commit

Permalink
feat: #16 product에 user 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sominyun committed Mar 5, 2024
1 parent 9e6f202 commit 61cd696
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@Builder
public record ProductResponse(
Long userId,
Long productId,
String company,
String brand,
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/backend/soullive_a/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/backend/soullive_a/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -45,8 +56,8 @@ public ProductResponse createProduct(CreateProductRequest request) {
.brandImage(image)
.build();
brandImages.add(brandImage);
brandImageRepository.save(brandImage);
}
brandImageRepository.saveAll(brandImages);

List<ProductImage> productImages = new ArrayList<>();
for (String image : request.productImage()) {
Expand All @@ -55,8 +66,8 @@ public ProductResponse createProduct(CreateProductRequest request) {
.productImage(image)
.build();
productImages.add(productImage);
productImageRepository.save(productImage);
}
productImageRepository.saveAll(productImages);

List<Gender> genders = new ArrayList<>();
for (GenderType genderType : request.gender()) {
Expand All @@ -65,8 +76,8 @@ public ProductResponse createProduct(CreateProductRequest request) {
.gender(genderType)
.build();
genders.add(gender);
genderRepository.save(gender);
}
genderRepository.saveAll(genders);

List<Age> ages = new ArrayList<>();
for (AgeType ageType : request.age()) {
Expand All @@ -75,8 +86,8 @@ public ProductResponse createProduct(CreateProductRequest request) {
.age(ageType)
.build();
ages.add(age);
ageRepository.save(age);
}
ageRepository.saveAll(ages);

List<Range> ranges = new ArrayList<>();
for (String rangeType : request.range()) {
Expand All @@ -85,16 +96,17 @@ 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);
}

@Override
@Transactional(readOnly = true)
public ProductResponse getProduct(Long productId) {
Product product = productRepository.findById(productId)
.orElseThrow();
.orElseThrow(() -> new NotFoundProductException("pk = " + productId + "광고 상품을 찾을 수 없습니다"));
List<BrandImage> brandImages = brandImageRepository.findAllByProductId(productId);
List<ProductImage> productImages = productImageRepository.findAllByProductId(productId);
List<Gender> genders = genderRepository.findAllByProductId(productId);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO test_db.user (user_id, password, phone_number) VALUES (1, 'password', '010-0000-0000');

0 comments on commit 61cd696

Please sign in to comment.