Skip to content

Commit

Permalink
feat: #16 product post,get api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sominyun committed Mar 3, 2024
1 parent 8a08a30 commit 6444b55
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.backend.soullive_a.controller;

import com.backend.soullive_a.dto.request.CreateProductRequest;
import com.backend.soullive_a.dto.response.CreateProductResponse;
import com.backend.soullive_a.exception.base.BaseResponse;
import com.backend.soullive_a.service.ProductService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
public class ProductController {

private final ProductService productService;

@PostMapping("")
public BaseResponse<CreateProductResponse> createProduct(
@RequestBody @Valid CreateProductRequest createProductRequest
) {
return BaseResponse.<CreateProductResponse>builder()
.isSuccess(true)
.code(200)
.message("광고 상품을 등록했습니다")
.data(productService.createProduct(createProductRequest))
.build();
}

@GetMapping("/{id}")
public BaseResponse<CreateProductResponse> getProduct(@PathVariable Long id) {
return BaseResponse.<CreateProductResponse>builder()
.isSuccess(true)
.code(200)
.message("productId = " + id + " 광고 상품을 불러옵니다")
.data(productService.getProduct(id))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.backend.soullive_a.dto.request;

import com.backend.soullive_a.constant.AgeType;
import com.backend.soullive_a.constant.GenderType;
import jakarta.validation.constraints.NotNull;

import java.util.List;

public record CreateProductRequest (
@NotNull String company,
String brand,
@NotNull List<String> brandImage,
@NotNull String product,
@NotNull String characteristic,
@NotNull List<String> productImage,
@NotNull List<GenderType> gender,
@NotNull List<AgeType> age,
@NotNull List<String> range
){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.soullive_a.dto.response;

import com.backend.soullive_a.constant.AgeType;

public record AgeResponse(
Long ageId,
AgeType ageType
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.backend.soullive_a.dto.response;

public record BrandImageResponse (
Long brandImageId,
String brandImage
){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.backend.soullive_a.dto.response;

import com.backend.soullive_a.entity.*;
import lombok.Builder;

import java.util.List;
import java.util.stream.Collectors;

@Builder
public record CreateProductResponse(
Long productId,
String company,
String brand,
List<BrandImageResponse> brandImages,
String product,
String characteristic,
List<ProductImageResponse> productImages,
List<GenderResponse> genders,
List<AgeResponse> ages,
List<RangeResponse> ranges
) {
public static CreateProductResponse fromProduct(
Product product,
List<BrandImage> brandImage,
List<ProductImage> productImage,
List<Gender> genderType,
List<Age> ageType,
List<Range> rangeType) {
List<BrandImageResponse> brandImages = brandImage.stream()
.map(image -> new BrandImageResponse(image.getId(), image.getBrandImage()))
.collect(Collectors.toList());

List<ProductImageResponse> productImages = productImage.stream()
.map(image -> new ProductImageResponse(image.getId(), image.getProductImage()))
.collect(Collectors.toList());

List<GenderResponse> genders = genderType.stream()
.map(gender -> new GenderResponse(gender.getId(), gender.getGender()))
.collect(Collectors.toList());

List<AgeResponse> ages = ageType.stream()
.map(age -> new AgeResponse(age.getId(), age.getAge()))
.collect(Collectors.toList());

List<RangeResponse> ranges = rangeType.stream()
.map(range -> new RangeResponse(range.getId(), range.getRange()))
.collect(Collectors.toList());

return CreateProductResponse.builder()
.productId(product.getId())
.company(product.getProduct())
.brand(product.getBrand())
.brandImages(brandImages)
.product(product.getProduct())
.characteristic(product.getCharacteristic())
.productImages(productImages)
.genders(genders)
.ages(ages)
.ranges(ranges)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.soullive_a.dto.response;

import com.backend.soullive_a.constant.GenderType;

public record GenderResponse (
Long genderId,
GenderType gender
){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.backend.soullive_a.dto.response;

public record ProductImageResponse(
Long productImageId,
String productImage
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.backend.soullive_a.dto.response;

public record RangeResponse(
Long rangeId,
String range
) {
}
12 changes: 12 additions & 0 deletions src/main/java/com/backend/soullive_a/repository/AgeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.entity.Age;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AgeRepository extends JpaRepository<Age, Long> {
List<Age> findAllByProductId(Long productId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.entity.BrandImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BrandImageRepository extends JpaRepository<BrandImage, Long> {
List<BrandImage> findAllByProductId(Long productId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.entity.Gender;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GenderRepository extends JpaRepository<Gender, Long> {
List<Gender> findAllByProductId(Long productId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.entity.ProductImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProductImageRepository extends JpaRepository<ProductImage, Long> {
List<ProductImage> findAllByProductId(Long productId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.entity.Range;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RangeRepository extends JpaRepository<Range, Long> {
List<Range> findAllByProductId(Long productId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.soullive_a.service;

import com.backend.soullive_a.dto.request.CreateProductRequest;
import com.backend.soullive_a.dto.response.CreateProductResponse;

public interface ProductService {
CreateProductResponse createProduct(CreateProductRequest request);
CreateProductResponse getProduct(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.backend.soullive_a.service.impl;

import com.backend.soullive_a.constant.AgeType;
import com.backend.soullive_a.constant.GenderType;
import com.backend.soullive_a.dto.request.CreateProductRequest;
import com.backend.soullive_a.dto.response.CreateProductResponse;
import com.backend.soullive_a.entity.*;
import com.backend.soullive_a.repository.*;
import com.backend.soullive_a.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ProductServiceImpl implements ProductService {

private final ProductRepository productRepository;
private final BrandImageRepository brandImageRepository;
private final ProductImageRepository productImageRepository;
private final GenderRepository genderRepository;
private final AgeRepository ageRepository;
private final RangeRepository rangeRepository;

@Override
@Transactional
public CreateProductResponse createProduct(CreateProductRequest request){
log.info("productservice 들어옴");
Product product = Product.builder()
.company(request.company())
.brand(request.brand())
.product(request.product())
.characteristic(request.characteristic())
.build();
productRepository.save(product);

List<BrandImage> brandImages = new ArrayList<>();
for (String image : request.brandImage()) {
BrandImage brandImage = BrandImage.builder()
.product(product)
.brandImage(image)
.build();
brandImages.add(brandImage);
brandImageRepository.save(brandImage);
}

List<ProductImage> productImages = new ArrayList<>();
for (String image : request.productImage()) {
ProductImage productImage = ProductImage.builder()
.product(product)
.productImage(image)
.build();
productImages.add(productImage);
productImageRepository.save(productImage);
}

List<Gender> genders = new ArrayList<>();
for (GenderType genderType : request.gender()) {
Gender gender = Gender.builder()
.product(product)
.gender(genderType)
.build();
genders.add(gender);
genderRepository.save(gender);
}

List<Age> ages = new ArrayList<>();
for (AgeType ageType : request.age()) {
Age age = Age.builder()
.product(product)
.age(ageType)
.build();
ages.add(age);
ageRepository.save(age);
}

List<Range> ranges = new ArrayList<>();
for (String rangeType : request.range()) {
Range range = Range.builder()
.product(product)
.range(rangeType)
.build();
ranges.add(range);
rangeRepository.save(range);
}
return CreateProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges);
}

@Override
@Transactional
public CreateProductResponse getProduct(Long productId) {
Product product=productRepository.findById(productId)
.orElseThrow();
List<BrandImage> brandImages = brandImageRepository.findAllByProductId(productId);
List<ProductImage> productImages = productImageRepository.findAllByProductId(productId);
List<Gender> genders = genderRepository.findAllByProductId(productId);
List<Age> ages = ageRepository.findAllByProductId(productId);
List<Range> ranges=rangeRepository.findAllByProductId(productId);
return CreateProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges);
}
}

0 comments on commit 6444b55

Please sign in to comment.