Skip to content

Commit b5459d8

Browse files
authored
Merge pull request #82 from Move-Log/develop
Merge develop to main
2 parents 7a8c1bd + fcef900 commit b5459d8

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/main/java/com/movelog/domain/record/application/KeywordService.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.movelog.domain.record.domain.Record;
55
import com.movelog.domain.record.domain.repository.RecordRepository;
66
import com.movelog.domain.record.dto.response.MyKeywordStatsRes;
7+
import com.movelog.domain.record.dto.response.RecommendKeywordInStatsRes;
78
import com.movelog.domain.record.dto.response.SearchKeywordInStatsRes;
89
import com.movelog.domain.record.exception.KeywordNotFoundException;
910
import com.movelog.domain.record.domain.repository.KeywordRepository;
@@ -14,6 +15,7 @@
1415
import com.movelog.global.config.security.token.UserPrincipal;
1516
import lombok.RequiredArgsConstructor;
1617
import lombok.extern.slf4j.Slf4j;
18+
import org.springframework.data.jpa.repository.Query;
1719
import org.springframework.stereotype.Service;
1820
import org.springframework.transaction.annotation.Transactional;
1921

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

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

37-
User user = validUserById(userPrincipal);
39+
validUserById(userPrincipal);
3840

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

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

5865
return MyKeywordStatsRes.builder()
5966
.noun(keyword.getKeyword())
@@ -122,6 +129,18 @@ private double roundToTwoDecimal(double value) {
122129
return Math.round(value * 100) / 100.0;
123130
}
124131

132+
public List<RecommendKeywordInStatsRes> getRecommendKeywords(UserPrincipal userPrincipal) {
133+
User user = validUserById(userPrincipal);
134+
List<Keyword> keywords = keywordRepository.findTop5ByUserOrderByCreatedAtDesc(user);
135+
136+
return keywords.stream()
137+
.map(keyword -> RecommendKeywordInStatsRes.builder()
138+
.keywordId(keyword.getKeywordId())
139+
.noun(keyword.getKeyword())
140+
.build())
141+
.toList();
142+
}
143+
125144
private User validUserById(UserPrincipal userPrincipal) {
126145
Optional<User> userOptional = userService.findById(userPrincipal.getId());
127146
// Optional<User> userOptional = userRepository.findById(5L);
@@ -135,4 +154,5 @@ private Keyword validKeywordById(Long keywordId) {
135154
return keywordOptional.get();
136155
}
137156

157+
138158
}

src/main/java/com/movelog/domain/record/domain/repository/KeywordRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.movelog.domain.record.domain.VerbType;
66
import com.movelog.domain.user.domain.User;
77
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
89
import org.springframework.stereotype.Repository;
910

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

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

25+
@Query("SELECT k FROM Keyword k WHERE LOWER(k.keyword) LIKE LOWER(CONCAT(:keyword, '%'))")
26+
List<Keyword> findAllKeywordStartingWith(String keyword);
27+
28+
2429
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.movelog.domain.record.dto.response;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
9+
@Builder
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
@Getter
13+
public class RecommendKeywordInStatsRes {
14+
@Schema( type = "int", example = "1", description="추천 키워드 ID")
15+
private Long keywordId;
16+
17+
@Schema( type = "String", example ="헬스", description="추천 키워드(명사)")
18+
private String noun;
19+
}

src/main/java/com/movelog/domain/record/presentation/StatsController.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.movelog.domain.record.application.KeywordService;
44
import com.movelog.domain.record.dto.response.MyKeywordStatsRes;
5+
import com.movelog.domain.record.dto.response.RecommendKeywordInStatsRes;
56
import com.movelog.domain.record.dto.response.SearchKeywordInStatsRes;
67
import com.movelog.global.config.security.token.UserPrincipal;
78
import com.movelog.global.payload.ErrorResponse;
@@ -64,6 +65,21 @@ public ResponseEntity<?> getMyKeywordStats(
6465
return ResponseEntity.ok(response);
6566
}
6667

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

6884

6985
}

0 commit comments

Comments
 (0)