diff --git a/build.gradle b/build.gradle index ad085824..e3d789d7 100644 --- a/build.gradle +++ b/build.gradle @@ -27,10 +27,14 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // mysql implementation 'mysql:mysql-connector-java:8.0.32' + //redis + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + //lombok compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/co/orange/ddanzi/controller/HomeController.java b/src/main/java/co/orange/ddanzi/controller/HomeController.java index db6cec20..edfc84ee 100644 --- a/src/main/java/co/orange/ddanzi/controller/HomeController.java +++ b/src/main/java/co/orange/ddanzi/controller/HomeController.java @@ -4,10 +4,7 @@ import co.orange.ddanzi.service.HomeService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @Slf4j @RequiredArgsConstructor @@ -21,8 +18,10 @@ public ApiResponse home() { return homeService.getProductList(); } - @GetMapping("/product/{id}") - public ApiResponse homeDetail(@PathVariable String id) { + @GetMapping( "/product/{id}") + public ApiResponse homeDetail(@RequestHeader("devicetoken") String devicetoken, + @PathVariable String id) { + log.info("devicetoken: {}", devicetoken); return homeService.getProductDetail(id); } } diff --git a/src/main/java/co/orange/ddanzi/domain/order/pk/OrderAgreementId.java b/src/main/java/co/orange/ddanzi/domain/order/pk/OrderAgreementId.java index 8e711367..2881c9ad 100644 --- a/src/main/java/co/orange/ddanzi/domain/order/pk/OrderAgreementId.java +++ b/src/main/java/co/orange/ddanzi/domain/order/pk/OrderAgreementId.java @@ -1,6 +1,6 @@ package co.orange.ddanzi.domain.order.pk; -import co.orange.ddanzi.domain.product.Item; +import co.orange.ddanzi.domain.order.Order; import co.orange.ddanzi.domain.term.TermOrder; import jakarta.persistence.*; import lombok.Data; @@ -11,8 +11,8 @@ @Data public class OrderAgreementId implements Serializable { @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "item_id") - private Item item; + @JoinColumn(name = "order_id") + private Order order; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "term_order_id") diff --git a/src/main/java/co/orange/ddanzi/domain/product/Category.java b/src/main/java/co/orange/ddanzi/domain/product/Category.java index a4e5e638..a1fbcbd7 100644 --- a/src/main/java/co/orange/ddanzi/domain/product/Category.java +++ b/src/main/java/co/orange/ddanzi/domain/product/Category.java @@ -29,8 +29,8 @@ public class Category extends BaseTimeEntity { private Boolean isForbidden; //금지 여부 @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) - @JoinColumn(name = "discount_id", nullable = true) - private Discount discount; //할인율 + @JoinColumn(name = "default_discount_id", nullable = true) + private DefaultDiscount defaultDiscount; //할인율 @OneToMany(fetch = FetchType.LAZY, mappedBy = "parentCategory", cascade = CascadeType.ALL) private List childrenCategory = new ArrayList<>(); //자식 카테고리 @@ -40,12 +40,12 @@ public class Category extends BaseTimeEntity { private Category parentCategory; //부모 카테고리 @Builder - public Category(Long id, String content, Boolean isForbidden, Category parentCategory, Discount discount) { + public Category(Long id, String content, Boolean isForbidden, Category parentCategory, DefaultDiscount defaultDiscount) { this.id = id; this.content = content; this.isForbidden = isForbidden; this.parentCategory = parentCategory; - this.discount = discount; + this.defaultDiscount = defaultDiscount; } public String getFullPath(){ diff --git a/src/main/java/co/orange/ddanzi/domain/product/DefaultDiscount.java b/src/main/java/co/orange/ddanzi/domain/product/DefaultDiscount.java new file mode 100644 index 00000000..b9429cfe --- /dev/null +++ b/src/main/java/co/orange/ddanzi/domain/product/DefaultDiscount.java @@ -0,0 +1,33 @@ +package co.orange.ddanzi.domain.product; + +import co.orange.ddanzi.global.common.domain.BaseTimeEntity; +import jakarta.persistence.*; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.ColumnDefault; + +@Getter +@NoArgsConstructor +@Table(name = "defalut_discounts") +@Entity +public class DefaultDiscount extends BaseTimeEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "defalut_discount_id") + private Long id; //할인율 고유 ID + + @ColumnDefault("0.3") + @Column(name = "rate", nullable = false) + private Float rate; //할인율, 디폴트 30% + +// @OneToOne(fetch = FetchType.LAZY) +// @JoinColumn(name = "root_category_id", nullable = true) +// private Category rootCategory; //카테고리 = root category + + @Builder + public DefaultDiscount(Long id, Float rate) { + this.id = id; + this.rate = rate; + } +} diff --git a/src/main/java/co/orange/ddanzi/domain/product/Discount.java b/src/main/java/co/orange/ddanzi/domain/product/Discount.java index e094d863..c3e8daae 100644 --- a/src/main/java/co/orange/ddanzi/domain/product/Discount.java +++ b/src/main/java/co/orange/ddanzi/domain/product/Discount.java @@ -5,7 +5,6 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.hibernate.annotations.ColumnDefault; @Getter @NoArgsConstructor @@ -13,21 +12,24 @@ @Entity public class Discount extends BaseTimeEntity { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "discount_id") - private Long id; //할인율 고유 ID + @Column(name = "product_id") + private String id; //상품 고유 ID (PK/FK) - @ColumnDefault("0.3") - @Column(name = "rate", nullable = false) - private Float rate; //할인율, 디폴트 30% + @MapsId + @OneToOne + @JoinColumn(name = "product_id") + private Product product; -// @OneToOne(fetch = FetchType.LAZY) -// @JoinColumn(name = "root_category_id", nullable = true) -// private Category rootCategory; //카테고리 = root category + @Column(name = "discount_rate", nullable = false) + private Float discountRate; + + @Column(name = "discount_price", nullable = false) + private Integer discountPrice; @Builder - public Discount(Long id, Float rate) { - this.id = id; - this.rate = rate; + public Discount(Product product, Float discountRate, Integer discountPrice) { + this.product = product; + this.discountRate = discountRate; + this.discountPrice = discountPrice; } } diff --git a/src/main/java/co/orange/ddanzi/domain/product/Item.java b/src/main/java/co/orange/ddanzi/domain/product/Item.java index 163c38f9..fcae023d 100644 --- a/src/main/java/co/orange/ddanzi/domain/product/Item.java +++ b/src/main/java/co/orange/ddanzi/domain/product/Item.java @@ -9,7 +9,6 @@ import org.hibernate.annotations.ColumnDefault; import java.time.LocalDate; -import java.util.UUID; @Getter @NoArgsConstructor diff --git a/src/main/java/co/orange/ddanzi/domain/product/Option.java b/src/main/java/co/orange/ddanzi/domain/product/Option.java index 1b78ace3..32e82840 100644 --- a/src/main/java/co/orange/ddanzi/domain/product/Option.java +++ b/src/main/java/co/orange/ddanzi/domain/product/Option.java @@ -5,6 +5,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.ColumnDefault; @Getter @NoArgsConstructor @@ -16,17 +17,21 @@ public class Option { @Column(name = "option_id") private Long id; //옵션 고유 ID - @Enumerated(EnumType.STRING) - @Column(name = "type") - private OptionType type; //옵션 종류 +// @Enumerated(EnumType.STRING) +// @Column(name = "type") +// private OptionType type; //옵션 종류 + + @ColumnDefault("'옵션'") + @Column(name = "content", nullable = false) + private String content; //옵션 내용 @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "product_id") private Product product; //참조하는 상품 @Builder - public Option(final OptionType type, final Product product) { - this.type = type; + public Option(String content, Product product) { + this.content = content; this.product = product; } } diff --git a/src/main/java/co/orange/ddanzi/domain/product/OptionDetail.java b/src/main/java/co/orange/ddanzi/domain/product/OptionDetail.java index 0d7caab9..8b826fca 100644 --- a/src/main/java/co/orange/ddanzi/domain/product/OptionDetail.java +++ b/src/main/java/co/orange/ddanzi/domain/product/OptionDetail.java @@ -19,13 +19,18 @@ public class OptionDetail { @Column(name = "content") private String content; //세부 옵션 내용 + @ColumnDefault("true") + @Column(name = "is_available") + private Boolean isAvailable; //옵션 선택 가능 여부 + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "option_id") private Option option; //참조하는 옵션 @Builder - public OptionDetail(String content, Option option) { + public OptionDetail(String content, Boolean isAvailable, Option option) { this.content = content; + this.isAvailable = isAvailable; this.option = option; } } diff --git a/src/main/java/co/orange/ddanzi/domain/product/Product.java b/src/main/java/co/orange/ddanzi/domain/product/Product.java index bcca3f43..07466edf 100644 --- a/src/main/java/co/orange/ddanzi/domain/product/Product.java +++ b/src/main/java/co/orange/ddanzi/domain/product/Product.java @@ -13,41 +13,46 @@ public class Product { @Id @Column(name = "product_id") - private String id; //상품 고유 ID + private String id; //상품 고유 ID @Column(name = "kakao_product_id", nullable = false, unique = true) - private Long kakaoProductId; //카카오톡 선물하기 상품 ID + private Long kakaoProductId; //카카오톡 선물하기 상품 ID @Column(name = "name", nullable = false) - private String name; //상품명 + private String name; //상품명 + + @Column(name = "origin_name", nullable = false, unique = true) + private String originName; //변경 전 상품명 @Column(name = "origin_price", nullable = false) - private Integer originPrice; //정가=선물하기 가격 + private Integer originPrice; //정가=선물하기 가격 - @Column(name = "discount_price", nullable = false) - private Integer discountPrice; //할인 금액 +// @OneToOne(fetch = FetchType.LAZY) +// @JoinColumn(name = "discount_id") +// private Discount discount; //할인 정보 @ColumnDefault("0") @Column(name = "stock", nullable = false) - private Integer stock; //재고 수 + private Integer stock; //재고 수 @Column(name = "img_url") - private String imgUrl; //상품 이미지 + private String imgUrl; //상품 이미지 @Column(name = "info_url") - private String infoUrl; //상품 상세 정보 url + private String infoUrl; //상품 상세 정보 url @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "leaf_category_id") - private Category leafCategory; + private Category leafCategory; //리프 카테고리 @Builder - public Product (String id, Long kakaoProductId, String name, Integer originPrice, Integer discountPrice, String imgUrl, String infoUrl, Integer stock, Category leafCategory) { + public Product (String id, Long kakaoProductId, String name, String originName, Integer originPrice, String imgUrl, String infoUrl, Integer stock, Category leafCategory) { this.id = id; + // this.discount = discount; this.kakaoProductId = kakaoProductId; this.name = name; + this.originName = originName; this.originPrice = originPrice; - this.discountPrice = discountPrice; this.imgUrl = imgUrl; this.infoUrl = infoUrl; this.stock = stock; diff --git a/src/main/java/co/orange/ddanzi/domain/user/Device.java b/src/main/java/co/orange/ddanzi/domain/user/Device.java new file mode 100644 index 00000000..52aab067 --- /dev/null +++ b/src/main/java/co/orange/ddanzi/domain/user/Device.java @@ -0,0 +1,21 @@ +package co.orange.ddanzi.domain.user; + +import co.orange.ddanzi.domain.user.enums.DeviceType; +import jakarta.persistence.*; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Table(name = "device") +@Entity +public class Device { + @Id + @Column(name = "device_token") + private String deviceToken; + + @Column(name = "type") + private DeviceType type; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; +} diff --git a/src/main/java/co/orange/ddanzi/domain/user/enums/DeviceType.java b/src/main/java/co/orange/ddanzi/domain/user/enums/DeviceType.java new file mode 100644 index 00000000..80e4705b --- /dev/null +++ b/src/main/java/co/orange/ddanzi/domain/user/enums/DeviceType.java @@ -0,0 +1,15 @@ +package co.orange.ddanzi.domain.user.enums; + +import lombok.Getter; + +@Getter +public enum DeviceType { + ANDROID("ANDROID"), + IOS("IOS") + ; + + private final String deviceToken; + DeviceType(String deviceToken) { + this.deviceToken = deviceToken; + } +} diff --git a/src/main/java/co/orange/ddanzi/dto/home/HomeDetailResponseDto.java b/src/main/java/co/orange/ddanzi/dto/home/HomeDetailResponseDto.java index 4dc9c363..78ae5669 100644 --- a/src/main/java/co/orange/ddanzi/dto/home/HomeDetailResponseDto.java +++ b/src/main/java/co/orange/ddanzi/dto/home/HomeDetailResponseDto.java @@ -10,11 +10,11 @@ public class HomeDetailResponseDto { private String name; private String category; - // private Boolean isOptionExist; + private Boolean isOptionExist; private Boolean isImminent; private Integer discountRate; private Integer stockCount; private String infoUrl; private Integer interestCount; - // private List optionList; + private List optionList; } diff --git a/src/main/java/co/orange/ddanzi/dto/home/OptionInfo.java b/src/main/java/co/orange/ddanzi/dto/home/OptionInfo.java index 40ed704e..d21d2045 100644 --- a/src/main/java/co/orange/ddanzi/dto/home/OptionInfo.java +++ b/src/main/java/co/orange/ddanzi/dto/home/OptionInfo.java @@ -10,6 +10,6 @@ @Builder public class OptionInfo { private Long optionId; - private OptionType type; + private String type; private List optionDetailList; } diff --git a/src/main/java/co/orange/ddanzi/dto/item/ConfirmProductRequestDto.java b/src/main/java/co/orange/ddanzi/dto/item/ConfirmProductRequestDto.java index ae67cec7..c04b2ee4 100644 --- a/src/main/java/co/orange/ddanzi/dto/item/ConfirmProductRequestDto.java +++ b/src/main/java/co/orange/ddanzi/dto/item/ConfirmProductRequestDto.java @@ -20,7 +20,6 @@ public Product toProduct(String id, Integer discountPrice, Category leafCategory .kakaoProductId(kakaoProductId) .name(productName) .originPrice(originPrice) - .discountPrice(discountPrice) .imgUrl(imgUrl) .infoUrl(infoUrl) .leafCategory(leafCategory) diff --git a/src/main/java/co/orange/ddanzi/global/common/response/Success.java b/src/main/java/co/orange/ddanzi/global/common/response/Success.java index 21e04e89..3983570e 100644 --- a/src/main/java/co/orange/ddanzi/global/common/response/Success.java +++ b/src/main/java/co/orange/ddanzi/global/common/response/Success.java @@ -13,6 +13,8 @@ public enum Success { SUCCESS(HttpStatus.OK, "Request successfully processed."), // 200 OK SUCCESS + GET_REDIS_KEY_SUCCESS(HttpStatus.CREATED, "Successfully retrieved the redis key"), + GET_HOME_INFO_SUCCESS(HttpStatus.OK, "Successfully retrieved home information."), GET_PRODUCT_DETAIL_SUCCESS(HttpStatus.OK, "Successfully retrieved product details."), @@ -46,13 +48,16 @@ public enum Success { // 201 CREATED - CREATE_PRODUCT_SUCCESS(HttpStatus.OK, "Successfully confirmed the product."), - CREATE_ORDER_SUCCESS(HttpStatus.OK, "Successfully completed the order"), - CREATE_ITEM_SUCCESS(HttpStatus.OK, "Successfully listed the item for sale."), + SET_REDIS_KEY_SUCCESS(HttpStatus.CREATED, "Successfully set the redis key"), + + CREATE_PRODUCT_SUCCESS(HttpStatus.CREATED, "Successfully confirmed the product."), + CREATE_ORDER_SUCCESS(HttpStatus.CREATED, "Successfully completed the order"), + CREATE_ITEM_SUCCESS(HttpStatus.CREATED, "Successfully listed the item for sale."), CREATE_INTEREST_SUCCESS(HttpStatus.CREATED, "Successfully added the product to interest List."), CREATE_ADDRESS_SUCCESS(HttpStatus.CREATED, "Successfully added the delivery address."), CREATE_ACCOUNT_SUCCESS(HttpStatus.CREATED, "Successfully added the account."), + ; private final HttpStatus httpStatus; diff --git a/src/main/java/co/orange/ddanzi/global/config/redis/RedisConfig.java b/src/main/java/co/orange/ddanzi/global/config/redis/RedisConfig.java new file mode 100644 index 00000000..817bed4f --- /dev/null +++ b/src/main/java/co/orange/ddanzi/global/config/redis/RedisConfig.java @@ -0,0 +1,36 @@ +package co.orange.ddanzi.global.config.redis; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.StringRedisSerializer; + + +@EnableRedisRepositories +@Configuration +public class RedisConfig { + @Value("${spring.redis.host}") + private String redisHost; + + @Value("${spring.redis.port}") + private int redisPort; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(redisHost, redisPort); + } + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(redisConnectionFactory()); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new StringRedisSerializer()); + return template; + } + +} diff --git a/src/main/java/co/orange/ddanzi/global/config/redis/RedisController.java b/src/main/java/co/orange/ddanzi/global/config/redis/RedisController.java new file mode 100644 index 00000000..99c9bc48 --- /dev/null +++ b/src/main/java/co/orange/ddanzi/global/config/redis/RedisController.java @@ -0,0 +1,35 @@ +package co.orange.ddanzi.global.config.redis; + +import co.orange.ddanzi.global.common.response.ApiResponse; +import co.orange.ddanzi.global.common.response.Success; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +public class RedisController { + + private final RedisTemplate redisTemplate; + + @PostMapping("/redisTest") + public ApiResponse addRedisKey() { + ValueOperations vop = redisTemplate.opsForValue(); + vop.set("red", "apple"); + vop.set("yellow", "apple"); + vop.set("green", "watermelon"); + + return ApiResponse.onSuccess(Success.SET_REDIS_KEY_SUCCESS, null); + } + + @GetMapping("/redisTest/{key}") + public ApiResponse getRedisKey(@PathVariable String key) { + ValueOperations vop = redisTemplate.opsForValue(); + String value = vop.get(key); + return ApiResponse.onSuccess(Success.GET_REDIS_KEY_SUCCESS, value); + } +} diff --git a/src/main/java/co/orange/ddanzi/repository/DefaultDiscountRepository.java b/src/main/java/co/orange/ddanzi/repository/DefaultDiscountRepository.java new file mode 100644 index 00000000..805b54b2 --- /dev/null +++ b/src/main/java/co/orange/ddanzi/repository/DefaultDiscountRepository.java @@ -0,0 +1,11 @@ +package co.orange.ddanzi.repository; + +import co.orange.ddanzi.domain.product.DefaultDiscount; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface DefaultDiscountRepository extends JpaRepository { + @Query(value = "SELECT * FROM discounts d WHERE d.category_id = :categoryId",nativeQuery = true) + DefaultDiscount findByCategoryId(@Param("categoryId") Long categoryId); +} diff --git a/src/main/java/co/orange/ddanzi/repository/DiscountRepository.java b/src/main/java/co/orange/ddanzi/repository/DiscountRepository.java index caba662a..b43e3db8 100644 --- a/src/main/java/co/orange/ddanzi/repository/DiscountRepository.java +++ b/src/main/java/co/orange/ddanzi/repository/DiscountRepository.java @@ -2,10 +2,6 @@ import co.orange.ddanzi.domain.product.Discount; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; -public interface DiscountRepository extends JpaRepository { - @Query(value = "SELECT * FROM discounts d WHERE d.category_id = :categoryId",nativeQuery = true) - Discount findByCategoryId(@Param("categoryId") Long categoryId); +public interface DiscountRepository extends JpaRepository { } diff --git a/src/main/java/co/orange/ddanzi/service/CategoryService.java b/src/main/java/co/orange/ddanzi/service/CategoryService.java index 7543f9e8..c93f14d8 100644 --- a/src/main/java/co/orange/ddanzi/service/CategoryService.java +++ b/src/main/java/co/orange/ddanzi/service/CategoryService.java @@ -1,9 +1,9 @@ package co.orange.ddanzi.service; import co.orange.ddanzi.domain.product.Category; -import co.orange.ddanzi.domain.product.Discount; +import co.orange.ddanzi.domain.product.DefaultDiscount; import co.orange.ddanzi.repository.CategoryRepository; -import co.orange.ddanzi.repository.DiscountRepository; +import co.orange.ddanzi.repository.DefaultDiscountRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -15,7 +15,7 @@ @Service public class CategoryService { private final CategoryRepository categoryRepository; - private final DiscountRepository discountRepository; + private final DefaultDiscountRepository defaultDiscountRepository; @Transactional public Pair createOrGetCategory(String fullPath, Boolean isForbidden){ @@ -29,7 +29,7 @@ public Pair createOrGetCategory(String fullPath, Boolean isForb if (parentCategory == null) { // 루트 카테고리의 경우 parentCategory = getRootCategoryOrCreate(categoryContent, isForbidden); - discountRate = parentCategory.getDiscount().getRate(); + discountRate = parentCategory.getDefaultDiscount().getRate(); } else { parentCategory = getCategoryOrCreate(categoryContent, parentCategory, isForbidden); @@ -42,24 +42,24 @@ public Pair createOrGetCategory(String fullPath, Boolean isForb public Category getRootCategoryOrCreate(String content, Boolean isForbidden) { return categoryRepository.findByContent(content) .orElseGet(() -> { - Discount newDiscount = createDiscount(); - log.info("discount 객체 생성 discount_id -> {}", newDiscount.getId()); + DefaultDiscount newDefaultDiscount = createDiscount(); + log.info("discount 객체 생성 discount_id -> {}", newDefaultDiscount.getId()); Category newCategory = Category.builder() .content(content) .isForbidden(isForbidden) .parentCategory(null) - .discount(newDiscount) + .defaultDiscount(newDefaultDiscount) .build(); log.info("root category 객체 생성 category_id -> {}", newCategory.getId()); return categoryRepository.save(newCategory); }); } - public Discount createDiscount(){ - Discount newDiscount = Discount.builder() + public DefaultDiscount createDiscount(){ + DefaultDiscount newDefaultDiscount = DefaultDiscount.builder() .rate(0.3f) .build(); - return discountRepository.save(newDiscount); + return defaultDiscountRepository.save(newDefaultDiscount); } public Category getCategoryOrCreate(String content, Category parentCategory, Boolean isForbidden) { @@ -69,7 +69,7 @@ public Category getCategoryOrCreate(String content, Category parentCategory, Boo .content(content) .isForbidden(isForbidden) .parentCategory(parentCategory) - .discount(null) + .defaultDiscount(null) .build(); categoryRepository.save(newCategory); log.info("새로운 category 객체 생성 category_id -> {}", newCategory.getId()); diff --git a/src/main/java/co/orange/ddanzi/service/HomeService.java b/src/main/java/co/orange/ddanzi/service/HomeService.java index c0886f2c..f9679f4b 100644 --- a/src/main/java/co/orange/ddanzi/service/HomeService.java +++ b/src/main/java/co/orange/ddanzi/service/HomeService.java @@ -32,6 +32,7 @@ public class HomeService { public ApiResponse getProductList(){ Banner banner = bannerRepository.findByIsSelected(Boolean.TRUE); List productList = productRepository.findAllByStock(0); + List productInfoList = setProductList(productList, interestProductRepository); HomeResponseDto responseDto = HomeResponseDto.builder() .homeImgUrl(banner.getImgUrl()) @@ -53,22 +54,19 @@ public ApiResponse getProductDetail(String productId){ Category leafCategory = product.getLeafCategory(); log.info("카테고리 조회 성공 -> catgory_id: {}", leafCategory.getId()); - log.info("루트 카테고리와 full path 조회"); - Pair rootCategoryAndFullPath = leafCategory.getRootCategoryAndFullPath(); - Category rootCategory = rootCategoryAndFullPath.getFirst(); - String categoryFullPath = rootCategoryAndFullPath.getSecond(); + log.info("카테고리 full path 조회"); + String categoryFullPath = leafCategory.getFullPath(); - log.info("루트 카테고리의 할인율 조회 -> catgory_id: {}",rootCategory.getId()); - Discount discountEntity = discountRepository.findByCategoryId(rootCategory.getId()); - if(discountEntity == null){ + log.info("상품 할인율 조회 -> product_id: {}",productId); + Discount discount = discountRepository.findById(productId).orElse(null); + if(discount == null){ ApiResponse.onFailure(Error.DISCOUNT_INFO_NOT_FOUND, null); } - Float discountRateFloat = discountEntity.getRate() * 100; + Float discountRateFloat = discount.getDiscountRate() * 100; Integer discountRate = discountRateFloat.intValue(); - // 옵션 관련 정보 삭제 - //log.info("해당 상품의 옵션 조회"); - //List optionList = getOptionList(productId); + log.info("해당 상품의 옵션 조회"); + List optionList = getOptionList(productId); log.info("해당 상품의 찜 개수 조회"); Integer interestCount = interestProductRepository.countByProductIdWithLimit(productId); @@ -77,27 +75,28 @@ public ApiResponse getProductDetail(String productId){ HomeDetailResponseDto responseDto = HomeDetailResponseDto.builder() .name(product.getName()) .category(categoryFullPath) - //.isOptionExist(!optionList.isEmpty()) + .isOptionExist(!optionList.isEmpty()) .isImminent(true) .discountRate(discountRate) .stockCount(product.getStock()) .infoUrl(product.getInfoUrl()) .interestCount(interestCount) - //.optionList(optionList) + .optionList(optionList) .build(); return ApiResponse.onSuccess(Success.GET_PRODUCT_DETAIL_SUCCESS,responseDto); } - public static List setProductList(List productList, InterestProductRepository interestProductRepository){ + public List setProductList(List productList, InterestProductRepository interestProductRepository){ List productInfoList = new ArrayList<>(); for(Product product : productList){ + Discount discount = discountRepository.findById(product.getId()).orElse(null); productInfoList.add(ProductInfo.builder() .productId(product.getId()) .kakaoProductId(product.getKakaoProductId()) .name(product.getName()) .originPrice(product.getOriginPrice()) - .salePrice(product.getOriginPrice() - product.getDiscountPrice()) + .salePrice(product.getOriginPrice() - discount.getDiscountPrice()) .imgUrl(product.getImgUrl()) .interestCount(interestProductRepository.countByProductIdWithLimit(product.getId())) .build()); @@ -116,17 +115,76 @@ private List getOptionList(String productId){ optionDetailInfoList.add(OptionDetailInfo.builder() .optionDetailId(optionDetail.getId()) .content(optionDetail.getContent()) - //.isAvailable(optionDetail.getIsAvailable()) + .isAvailable(optionDetail.getIsAvailable()) .build()); } log.info("세부 옵션 조회 성공 -> option_id: {}", option.getId()); optionInfoList.add(OptionInfo.builder() .optionId(option.getId()) - .type(option.getType()) + .type(option.getContent()) .optionDetailList(optionDetailInfoList) .build()); } log.info("해당 상품의 전체 옵션 조회 성공 -> product_id: {}",productId); return optionInfoList; } + + + + + + @Transactional + public ApiResponse getProductDetail_oldVersion(String productId){ + log.info("상품 조회 -> product_id: {}", productId); + Product product = productRepository.findById(productId).orElse(null); + if(product == null){ + return ApiResponse.onFailure(Error.PRODUCT_NOT_FOUND, null); + } + log.info("해당 상품의 리프 카테고리 찾기"); + if(product.getLeafCategory() == null){ + ApiResponse.onFailure(Error.CATEGORY_NOT_FOUND, null); + } + Category leafCategory = product.getLeafCategory(); + log.info("카테고리 조회 성공 -> catgory_id: {}", leafCategory.getId()); + + log.info("루트 카테고리와 full path 조회"); + Pair rootCategoryAndFullPath = leafCategory.getRootCategoryAndFullPath(); + Category rootCategory = rootCategoryAndFullPath.getFirst(); + String categoryFullPath = rootCategoryAndFullPath.getSecond(); + + log.info("루트 카테고리의 할인율 조회 -> catgory_id: {}",rootCategory.getId()); + //DefaultDiscount defaultDiscountEntity = defaultDiscountRepository.findByCategoryId(rootCategory.getId()); +// if(defaultDiscountEntity == null){ +// ApiResponse.onFailure(Error.DISCOUNT_INFO_NOT_FOUND, null); +// } +// Float discountRateFloat = defaultDiscountEntity.getRate() * 100; +// Integer discountRate = discountRateFloat.intValue(); + + log.info("해당 상품의 옵션 조회"); + List optionList = getOptionList(productId); + + log.info("해당 상품의 찜 개수 조회"); + Integer interestCount = interestProductRepository.countByProductIdWithLimit(productId); + + + HomeDetailResponseDto responseDto = HomeDetailResponseDto.builder() + .name(product.getName()) + .category(categoryFullPath) + .isOptionExist(!optionList.isEmpty()) + .isImminent(true) + //.discountRate(discountRate) + .stockCount(product.getStock()) + .infoUrl(product.getInfoUrl()) + .interestCount(interestCount) + .optionList(optionList) + .build(); + + return ApiResponse.onSuccess(Success.GET_PRODUCT_DETAIL_SUCCESS,responseDto); + } + } + + + + + diff --git a/src/main/java/co/orange/ddanzi/service/MyPageService.java b/src/main/java/co/orange/ddanzi/service/MyPageService.java index e8abfcb6..f88fb042 100644 --- a/src/main/java/co/orange/ddanzi/service/MyPageService.java +++ b/src/main/java/co/orange/ddanzi/service/MyPageService.java @@ -22,6 +22,7 @@ @RequiredArgsConstructor @Service public class MyPageService { + private final HomeService homeService; private final UserRepository userRepository; private final InterestProductRepository interestProductRepository; private final ProductRepository productRepository; @@ -41,7 +42,7 @@ public ApiResponse getInterest(){ log.info("찜한 상품 찾기"); List productList = interestProductRepository.findProductsByUserId(user.getId()); log.info("찜한 상품의 정보 입력하기"); - List productInfoList = HomeService.setProductList(productList,interestProductRepository); + List productInfoList = homeService.setProductList(productList,interestProductRepository); MyPageInterestResponseDto responseDto = MyPageInterestResponseDto.builder() .totalCount(productList.size()) .productList(productInfoList) diff --git a/src/main/java/co/orange/ddanzi/service/ProductService.java b/src/main/java/co/orange/ddanzi/service/ProductService.java index f34db969..9421e20c 100644 --- a/src/main/java/co/orange/ddanzi/service/ProductService.java +++ b/src/main/java/co/orange/ddanzi/service/ProductService.java @@ -1,6 +1,7 @@ package co.orange.ddanzi.service; import co.orange.ddanzi.domain.product.Category; +import co.orange.ddanzi.domain.product.Discount; import co.orange.ddanzi.domain.product.Product; import co.orange.ddanzi.domain.user.Address; import co.orange.ddanzi.domain.user.User; @@ -11,17 +12,13 @@ import co.orange.ddanzi.global.common.exception.Error; import co.orange.ddanzi.global.common.response.ApiResponse; import co.orange.ddanzi.global.common.response.Success; -import co.orange.ddanzi.repository.AddressRepository; -import co.orange.ddanzi.repository.CategoryRepository; -import co.orange.ddanzi.repository.ProductRepository; -import co.orange.ddanzi.repository.UserRepository; +import co.orange.ddanzi.repository.*; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; -import java.util.Optional; @Slf4j @RequiredArgsConstructor @@ -29,6 +26,7 @@ public class ProductService { private final UserRepository userRepository; private final ProductRepository productRepository; + private final DiscountRepository discountRepository; private final AddressRepository addressRepository; private final CategoryService categoryService; @@ -38,6 +36,7 @@ public ApiResponse checkOrderProduct(String productId){ if(product == null){ return ApiResponse.onFailure(Error.PRODUCT_NOT_FOUND, null); } + Discount discount = discountRepository.findById(productId).orElse(null); User user = userRepository.findById(1L).orElse(null); Address address = addressRepository.findByUser(user); @@ -53,9 +52,9 @@ public ApiResponse checkOrderProduct(String productId){ .imgUrl(product.getImgUrl()) .originPrice(product.getOriginPrice()) .addressInfo(addressInfo) - .discountPrice(product.getDiscountPrice()) + .discountPrice(discount.getDiscountPrice()) .charge(null) - .totalPrice(product.getOriginPrice() - product.getDiscountPrice()) + .totalPrice(product.getOriginPrice() - discount.getDiscountPrice()) .build(); return ApiResponse.onSuccess(Success.GET_ORDER_PRODUCT_SUCCESS, responseDto); } @@ -78,11 +77,12 @@ public ApiResponse confirmProduct(ConfirmProductRequestDto requestDto){ product = newProduct; log.info("상품 등록 완료 -> product_id: {}",product.getId()); } + Discount discount = discountRepository.findById(product.getId()).orElse(null); ConfirmProductResponseDto responseDto = ConfirmProductResponseDto.builder() .productId(product.getId()) .productName(product.getName()) .originPrice(product.getOriginPrice()) - .salePrice(product.getOriginPrice() - product.getDiscountPrice()) + .salePrice(product.getOriginPrice() - discount.getDiscountPrice()) .build(); return ApiResponse.onSuccess(Success.CREATE_PRODUCT_SUCCESS, responseDto); } diff --git a/src/main/java/co/orange/ddanzi/service/SearchService.java b/src/main/java/co/orange/ddanzi/service/SearchService.java index f66655f4..472c7761 100644 --- a/src/main/java/co/orange/ddanzi/service/SearchService.java +++ b/src/main/java/co/orange/ddanzi/service/SearchService.java @@ -17,14 +17,15 @@ @RequiredArgsConstructor @Service public class SearchService { + private final HomeService homeService; private final ProductRepository productRepository; private final InterestProductRepository interestProductRepository; @Transactional public ApiResponse searchKeyword(String keyword) { - List topSearchedList = new ArrayList<>(); + List topSearchedList = List.of("예시1", "예시2", "예시3"); List productList = productRepository.findAllByName(keyword); - List productInfoList = HomeService.setProductList(productList, interestProductRepository); + List productInfoList = homeService.setProductList(productList, interestProductRepository); return ApiResponse.onSuccess(Success.GET_SEARCH_RESULTS_SUCCESS, SearchResultResponseDto.builder() .topSearchedList(topSearchedList) .searchedProductList(productInfoList) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d5b93519..cacc530a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,3 +1,4 @@ spring: profiles: - active: db \ No newline at end of file + active: db +