Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class SecurityPath {
"/api/interests",
"/api/interests/me",
"/api/posts",
"/api/posts/*"
"/api/posts/*",
"/api/search/history",
"/api/search/history/*"
};

// hasRole("ADMIN")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.hyetaekon.hyetaekon.common.config.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableRedisRepositories(basePackages = "com.hyetaekon.hyetaekon.publicservice.repository.redis")
public class SearchHistoryConfig {

@Bean
public RedisTemplate<String, Object> searchHistoryRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
// ํ‚ค์™€ ๊ฐ’์˜ ์ง๋ ฌํ™” ๋ฐฉ์‹ ์ง€์ •
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));

return redisTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,4 @@ public ServiceSearchCriteriaDto withUserInfo(
.build();
}

// ๊ฒ€์ƒ‰ ์กฐ๊ฑด ์œ ๋ฌด ํ™•์ธ
public boolean hasSearchCriteria() {
return StringUtils.hasText(searchTerm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ private String buildUserMustClause(ServiceSearchCriteriaDto criteria) {
{
compound: {
should: [
{exists: {path: "%s", exists: false}},
{equals: {path: "%s", value: "Y"}}
{
compound: {
mustNot: [{exists: {path: "%s"}}]
}
},
{
equals: {path: "%s", value: "Y"}
}
]
}
}""".formatted(genderField, genderField));
}
""".formatted(genderField, genderField));

}

// ๋‚˜์ด ํ•„์ˆ˜ ์กฐ๊ฑด
Expand All @@ -176,21 +184,36 @@ private String buildUserMustClause(ServiceSearchCriteriaDto criteria) {
{
compound: {
should: [
{exists: {path: "targetAgeStart", exists: false}},
{range: {path: "targetAgeStart", lte: %d}}
{
compound: {
mustNot: [{exists: {path: "targetAgeStart"}}]
}
},
{
range: {path: "targetAgeStart", lte: %d}
}
]
}
}""".formatted(age));
}
""".formatted(age));

mustClauses.add("""
mustClauses.add("""
{
compound: {
should: [
{exists: {path: "targetAgeEnd", exists: false}},
{range: {path: "targetAgeEnd", gte: %d}}
{
compound: {
mustNot: [{exists: {path: "targetAgeEnd"}}]
}
},
{
range: {path: "targetAgeEnd", gte: %d}
}
]
}
}""".formatted(age));
}
""".formatted(age));

}

// must ์กฐ๊ฑด์ด ์—†์œผ๋ฉด ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.hyetaekon.hyetaekon.publicservice.service.mongodb;

