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 @@ -4,6 +4,7 @@
import com.movelog.domain.record.domain.Record;
import com.movelog.domain.record.domain.repository.RecordRepository;
import com.movelog.domain.record.dto.response.MyKeywordStatsRes;
import com.movelog.domain.record.dto.response.RecommendKeywordInStatsRes;
import com.movelog.domain.record.dto.response.SearchKeywordInStatsRes;
import com.movelog.domain.record.exception.KeywordNotFoundException;
import com.movelog.domain.record.domain.repository.KeywordRepository;
Expand All @@ -14,6 +15,7 @@
import com.movelog.global.config.security.token.UserPrincipal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -34,10 +36,11 @@ public class KeywordService {

public List<SearchKeywordInStatsRes> searchKeywordInStats(UserPrincipal userPrincipal, String keyword) {

User user = validUserById(userPrincipal);
validUserById(userPrincipal);

// 검색어를 포함한 키워드 리스트 조회
List<Keyword> keywords = keywordRepository.findAllByUserAndKeywordContaining(user, keyword);
List<Keyword> keywords = keywordRepository.findAllKeywordStartingWith(keyword);
log.info("Searching for keywords starting with: {}", keyword);

// 기록이 많은 순서대로 정렬
keywords = sortKeywordByRecordCount(keywords);
Expand All @@ -54,6 +57,10 @@ public List<SearchKeywordInStatsRes> searchKeywordInStats(UserPrincipal userPrin
public MyKeywordStatsRes getMyKeywordStatsRes(UserPrincipal userPrincipal, Long keywordId) {
validUserById(userPrincipal);
Keyword keyword = validKeywordById(keywordId);
// 사용자가 기록한 키워드가 아닐 시, 빈 배열 반환
if (!keyword.getUser().getId().equals(userPrincipal.getId())) {
return MyKeywordStatsRes.builder().build();
}

return MyKeywordStatsRes.builder()
.noun(keyword.getKeyword())
Expand Down Expand Up @@ -122,6 +129,18 @@ private double roundToTwoDecimal(double value) {
return Math.round(value * 100) / 100.0;
}

public List<RecommendKeywordInStatsRes> getRecommendKeywords(UserPrincipal userPrincipal) {
User user = validUserById(userPrincipal);
List<Keyword> keywords = keywordRepository.findTop5ByUserOrderByCreatedAtDesc(user);

return keywords.stream()
.map(keyword -> RecommendKeywordInStatsRes.builder()
.keywordId(keyword.getKeywordId())
.noun(keyword.getKeyword())
.build())
.toList();
}

private User validUserById(UserPrincipal userPrincipal) {
Optional<User> userOptional = userService.findById(userPrincipal.getId());
// Optional<User> userOptional = userRepository.findById(5L);
Expand All @@ -135,4 +154,5 @@ private Keyword validKeywordById(Long keywordId) {
return keywordOptional.get();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.movelog.domain.record.domain.VerbType;
import com.movelog.domain.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -21,4 +22,8 @@ public interface KeywordRepository extends JpaRepository<Keyword,Long> {

List<Keyword> findAllByUserAndKeywordContaining(User user, String keyword);

@Query("SELECT k FROM Keyword k WHERE LOWER(k.keyword) LIKE LOWER(CONCAT(:keyword, '%'))")
List<Keyword> findAllKeywordStartingWith(String keyword);


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

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class RecommendKeywordInStatsRes {
@Schema( type = "int", example = "1", description="추천 키워드 ID")
private Long keywordId;

@Schema( type = "String", example ="헬스", description="추천 키워드(명사)")
private String noun;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.movelog.domain.record.application.KeywordService;
import com.movelog.domain.record.dto.response.MyKeywordStatsRes;
import com.movelog.domain.record.dto.response.RecommendKeywordInStatsRes;
import com.movelog.domain.record.dto.response.SearchKeywordInStatsRes;
import com.movelog.global.config.security.token.UserPrincipal;
import com.movelog.global.payload.ErrorResponse;
Expand Down Expand Up @@ -64,6 +65,21 @@ public ResponseEntity<?> getMyKeywordStats(
return ResponseEntity.ok(response);
}

@Operation(summary = "통계 추천 단어 조회 API", description = "사용자가 최근 기록한 단어 목록(최대 5개)을 조회하는 API입니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "통계 추천 단어 조회 성공",
content = @Content(mediaType = "application/json",
schema = @Schema(type = "array", implementation = RecommendKeywordInStatsRes.class))),
@ApiResponse(responseCode = "400", description = "통계 추천 단어 조회 실패",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/word/recommend")
public ResponseEntity<?> getRecommendKeywords(
@Parameter(description = "Access Token을 입력해주세요.", required = true) @AuthenticationPrincipal UserPrincipal userPrincipal
) {
List<RecommendKeywordInStatsRes> response = keywordService.getRecommendKeywords(userPrincipal);
return ResponseEntity.ok(response);
}


}