Skip to content

Commit

Permalink
feat: 상품 등록 시 title에 대한 카테고리 추천 기능(랜덤으로 임시 구현)(#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
acceptor-gyu committed Jun 14, 2023
1 parent 49882c0 commit 311a97f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
19 changes: 19 additions & 0 deletions be/src/main/java/com/secondhand/category/CategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +20,7 @@ public class CategoryController {

private final CategoryService categoryService;
private final InterestRepository interestRepository;

@GetMapping
public ResponseEntity<CustomResponse<CategoriesDto>> getCategoryList() {
return ResponseEntity
Expand All @@ -40,4 +44,19 @@ public ResponseEntity<CustomResponse<CategoryInterestsDto>> getInterestCategoryL
new CategoryInterestsDto(interestRepository.interestCategory()))
);
}

@GetMapping("/recommend")
public ResponseEntity<CustomResponse<RecommendedCategoriesDto>> findRecommendedCategories(@RequestBody PostTitleDto postTitleDto) {



return ResponseEntity
.ok()
.body(new CustomResponse<>(
"success",
200,
"관심 카테고리 목록 조회 성공",
null)
);
}
}
27 changes: 27 additions & 0 deletions be/src/main/java/com/secondhand/category/CategoryService.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,4 +18,28 @@ public CategoriesDto getCategoryList() {

return categoryRepository.findAllCategories();
}

public CategoriesDto getRecommendedCategories() {

return recommendCategories(categoryRepository.findAllCategories().getCategories());
}

private CategoriesDto recommendCategories(List<CategoryDto> categories) {

List<CategoryDto> recommendedCategories = new ArrayList<>();

Random random = new Random();
Set<Integer> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;
import java.util.*;

@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.secondhand.category.dto;

import lombok.Getter;

@Getter
public class PostTitleDto {

private String title;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 311a97f

Please sign in to comment.