From 311a97f3bd61f0a65627a331035973592f112135 Mon Sep 17 00:00:00 2001 From: acceptor-gyu Date: Wed, 14 Jun 2023 12:36:03 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=83=81=ED=92=88=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=20=EC=8B=9C=20title=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EC=B6=94=EC=B2=9C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5(=EB=9E=9C=EB=8D=A4=EC=9C=BC=EB=A1=9C=20=EC=9E=84?= =?UTF-8?q?=EC=8B=9C=20=EA=B5=AC=ED=98=84)(#75)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../category/CategoryController.java | 19 +++++++++++++ .../secondhand/category/CategoryService.java | 27 +++++++++++++++++++ .../category/dto/CategoriesDto.java | 2 +- .../secondhand/category/dto/PostTitleDto.java | 9 +++++++ .../dto/RecommendedCategoriesDto.java | 15 +++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 be/src/main/java/com/secondhand/category/dto/PostTitleDto.java create mode 100644 be/src/main/java/com/secondhand/category/dto/RecommendedCategoriesDto.java diff --git a/be/src/main/java/com/secondhand/category/CategoryController.java b/be/src/main/java/com/secondhand/category/CategoryController.java index 360004c9e..0480e355a 100644 --- a/be/src/main/java/com/secondhand/category/CategoryController.java +++ b/be/src/main/java/com/secondhand/category/CategoryController.java @@ -2,11 +2,14 @@ import com.secondhand.category.dto.CategoriesDto; import com.secondhand.category.dto.CategoryInterestsDto; +import com.secondhand.category.dto.PostTitleDto; +import com.secondhand.category.dto.RecommendedCategoriesDto; import com.secondhand.post.repository.InterestRepository; import com.secondhand.util.CustomResponse; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -17,6 +20,7 @@ public class CategoryController { private final CategoryService categoryService; private final InterestRepository interestRepository; + @GetMapping public ResponseEntity> getCategoryList() { return ResponseEntity @@ -40,4 +44,19 @@ public ResponseEntity> getInterestCategoryL new CategoryInterestsDto(interestRepository.interestCategory())) ); } + + @GetMapping("/recommend") + public ResponseEntity> findRecommendedCategories(@RequestBody PostTitleDto postTitleDto) { + + + + return ResponseEntity + .ok() + .body(new CustomResponse<>( + "success", + 200, + "관심 카테고리 목록 조회 성공", + null) + ); + } } diff --git a/be/src/main/java/com/secondhand/category/CategoryService.java b/be/src/main/java/com/secondhand/category/CategoryService.java index db6e3a7ca..cf70d40ee 100644 --- a/be/src/main/java/com/secondhand/category/CategoryService.java +++ b/be/src/main/java/com/secondhand/category/CategoryService.java @@ -1,10 +1,13 @@ package com.secondhand.category; import com.secondhand.category.dto.CategoriesDto; +import com.secondhand.category.dto.CategoryDto; import com.secondhand.post.repository.CategoryRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.*; + @Service @RequiredArgsConstructor public class CategoryService { @@ -15,4 +18,28 @@ public CategoriesDto getCategoryList() { return categoryRepository.findAllCategories(); } + + public CategoriesDto getRecommendedCategories() { + + return recommendCategories(categoryRepository.findAllCategories().getCategories()); + } + + private CategoriesDto recommendCategories(List categories) { + + List recommendedCategories = new ArrayList<>(); + + Random random = new Random(); + Set numbers = new HashSet<>(); + + while (numbers.size() < 3) { + int randomNumber = random.nextInt(categories.size() + 1); + numbers.add(randomNumber); + } + + for (Integer number : numbers) { + recommendedCategories.add(categories.get(number)); + } + + return new CategoriesDto(recommendedCategories); + } } diff --git a/be/src/main/java/com/secondhand/category/dto/CategoriesDto.java b/be/src/main/java/com/secondhand/category/dto/CategoriesDto.java index 3c1146867..337406f43 100644 --- a/be/src/main/java/com/secondhand/category/dto/CategoriesDto.java +++ b/be/src/main/java/com/secondhand/category/dto/CategoriesDto.java @@ -3,7 +3,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; -import java.util.List; +import java.util.*; @Getter @AllArgsConstructor diff --git a/be/src/main/java/com/secondhand/category/dto/PostTitleDto.java b/be/src/main/java/com/secondhand/category/dto/PostTitleDto.java new file mode 100644 index 000000000..0c48f750f --- /dev/null +++ b/be/src/main/java/com/secondhand/category/dto/PostTitleDto.java @@ -0,0 +1,9 @@ +package com.secondhand.category.dto; + +import lombok.Getter; + +@Getter +public class PostTitleDto { + + private String title; +} diff --git a/be/src/main/java/com/secondhand/category/dto/RecommendedCategoriesDto.java b/be/src/main/java/com/secondhand/category/dto/RecommendedCategoriesDto.java new file mode 100644 index 000000000..1202b6e6e --- /dev/null +++ b/be/src/main/java/com/secondhand/category/dto/RecommendedCategoriesDto.java @@ -0,0 +1,15 @@ +package com.secondhand.category.dto; + +import lombok.Getter; + +@Getter +public class RecommendedCategoriesDto { + + private int id; + private String name; + + public RecommendedCategoriesDto(int id, String name) { + this.id = id; + this.name = name; + } +}