Skip to content

Commit

Permalink
Merge pull request #17 from soulive-A/feature/#16-post-get-product
Browse files Browse the repository at this point in the history
[feat] 광고 상품 post,get API 구현
  • Loading branch information
lsm-del authored Mar 4, 2024
2 parents 41477d0 + 398d27c commit 9e6f202
Show file tree
Hide file tree
Showing 16 changed files with 378 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.backend.soullive_a.controller;

import com.backend.soullive_a.dto.request.CreateProductRequest;
import com.backend.soullive_a.dto.response.ProductResponse;
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.*;

import java.util.List;

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

private final ProductService productService;

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

@GetMapping()
public BaseResponse<List<ProductResponse>> getAllProduct() {
return BaseResponse.<List<ProductResponse>>builder()
.isSuccess(true)
.code(200)
.message("모든 광고 상품을 불러왔습니다.")
.data(productService.getAllProduct())
.build();
}

@GetMapping("/{id}")
public BaseResponse<ProductResponse> getProduct(@PathVariable Long id) {
return BaseResponse.<ProductResponse>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,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,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 ProductResponse(
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 ProductResponse 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 ProductResponse.builder()
.productId(product.getId())
.company(product.getCompany())
.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,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);
}
14 changes: 14 additions & 0 deletions src/main/java/com/backend/soullive_a/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.backend.soullive_a.service;

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

import java.util.List;

public interface ProductService {
ProductResponse createProduct(CreateProductRequest request);

ProductResponse getProduct(Long id);

List<ProductResponse> getAllProduct();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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.ProductResponse;
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
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 ProductResponse createProduct(CreateProductRequest request) {
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 ProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges);
}

@Override
@Transactional(readOnly = true)
public ProductResponse 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 ProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges);
}

@Override
@Transactional(readOnly = true)
public List<ProductResponse> getAllProduct() {
List<Product> products = productRepository.findAll();
List<ProductResponse> productResponses = new ArrayList<>();
for (Product product : products) {
List<BrandImage> brandImages = brandImageRepository.findAllByProductId(product.getId());
List<ProductImage> productImages = productImageRepository.findAllByProductId(product.getId());
List<Gender> genders = genderRepository.findAllByProductId(product.getId());
List<Age> ages = ageRepository.findAllByProductId(product.getId());
List<Range> ranges = rangeRepository.findAllByProductId(product.getId());
productResponses.add(ProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges));
}
return productResponses;
}
}

0 comments on commit 9e6f202

Please sign in to comment.