import com.hyetaekon.hyetaekon.UserInterest.entity.UserInterest;
import com.hyetaekon.hyetaekon.UserInterest.repository.UserInterestRepository;
import com.hyetaekon.hyetaekon.searchHistory.Service.SearchHistoryService;
import com.hyetaekon.hyetaekon.userInterest.entity.UserInterest;
import com.hyetaekon.hyetaekon.userInterest.repository.UserInterestRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
Expand Down Expand Up @@ -34,11 +35,12 @@ public class ServiceSearchService {
private final UserRepository userRepository;
private final UserInterestRepository userInterestRepository;
private final IncomeEstimationHandler incomeEstimationHandler;
private final SearchHistoryService searchHistoryService;

// ๊ธฐ๋ณธ ๊ฒ€์ƒ‰ (๋น„๋กœ๊ทธ์ธ)
public Page<PublicServiceListResponseDto> searchServices(ServiceSearchCriteriaDto criteria) {
// ๊ฒ€์ƒ‰ ์กฐ๊ฑด์ด ์—†๋Š” ๊ฒฝ์šฐ ๋นˆ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
if (!criteria.hasSearchCriteria()) {
if (!StringUtils.hasText(criteria.getSearchTerm())) {
return Page.empty(criteria.getPageable());
}

Expand All @@ -54,8 +56,10 @@ public Page<PublicServiceListResponseDto> searchPersonalizedServices(
ServiceSearchCriteriaDto criteria, Long userId) {

// ๊ฒ€์ƒ‰ ์กฐ๊ฑด์ด ์—†๋Š” ๊ฒฝ์šฐ ๋นˆ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
if (!criteria.hasSearchCriteria()) {
if (!StringUtils.hasText(criteria.getSearchTerm())) {
return Page.empty(criteria.getPageable());
} else if(StringUtils.hasText(criteria.getSearchTerm())) { // ๊ฒ€์ƒ‰์–ด๊ฐ€ ์œ ํšจํ•˜๋ฉด ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์ €์žฅ
searchHistoryService.saveSearchHistory(userId, criteria.getSearchTerm());
}

// ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hyetaekon.hyetaekon.searchHistory.Dto;

import com.hyetaekon.hyetaekon.searchHistory.entity.SearchHistory;
import lombok.*;

import java.time.format.DateTimeFormatter;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SearchHistoryDto {
private String id;
private String searchTerm;
private String createdAt;

// Entity -> DTO ๋ณ€ํ™˜
public static SearchHistoryDto from(SearchHistory entity) {
return SearchHistoryDto.builder()
.id(entity.getId())
.searchTerm(entity.getSearchTerm())
.createdAt(entity.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hyetaekon.hyetaekon.searchHistory.Repository;

import com.hyetaekon.hyetaekon.searchHistory.entity.SearchHistory;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SearchHistoryRepository extends CrudRepository<SearchHistory, String> {
// ํŠน์ • ์‚ฌ์šฉ์ž์˜ ๋ชจ๋“  ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์กฐํšŒ
List<SearchHistory> findByUserId(Long userId);

// ํŠน์ • ์‚ฌ์šฉ์ž์˜ ํŠน์ • ๊ฒ€์ƒ‰์–ด ๊ธฐ๋ก ์‚ญ์ œ
void deleteByUserIdAndId(Long userId, String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.hyetaekon.hyetaekon.searchHistory.Service;

import com.hyetaekon.hyetaekon.searchHistory.Dto.SearchHistoryDto;
import com.hyetaekon.hyetaekon.searchHistory.Repository.SearchHistoryRepository;
import com.hyetaekon.hyetaekon.searchHistory.entity.SearchHistory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Comparator;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class SearchHistoryService {
private final SearchHistoryRepository searchHistoryRepository;

private static final int MAX_DISPLAY_COUNT = 6;

/**
* ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์ €์žฅ
*/
@Transactional
public void saveSearchHistory(Long userId, String searchTerm) {
// ๊ฒ€์ƒ‰์–ด๊ฐ€ ์—†๊ฑฐ๋‚˜ ๋นˆ ๋ฌธ์ž์—ด์ด๋ฉด ์ €์žฅํ•˜์ง€ ์•Š์Œ
if (!StringUtils.hasText(searchTerm) || userId == 0L) {
return;
}

// ๊ฒ€์ƒ‰์–ด ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ์œ„ํ•œ ๊ธฐ์กด ๊ฒ€์ƒ‰ ๊ธฐ๋ก ํ™•์ธ
List<SearchHistory> existingHistories = searchHistoryRepository.findByUserId(userId);

// ์ด๋ฏธ ๋™์ผํ•œ ๊ฒ€์ƒ‰์–ด๊ฐ€ ์žˆ์œผ๋ฉด ์‚ญ์ œ
existingHistories.stream()
.filter(history -> history.getSearchTerm().equals(searchTerm))
.forEach(history -> searchHistoryRepository.deleteById(history.getId()));

// ์ƒˆ๋กœ์šด ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์ €์žฅ
SearchHistory newHistory = SearchHistory.of(userId, searchTerm);
searchHistoryRepository.save(newHistory);
log.debug("์‚ฌ์šฉ์ž {} ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์ €์žฅ: {}", userId, searchTerm);
}

/**
* ์‚ฌ์šฉ์ž์˜ ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์กฐํšŒ (์ตœ์‹  6๊ฐœ)
*/
@Transactional(readOnly = true)
public List<SearchHistoryDto> getUserSearchHistories(Long userId) {
List<SearchHistory> histories = searchHistoryRepository.findByUserId(userId);

// ์ตœ์‹  ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ตœ๋Œ€ 6๊ฐœ ๋ฐ˜ํ™˜
return histories.stream()
.sorted(Comparator.comparing(SearchHistory::getCreatedAt).reversed())
.limit(MAX_DISPLAY_COUNT)
.map(SearchHistoryDto::from)
.collect(Collectors.toList());
}

/**
* ๊ฐœ๋ณ„ ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์‚ญ์ œ
*/
@Transactional
public void deleteSearchHistory(Long userId, String historyId) {
if (!StringUtils.hasText(historyId)) {
return;
}
// ์‚ฌ์šฉ์ž์˜ ๊ฒ€์ƒ‰ ๊ธฐ๋ก๋งŒ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž ID๋„ ํ•จ๊ป˜ ํ™•์ธ
searchHistoryRepository.deleteByUserIdAndId(userId, historyId);
log.debug("์‚ฌ์šฉ์ž {} ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์‚ญ์ œ: {}", userId, historyId);
}

/**
* ์‚ฌ์šฉ์ž์˜ ๋ชจ๋“  ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์‚ญ์ œ
*/
@Transactional
public void deleteAllSearchHistories(Long userId) {
List<SearchHistory> histories = searchHistoryRepository.findByUserId(userId);
searchHistoryRepository.deleteAll(histories);
log.debug("์‚ฌ์šฉ์ž {} ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์ „์ฒด ์‚ญ์ œ", userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.hyetaekon.hyetaekon.searchHistory.controller;

import com.hyetaekon.hyetaekon.common.jwt.CustomUserDetails;
import com.hyetaekon.hyetaekon.searchHistory.Dto.SearchHistoryDto;
import com.hyetaekon.hyetaekon.searchHistory.Service.SearchHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/search/history")
@RequiredArgsConstructor
public class SearchHistoryController {

private final SearchHistoryService searchHistoryService;

/**
* ํ˜„์žฌ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž์˜ ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์กฐํšŒ (์ตœ์‹  6๊ฐœ)
*/
@GetMapping
public ResponseEntity<List<SearchHistoryDto>> getSearchHistories(
@AuthenticationPrincipal CustomUserDetails userDetails) {
return ResponseEntity.ok(searchHistoryService.getUserSearchHistories(userDetails.getId()));
}

/**
* ํŠน์ • ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์‚ญ์ œ
*/
@DeleteMapping("/{historyId}")
public ResponseEntity<Void> deleteSearchHistory(@PathVariable String historyId
, @AuthenticationPrincipal CustomUserDetails userDetails) {
searchHistoryService.deleteSearchHistory(userDetails.getId(), historyId);
return ResponseEntity.ok().build();
}

/**
* ๋ชจ๋“  ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์‚ญ์ œ
*/
@DeleteMapping
public ResponseEntity<Void> deleteAllSearchHistories(
@AuthenticationPrincipal CustomUserDetails userDetails
) {
searchHistoryService.deleteAllSearchHistories(userDetails.getId());
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.hyetaekon.hyetaekon.searchHistory.entity;

import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;

import java.io.Serializable;
import java.time.LocalDateTime;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RedisHash(value = "searchHistory", timeToLive = 2592000) // 30์ผ ์œ ์ง€
public class SearchHistory implements Serializable {
@Id
private String id; // userId:timestamp ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ

@Indexed // ์ธ๋ฑ์‹ฑ์œผ๋กœ ํŠน์ • ์‚ฌ์šฉ์ž์˜ ๊ฒ€์ƒ‰ ๊ธฐ๋ก ์กฐํšŒ ๊ฐ€๋Šฅ
private Long userId;

private String searchTerm;
private LocalDateTime createdAt;

// ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ
public static SearchHistory of(Long userId, String searchTerm) {
String id = userId + ":" + System.currentTimeMillis();
return SearchHistory.builder()
.id(id)
.userId(userId)
.searchTerm(searchTerm)
.createdAt(LocalDateTime.now())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hyetaekon.hyetaekon.user.entity;

import com.hyetaekon.hyetaekon.UserInterest.entity.UserInterest;
import com.hyetaekon.hyetaekon.userInterest.entity.UserInterest;
import com.hyetaekon.hyetaekon.bookmark.entity.Bookmark;
import com.hyetaekon.hyetaekon.recommend.entity.Recommend;
import jakarta.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.hyetaekon.hyetaekon.UserInterest.controller;
package com.hyetaekon.hyetaekon.userInterest.controller;

import com.hyetaekon.hyetaekon.UserInterest.dto.CategorizedInterestsResponseDto;
import com.hyetaekon.hyetaekon.UserInterest.dto.CategorizedInterestsWithSelectionDto;
import com.hyetaekon.hyetaekon.UserInterest.dto.InterestSelectionRequestDto;
import com.hyetaekon.hyetaekon.UserInterest.entity.UserInterestEnum;
import com.hyetaekon.hyetaekon.UserInterest.service.UserInterestService;
import com.hyetaekon.hyetaekon.userInterest.dto.CategorizedInterestsResponseDto;
import com.hyetaekon.hyetaekon.userInterest.dto.CategorizedInterestsWithSelectionDto;
import com.hyetaekon.hyetaekon.userInterest.dto.InterestSelectionRequestDto;
import com.hyetaekon.hyetaekon.userInterest.entity.UserInterestEnum;
import com.hyetaekon.hyetaekon.userInterest.service.UserInterestService;
import com.hyetaekon.hyetaekon.common.jwt.CustomUserDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.hyetaekon.hyetaekon.UserInterest.dto;
package com.hyetaekon.hyetaekon.userInterest.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;
Expand Down
Loading