Skip to content

Commit b30cd6c

Browse files
authored
[fix] 검색 기능 보완, 추천리스트 로직 보완, ddl-auto: update 문제점 개선 (#172)
* fix : create table bug * fix : recommend logix * fix : bug * fix : bugs of testCase, init data * fix : bug
1 parent 54a7588 commit b30cd6c

File tree

8 files changed

+51
-40
lines changed

8 files changed

+51
-40
lines changed

src/main/java/com/back/domain/cocktail/controller/CocktailRecommendController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CocktailRecommendController {
2121

2222
private final RecommendService recommendService;
2323

24-
// 상세페이지 추천 (DTO로 반환)
24+
// 상세페이지 3개 칵테일 추천 (DTO로 반환)
2525
@Operation(summary = "상세페이지 유사 칵테일 추천", description = "현재 칵테일과 유사한 칵테일 최대 3개를 반환합니다.")
2626
@GetMapping("/related")
2727
public RsData<List<CocktailRecommendResponseDto>> recommendRelated(@RequestParam Long cocktailId) {

src/main/java/com/back/domain/cocktail/entity/Cocktail.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class Cocktail {
4141

4242
private String ingredient;
4343

44+
@Column(length = 1000)
4445
private String recipe;
4546

4647
private String cocktailImgUrl;

src/main/java/com/back/domain/cocktail/repository/CocktailRepository.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ Page<Cocktail> searchWithFilters(@Param("keyword") String keyword,
3636
@Param("types") List<CocktailType> types,
3737
@Param("bases") List<AlcoholBaseType> bases,
3838
Pageable pageable);
39-
//유사칵테일 추천관련
40-
List<Cocktail> findByAlcoholStrengthAndIdNot(AlcoholStrength strength, Long excludeId);
4139

42-
//유사칵테일 추천관련
43-
List<Cocktail> findByCocktailTypeAndIdNot(CocktailType type, Long excludeId);
44-
45-
//유사칵테일 추천관련
46-
List<Cocktail> findByAlcoholBaseTypeAndIdNot(AlcoholBaseType baseType, Long excludeId);
40+
List<Cocktail> findByAlcoholStrengthAndAlcoholBaseTypeAndIdNot(
41+
AlcoholStrength alcoholStrength,
42+
AlcoholBaseType alcoholBaseType,
43+
Long id
44+
);
4745
}

src/main/java/com/back/domain/cocktail/service/CocktailService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public List<CocktailSearchResponseDto> searchAndFilter (CocktailSearchRequestDto
7878
int size = cocktailSearchRequestDto.getSize() != null && cocktailSearchRequestDto.getSize() > 0
7979
? cocktailSearchRequestDto.getSize() : DEFAULT_SIZE;
8080

81-
// searchWithFilters에서 조회한 결과값을 pageResult에 저장.
81+
// searchAndFilters에서 조회한 결과값을 pageResult에 저장.
8282
Pageable pageable = PageRequest.of(page, size);
8383

8484
// 빈 리스트(null 또는 [])는 null로 변환

src/main/java/com/back/domain/cocktail/service/RecommendService.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import lombok.RequiredArgsConstructor;
77
import org.springframework.stereotype.Service;
88

9-
import java.util.ArrayList;
10-
import java.util.LinkedHashSet;
119
import java.util.List;
12-
import java.util.Set;
1310

1411
@Service
1512
@RequiredArgsConstructor
@@ -21,24 +18,20 @@ public List<CocktailRecommendResponseDto> recommendRelatedCocktails(Long cocktai
2118
Cocktail current = cocktailRepository.findById(cocktailId)
2219
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 칵테일입니다."));
2320

24-
// 3가지 조건으로 유사 칵테일 조회
25-
List<Cocktail> byAlcoholStrength = cocktailRepository.findByAlcoholStrengthAndIdNot(current.getAlcoholStrength(), current.getId());
26-
List<Cocktail> byCocktailType = cocktailRepository.findByCocktailTypeAndIdNot(current.getCocktailType(), current.getId());
27-
List<Cocktail> byAlcoholBase = cocktailRepository.findByAlcoholBaseTypeAndIdNot(current.getAlcoholBaseType(), current.getId());
21+
// 알콜 강도와 베이스 타이이 같은 칵테일만 조회
22+
List<Cocktail> related = cocktailRepository
23+
.findByAlcoholStrengthAndAlcoholBaseTypeAndIdNot(
24+
current.getAlcoholStrength(),
25+
current.getAlcoholBaseType(),
26+
current.getId()
27+
);
2828

29-
// 합치고 중복 제거
30-
Set<Cocktail> combined = new LinkedHashSet<>();
31-
combined.addAll(byAlcoholStrength);
32-
combined.addAll(byCocktailType);
33-
combined.addAll(byAlcoholBase);
34-
35-
List<Cocktail> combinedList = new ArrayList<>(combined);
36-
if (combinedList.size() > maxSize) {
37-
combinedList = combinedList.subList(0, maxSize);
29+
if (related.size() > maxSize) {
30+
related = related.subList(0, maxSize);
3831
}
3932

4033
// DTO로 변환
41-
return combinedList.stream()
34+
return related.stream()
4235
.map(c -> new CocktailRecommendResponseDto(
4336
c.getId(),
4437
c.getCocktailNameKo(),

src/main/resources/application-dev.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
spring:
2-
sql:
3-
init:
4-
mode: always
5-
data-locations: classpath:data-h2.sql
62
# H2 Database 설정
73
datasource:
84
driver-class-name: org.h2.Driver
@@ -18,14 +14,21 @@ spring:
1814

1915
# JPA 설정
2016
jpa:
17+
defer-datasource-initialization: true
2118
database-platform: org.hibernate.dialect.H2Dialect
2219
hibernate:
23-
ddl-auto: update # 실행전 db_dev 파일 삭제 필요
20+
ddl-auto: create-drop # 개발용: 시작할 때 테이블 생성, 종료할 때 삭제 / db_dev 에러 방지용으로 update->create-drop 변경
2421
properties:
2522
hibernate:
2623
format_sql: true
2724
show_sql: true
2825

26+
sql:
27+
init:
28+
mode: always # 항상 실행
29+
encoding: UTF-8
30+
data-locations: classpath:data-h2.sql
31+
2932
cloud:
3033
aws:
3134
region:

src/main/resources/application-test.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
spring:
2-
sql:
3-
init:
4-
mode: always
5-
data-locations: classpath:data-h2.sql
62
datasource:
73
url: jdbc:h2:mem:db_test;MODE=MySQL
84
username: sa

src/test/java/com/back/domain/cocktail/controller/CocktailControllerTest.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
import com.back.domain.cocktail.enums.AlcoholStrength;
66
import com.back.domain.cocktail.enums.CocktailType;
77
import com.back.domain.cocktail.repository.CocktailRepository;
8-
import com.back.domain.cocktail.service.CocktailService;
8+
import com.back.domain.user.service.UserService;
9+
import com.back.global.rq.Rq;
10+
import jakarta.servlet.http.HttpServletRequest;
11+
import jakarta.servlet.http.HttpServletResponse;
912
import org.junit.jupiter.api.DisplayName;
1013
import org.junit.jupiter.api.Test;
14+
import org.mockito.Mockito;
1115
import org.springframework.beans.factory.annotation.Autowired;
1216
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1317
import org.springframework.boot.test.context.SpringBootTest;
18+
import org.springframework.boot.test.context.TestConfiguration;
19+
import org.springframework.context.annotation.Bean;
1420
import org.springframework.http.MediaType;
1521
import org.springframework.test.context.ActiveProfiles;
22+
import org.springframework.test.context.TestPropertySource;
1623
import org.springframework.test.web.servlet.MockMvc;
1724
import org.springframework.test.web.servlet.ResultActions;
1825
import org.springframework.transaction.annotation.Transactional;
@@ -26,6 +33,10 @@
2633

2734
@ActiveProfiles("test")
2835
@SpringBootTest
36+
@TestPropertySource(properties = {
37+
"custom.cookie.secure=false",
38+
"custom.cookie.same=Strict"
39+
})
2940
@AutoConfigureMockMvc(addFilters = false)
3041
@Transactional
3142
public class CocktailControllerTest {
@@ -35,10 +46,6 @@ public class CocktailControllerTest {
3546
@Autowired
3647
private CocktailRepository cocktailRepository;
3748

38-
@Autowired
39-
private CocktailService cocktailService;
40-
41-
4249
@Test
4350
@DisplayName("칵테일 단건 조회 - 로그인 없이 성공")
4451
void t1() throws Exception {
@@ -130,4 +137,17 @@ void t4() throws Exception {
130137
.andExpect(jsonPath("$.message").value("success"))
131138
.andExpect(jsonPath("$.data").isArray());
132139
}
140+
141+
@TestConfiguration
142+
static class TestConfig {
143+
@Bean
144+
public Rq rq() {
145+
HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
146+
HttpServletResponse resp = Mockito.mock(HttpServletResponse.class);
147+
UserService userService = Mockito.mock(UserService.class);
148+
149+
return new Rq(req, resp, userService);
150+
}
151+
}
152+
133153
}

0 commit comments

Comments
 (0)