Skip to content

Commit

Permalink
Merge pull request #31 from soulive-A/feature/#20-model
Browse files Browse the repository at this point in the history
최근 조회한 모델,모델 추천 API 추가 + swagger 문서 수정
  • Loading branch information
lsm-del authored Mar 7, 2024
2 parents 0e2c19d + 5248eb4 commit 6867793
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.backend.soullive_a.controller;

import com.backend.soullive_a.dto.request.ModelRequest;
import com.backend.soullive_a.dto.response.ModelRecommendResponse;
import com.backend.soullive_a.dto.response.ModelResponse;
import com.backend.soullive_a.dto.response.RecentModelResponse;
import com.backend.soullive_a.exception.base.BaseResponse;
import com.backend.soullive_a.service.ModelService;
import jakarta.validation.Valid;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "모델 API", description = "모델 조회,생성,최근 조회한 모델 API입니다.")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/model")
Expand All @@ -19,15 +23,38 @@ public class ModelController {
* @param modelName
* @return
*/
@Operation(summary = "모델 조회 API", description = "모델 기본 정보를 조회하는 API입니다.")
@GetMapping("")
public BaseResponse<ModelResponse> getModel(@RequestParam String modelName) {
public BaseResponse<ModelResponse> getModel(@RequestParam String modelName, @RequestParam Long productId) {
return BaseResponse.<ModelResponse>builder()
.isSuccess(true)
.code(200)
.message("모델 조회에 성공했습니다.")
.data(modelService.getModel(modelName))
.data(modelService.getModel(modelName,productId))
.build();

}

@Operation(summary = "최근 조회 모델 API", description = "최근 조회한 모델들을 조회하는 API입니다.")
@GetMapping("/recent/{productId}")
public BaseResponse<List<RecentModelResponse>> getRecentModel(@Parameter(name = "상품 id",description = "상품 기본키(필수)",
required = true) @PathVariable Long productId){
return BaseResponse.<List<RecentModelResponse>>builder()
.isSuccess(true)
.code(200)
.message("최근 조회한 모델 조회 성공")
.data(modelService.getRecentModel(productId))
.build();
}

@Operation(summary = "모델 추천 API", description = "모델을 추천 받는 API이다.")
@GetMapping("/recommendation")
public BaseResponse<List<ModelRecommendResponse>> getRecommendModel(){
return BaseResponse.<List<ModelRecommendResponse>>builder()
.isSuccess(true)
.code(200)
.message("모델 추천 성공")
.data(modelService.getRecommendModel())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

package com.backend.soullive_a.controller;

import com.backend.soullive_a.dto.request.ModelIntroduceRequest;

import com.backend.soullive_a.dto.response.ModelIntroductionResponse;
import com.backend.soullive_a.dto.response.ModelResponse;
import com.backend.soullive_a.exception.base.BaseResponse;
import com.backend.soullive_a.service.ModelIntroductionService;
import jakarta.validation.Valid;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "모델 소개 API", description = "모델 소개 API입니다.")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/model/introduction")
Expand All @@ -22,6 +22,7 @@ public class ModelIntroductionController {
* @param modelName
* @return
*/
@Operation(summary = "모델 소개 API", description = "모델 소개 정보를 조회하는 API입니다.")
@GetMapping("")
public BaseResponse<ModelIntroductionResponse> getModelIntroduction(@RequestParam String modelName) {
return BaseResponse.<ModelIntroductionResponse>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
import com.backend.soullive_a.dto.response.model.popularity.ModelPopularityResponse;
import com.backend.soullive_a.exception.base.BaseResponse;
import com.backend.soullive_a.service.ModelPopularityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "모델 화제성 API", description = "모델 화제성 API입니다.")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/model/popularity")
public class ModelPopularityController {

private final ModelPopularityService modelPopularityService;

@Operation(summary = "모델 화제성 API", description = "모델 화제성 정보를 조회하는 API입니다.")
@GetMapping("")
public BaseResponse<ModelPopularityResponse> getModelPopularity(
@RequestParam String name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.backend.soullive_a.dto.response.ProductResponse;
import com.backend.soullive_a.exception.base.BaseResponse;
import com.backend.soullive_a.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "광고 상품 API", description = "광고 상품 API입니다.")
@Slf4j
@RestController
@RequestMapping("api/product")
Expand All @@ -19,6 +21,7 @@ public class ProductController {

private final ProductService productService;

@Operation(summary = "광고 상품 생성 API", description = "광고 상품을 생성하는 API입니다.")
@PostMapping("")
public BaseResponse<ProductResponse> createProduct(
@RequestBody @Valid CreateProductRequest createProductRequest
Expand All @@ -30,7 +33,7 @@ public BaseResponse<ProductResponse> createProduct(
.data(productService.createProduct(createProductRequest))
.build();
}

@Operation(summary = "모든 광고 상품 조회 API", description = "모든 광고 상품을 조회 API입니다.")
@GetMapping()
public BaseResponse<List<ProductResponse>> getAllProduct() {
return BaseResponse.<List<ProductResponse>>builder()
Expand All @@ -40,7 +43,7 @@ public BaseResponse<List<ProductResponse>> getAllProduct() {
.data(productService.getAllProduct())
.build();
}

@Operation(summary = "특정 광고 상품 조회 API", description = "특정 광고 상품을 조회 API입니다.")
@GetMapping("/{id}")
public BaseResponse<ProductResponse> getProduct(@PathVariable Long id) {
return BaseResponse.<ProductResponse>builder()
Expand All @@ -50,7 +53,7 @@ public BaseResponse<ProductResponse> getProduct(@PathVariable Long id) {
.data(productService.getProduct(id))
.build();
}

@Operation(summary = "특정 광고 상품 삭제 API", description = "특정 광고 상품을 삭제 API입니다.")
@DeleteMapping("/{id}")
public BaseResponse<Void> deleteProduct(@PathVariable Long id) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.backend.soullive_a.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(name = "모델 추천 응답 model")
public record ModelRecommendResponse(
@Schema(name = "imageUrl", example = "https://soullive-bucket.s3.ap-northeast-2.amazonaws.com/heeae_profile.png", description = "모델 이미지 url입니다.")
String imageUrl,
@Schema(name = "name", example = "김희애", description = "모델 이름입니다.")
String name,
@Schema(name = "job", example = "텔런트/영화배우", description = "모델 직업(구분자)입니다.")
String job
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.backend.soullive_a.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "최근 조회한 모델 model 객체")
public record RecentModelResponse(
@Schema(name = "modelId", example = "1", description = "모델 기본키")
Long modelId,
@Schema(name = "imageUrl", example = "https://soullive-bucket.s3.ap-northeast-2.amazonaws.com/heeae_profile.png", description = "모델 이미지 url입니다.")
String imageUrl,
@Schema(name = "modelName", example = "한지민", description = "모델 이름입니다.")
String modelName,
@Schema(name = "aiRate", example = "3.0", description = "ai 추천 점수입니다.")
Float aiRate,
@Schema(name = "job", example = "배우", description = "직업에 대한 값입니다.")
String job
) {

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.dto.response.ModelRecommendResponse;
import com.backend.soullive_a.entity.model.Model;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;


public interface ModelRepository extends JpaRepository<Model, Long> {

public Optional<Model> findByModelName(String modelName);

@Query("select "
+ "new com.backend.soullive_a.dto.response.ModelRecommendResponse(m.imageUrl,m.modelName,m.job) "
+ "From Model m ")
List<ModelRecommendResponse> findAllRecommendModel();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package com.backend.soullive_a.repository;

import com.backend.soullive_a.dto.response.RecentModelResponse;
import com.backend.soullive_a.entity.model.ProductModel;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ProductModelRepository extends JpaRepository<ProductModel, Long> {

@Query("Select "
+ "new com.backend.soullive_a.dto.response.RecentModelResponse(m.id,m.imageUrl,m.modelName,m.aiRate,m.job) "
+ "From ProductModel p "
+ "Inner Join Model m On p.model.id = m.id "
+ "Where p.product.id = :productId "
+ "ORDER BY p.searchTime DESC")
List<RecentModelResponse> getRecentModels(@Param("productId") Long productId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.backend.soullive_a.service;

import com.backend.soullive_a.dto.request.ModelRequest;
import com.backend.soullive_a.dto.response.ModelRecommendResponse;
import com.backend.soullive_a.dto.response.ModelResponse;
import com.backend.soullive_a.dto.response.RecentModelResponse;
import java.util.List;

public interface ModelService {
public ModelResponse getModel(String modelName );
public ModelResponse getModel(String modelName, Long productId);

public List<RecentModelResponse> getRecentModel(Long productId);

public List<ModelRecommendResponse> getRecommendModel();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.backend.soullive_a.service.impl;

import com.backend.soullive_a.dto.request.ModelRequest;
import com.backend.soullive_a.dto.response.ModelRecommendResponse;
import com.backend.soullive_a.dto.response.ModelResponse;
import com.backend.soullive_a.dto.response.RecentModelResponse;
import com.backend.soullive_a.entity.Product;
import com.backend.soullive_a.entity.model.Model;
import com.backend.soullive_a.entity.model.ProductModel;
import com.backend.soullive_a.exception.custom.NotFoundUserException;
import com.backend.soullive_a.repository.ModelRepository;
import com.backend.soullive_a.repository.ProductModelRepository;
import com.backend.soullive_a.repository.ProductRepository;
import com.backend.soullive_a.service.ModelService;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,13 +22,19 @@
public class ModelServiceImpl implements ModelService {

private final ModelRepository modelRepository;
private final ProductRepository productRepository;
private final ProductModelRepository productModelRepository;

@Override
public ModelResponse getModel(String modelName) {
@Transactional
public ModelResponse getModel(String modelName,Long productId) {
System.out.println(modelName);
Model model = modelRepository.findByModelName(modelName)
.orElseThrow(() -> new NotFoundUserException());

//최근 조회 모델 데이터 업데이트
updateRecentViewModel(model,productId);

return ModelResponse.builder()
.modelId(model.getId())

Expand All @@ -39,4 +52,37 @@ public ModelResponse getModel(String modelName) {



/**
* 최근 조회한 모델들 조회
*/
@Override
@Transactional
public List<RecentModelResponse> getRecentModel(Long productId) {
return productModelRepository.getRecentModels(productId);
}

@Override
@Transactional
public List<ModelRecommendResponse> getRecommendModel(){

return modelRepository.findAllRecommendModel();
}
/**
* 광고 상품 엔티티 생성
* @param model
* @param productId
*/
private void updateRecentViewModel(Model model, Long productId){

Product product = productRepository.findById(productId)
.orElseThrow(NotFoundUserException::new);

productModelRepository.save(
ProductModel.builder()
.model(model)
.product(product)
.searchTime(LocalDateTime.now())
.build()
);
}
}

0 comments on commit 6867793

Please sign in to comment.