Skip to content

Commit

Permalink
Merge pull request #76 from second-hand-team06/be-feat/#75-recommend-…
Browse files Browse the repository at this point in the history
…categories

[Be] feat/#75 recommend categories
  • Loading branch information
birdieHyun authored Jun 14, 2023
2 parents 49882c0 + 55cb32e commit 128ddcc
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
16 changes: 16 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,13 @@

import com.secondhand.category.dto.CategoriesDto;
import com.secondhand.category.dto.CategoryInterestsDto;
import com.secondhand.category.dto.PostTitleDto;
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 +19,7 @@ public class CategoryController {

private final CategoryService categoryService;
private final InterestRepository interestRepository;

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

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

return ResponseEntity
.ok()
.body(new CustomResponse<>(
"success",
200,
"관심 카테고리 목록 조회 성공",
categoryService.getRecommendedCategories(postTitleDto.getTitle())
));
}
}
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(String postTitle) {

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,11 @@
package com.secondhand.category.dto;

import lombok.Getter;

import java.util.List;

@Getter
public class RecommendedCategoriesDto {

private List<RecommendedCategoryDto> categories;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.secondhand.category.dto;

import com.querydsl.core.annotations.QueryProjection;

public class RecommendedCategoryDto {

private int id;
private String name;

@QueryProjection
public RecommendedCategoryDto(CategoryDto categoryDto) {
this.id = categoryDto.getId();
this.name = categoryDto.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
public interface CategoryRepositoryCustom {

CategoriesDto findAllCategories();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.secondhand.category.dto.CategoriesDto;
import com.secondhand.category.dto.QCategoryDto;
import com.secondhand.post.repository.CategoryRepositoryCustom;
import com.secondhand.category.dto.RecommendedCategoriesDto;

import javax.persistence.EntityManager;

Expand All @@ -29,4 +29,5 @@ public CategoriesDto findAllCategories() {
.from(category)
.fetch());
}

}

0 comments on commit 128ddcc

Please sign in to comment.