Skip to content

Commit 874ee48

Browse files
authored
[feat] 나만의 bar: 전체 삭제 기능 추가 #291 (#295)
* Revert "chore: initData용 이미지 추가" This reverts commit ef30eef. * . * chore: 수정사항 없음 * feat: MyHistoryPostItemDto에 상세 정보 필드 추가 및 매핑 로직 업데이트 * refactor: 게시글 좋아요 알림 메시지에 게시글 제목 추가 * chore: 수정사항 없음 * feat: MyBarRepository에 모든 킵 기록 일괄 소프트 삭제 기능 추가 * feat: MyBarService에 사용자 전체 킵 목록 초기화 기능 구현 * feat: 킵 취소 점수 일괄 회수를 위한 revokeForKeep 오버로드 추가 * feat: MyBarController에 모든 킵 기록을 초기화하는 DELETE /me/bar 엔드포인트 추가 * test: MyBarControllerTest에 clearAllMyBar 테스트 케이스 추가
1 parent fffbd3d commit 874ee48

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

src/main/java/com/back/domain/mybar/controller/MyBarController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,15 @@ public RsData<Void> unkeep(
7777
myBarService.unkeep(userId, cocktailId);
7878
return RsData.of(200, "deleted");
7979
}
80+
81+
@DeleteMapping
82+
@Operation(summary = "내 바 전체 삭제", description = "내 바에 담긴 모든 칵테일을 소프트 삭제합니다")
83+
public RsData<Void> clearAll(
84+
@AuthenticationPrincipal SecurityUser principal
85+
) {
86+
Long userId = principal.getId();
87+
myBarService.clearAll(userId);
88+
return RsData.of(200, "cleared");
89+
}
8090
}
8191

src/main/java/com/back/domain/mybar/repository/MyBarRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public interface MyBarRepository extends JpaRepository<MyBar, Long> {
3838
/** 복원/재킵을 위해 status 무시하고 한 건 찾기 (없으면 Optional.empty) */
3939
Optional<MyBar> findByUser_IdAndCocktail_Id(Long userId, Long cocktailId);
4040

41+
@Modifying(clearAutomatically = true, flushAutomatically = true)
42+
@Query("""
43+
update MyBar m
44+
set m.status = 'DELETED', m.deletedAt = CURRENT_TIMESTAMP
45+
where m.user.id = :userId
46+
and m.status = 'ACTIVE'
47+
""")
48+
int softDeleteAllByUser(Long userId);
49+
4150
@Modifying(clearAutomatically = true, flushAutomatically = true)
4251
@Query("""
4352
update MyBar m

src/main/java/com/back/domain/mybar/service/MyBarService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,13 @@ public void unkeep(Long userId, Long cocktailId) {
123123
abvScoreService.revokeForKeep(userId);
124124
}
125125
}
126+
127+
@Transactional
128+
public void clearAll(Long userId) {
129+
int changed = myBarRepository.softDeleteAllByUser(userId);
130+
if (changed > 0) {
131+
abvScoreService.revokeForKeep(userId, changed);
132+
}
133+
}
126134
}
127135

src/main/java/com/back/domain/user/service/AbvScoreService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public void revokeForKeep(Long userId) {
5858
addScore(userId, -KEEP_SCORE);
5959
}
6060

61+
@Transactional
62+
public void revokeForKeep(Long userId, int count) {
63+
if (count <= 0) {
64+
return;
65+
}
66+
addScore(userId, -KEEP_SCORE * count);
67+
}
68+
6169
private void addScore(Long userId, double delta) {
6270
User user = userRepository.findById(userId)
6371
.orElseThrow(() -> new ServiceException(404, "사용자를 찾을 수 없습니다."));

src/test/java/com/back/domain/mybar/controller/MyBarControllerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,22 @@ void unkeepCocktail() throws Exception {
220220

221221
verify(myBarService).unkeep(principal.getId(), cocktailId);
222222
}
223+
224+
@Test
225+
@DisplayName("Clear entire my bar")
226+
void clearAllMyBar() throws Exception {
227+
SecurityUser principal = createPrincipal(21L);
228+
229+
willDoNothing().given(myBarService).clearAll(principal.getId());
230+
231+
mockMvc.perform(delete("/me/bar")
232+
.with(withPrincipal(principal))
233+
.accept(MediaType.APPLICATION_JSON))
234+
.andExpect(status().isOk())
235+
.andExpect(jsonPath("$.code").value(200))
236+
.andExpect(jsonPath("$.message").value("cleared"))
237+
.andExpect(jsonPath("$.data").doesNotExist());
238+
239+
verify(myBarService).clearAll(principal.getId());
240+
}
223241
}

0 commit comments

Comments
 (0)