Skip to content

Commit dd4a1c8

Browse files
committed
버그 수정 및 코드 리팩토링
북마크 정보 수정 시 태그 수정 추가 카테고리 순서 커스텀 기능 추가 그룹 탈퇴 로직 수정(ADMIN 기준 -> 소유주 기준)
1 parent e235dde commit dd4a1c8

File tree

13 files changed

+121
-17
lines changed

13 files changed

+121
-17
lines changed

src/main/java/com/sonkim/bookmarking/auth/service/AuthService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public User createAccount(RegisterRequestDto dto) {
6868
// 기본 개인 그룹 생성
6969
Team personalTeam = Team.builder()
7070
.name(dto.getNickname() + "님의 개인 공간")
71-
.user(newUser)
71+
.owner(newUser)
7272
.build();
7373
teamService.saveTeam(personalTeam);
7474

src/main/java/com/sonkim/bookmarking/domain/bookmark/repository/BookmarkTagRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.List;
8+
79
@Repository
810
public interface BookmarkTagRepository extends JpaRepository<BookmarkTag, Long>, BookmarkTagRepositoryCustom {
11+
List<BookmarkTag> findAllByBookmarkId(Long bookmarkId);
912
}

src/main/java/com/sonkim/bookmarking/domain/bookmark/service/BookmarkService.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,36 @@ public void updateBookmark(Long userId, Long bookmarkId, BookmarkRequestDto dto)
201201
}
202202
}
203203

204+
if (dto.getTagNames() != null) {
205+
// 현재 북마크에 연결된 태그들 조회
206+
List<Tag> oldTags = bookmarkTagRepository.findAllByBookmarkId(bookmarkId).stream()
207+
.map(BookmarkTag::getTag)
208+
.toList();
209+
210+
// 요청으로 들어온 Tag 정보 조회
211+
List<Tag> newTags = tagService.findOrCreateTags(userId, bookmark.getTeam().getId(), dto.getTagNames());
212+
213+
// 삭제할 태그를 찾아서 BookmarkTag 연결 끊기
214+
List<BookmarkTag> tagsToRemove = bookmark.getBookmarkTags().stream()
215+
.filter(bookmarkTag -> !newTags.contains(bookmarkTag.getTag()))
216+
.toList();
217+
bookmark.getBookmarkTags().removeAll(tagsToRemove);
218+
bookmarkTagRepository.deleteAll(tagsToRemove);
219+
220+
// 추가할 태그를 찾아서 BookmarkTag 생성하여 연결
221+
List<Tag> tagsToAdd = newTags.stream()
222+
.filter(tag -> !oldTags.contains(tag))
223+
.toList();
224+
225+
for (Tag tag : tagsToAdd) {
226+
BookmarkTag bookmarkTag = BookmarkTag.builder()
227+
.bookmark(bookmark)
228+
.tag(tag)
229+
.build();
230+
bookmarkTagRepository.save(bookmarkTag);
231+
}
232+
}
233+
204234
bookmark.update(dto);
205235
}
206236

src/main/java/com/sonkim/bookmarking/domain/category/controller/CategoryController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ public ResponseEntity<List<CategoryDto.CategoryResponseDto>> deleteCategory(@Aut
7474
List<CategoryDto.CategoryResponseDto> updatedCategories = categoryService.deleteCategory(userDetails.getId(), categoryId);
7575
return ResponseEntity.ok(updatedCategories);
7676
}
77+
78+
@Operation(summary = "카테고리 순서 일괄 수정", description = "특정 그룹 내 카테고리들의 표시 순서를 한 번에 업데이트합니다. EDITOR 이상의 권한이 필요합니다.")
79+
@ApiResponse(responseCode = "200", description = "순서 업데이트 성공")
80+
@PatchMapping("/groups/{groupId}/categories/order")
81+
public ResponseEntity<Void> updateCategoryPositions(
82+
@AuthenticationPrincipal UserDetailsImpl userDetails,
83+
@PathVariable Long groupId,
84+
@RequestBody List<CategoryDto.UpdatePositionRequestDto> requests
85+
) {
86+
categoryService.updateCategoryPositions(userDetails.getId(), groupId, requests);
87+
return ResponseEntity.ok().build();
88+
}
7789
}

src/main/java/com/sonkim/bookmarking/domain/category/dto/CategoryDto.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ public static class CategoryResponseDto {
2323
private String name;
2424
private Long bookmarkCount;
2525
}
26+
27+
// 카테고리 순서 업데이트 요청 DTO
28+
@Data
29+
public static class UpdatePositionRequestDto {
30+
private Long categoryId;
31+
private Integer position;
32+
}
2633
}

src/main/java/com/sonkim/bookmarking/domain/category/entity/Category.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public class Category {
2525
@Column(nullable = false, length = 20)
2626
private String name;
2727

28+
// 카테고리 순서
29+
@Column(nullable = false)
30+
private Integer position;
31+
2832
public void update(CategoryDto.CategoryRequestDto request) {
2933
if (request.getName() != null) this.name = request.getName();}
34+
35+
public void updatePosition(Integer position) {
36+
this.position = position;
37+
}
3038
}

src/main/java/com/sonkim/bookmarking/domain/category/repository/CategoryRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
@Repository
88
public interface CategoryRepository extends JpaRepository<Category, Long>, CategoryRepositoryCustom {
99
boolean existsByNameAndTeam_Id(String name, Long teamId);
10+
11+
long countByTeam_Id(Long teamId);
1012
}

src/main/java/com/sonkim/bookmarking/domain/category/repository/CategoryRepositoryCustom.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
public interface CategoryRepositoryCustom {
88
// 카테고리 정보와 해당 카테고리에 속한 북마크의 개수를 카운트
9-
List<CategoryDto.CategoryResponseDto> findAllWithBookmarkCountByTeam_Id(Long teamId);
9+
List<CategoryDto.CategoryResponseDto> findAllWithBookmarkCountByTeamId(Long teamId);
1010
}

src/main/java/com/sonkim/bookmarking/domain/category/repository/CategoryRepositoryCustomImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class CategoryRepositoryCustomImpl implements CategoryRepositoryCustom {
1616
private final JPAQueryFactory queryFactory;
1717

1818
@Override
19-
public List<CategoryDto.CategoryResponseDto> findAllWithBookmarkCountByTeam_Id(Long teamId) {
19+
public List<CategoryDto.CategoryResponseDto> findAllWithBookmarkCountByTeamId(Long teamId) {
2020
return queryFactory
2121
.select(Projections.constructor(CategoryDto.CategoryResponseDto.class,
2222
category.id,
@@ -27,6 +27,7 @@ public List<CategoryDto.CategoryResponseDto> findAllWithBookmarkCountByTeam_Id(L
2727
.leftJoin(bookmark).on(bookmark.category.id.eq(category.id))
2828
.where(category.team.id.eq(teamId))
2929
.groupBy(category.id, category.name)
30+
.orderBy(category.position.asc())
3031
.fetch();
3132
}
3233
}

src/main/java/com/sonkim/bookmarking/domain/category/service/CategoryService.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import java.util.ArrayList;
1717
import java.util.List;
18+
import java.util.Map;
19+
import java.util.stream.Collectors;
1820

1921
@Slf4j
2022
@RequiredArgsConstructor
@@ -42,11 +44,15 @@ public List<CategoryDto.CategoryResponseDto> createCategory(Long userId, Long te
4244
throw new IllegalStateException("이미 존재하는 카테고리 이름입니다.");
4345
}
4446

47+
// 현재 그룹의 카테고리 개수
48+
long currentCategoryCount = categoryRepository.countByTeam_Id(teamId);
49+
4550
// 카테고리 객체 생성 및 저장
4651
Team team = teamService.getTeamById(teamId);
4752
Category newCategory = Category.builder()
4853
.name(request.getName())
4954
.team(team)
55+
.position((int) currentCategoryCount + 1)
5056
.build();
5157
categoryRepository.save(newCategory);
5258

@@ -57,7 +63,7 @@ public List<CategoryDto.CategoryResponseDto> createCategory(Long userId, Long te
5763
@Transactional(readOnly = true)
5864
public List<CategoryDto.CategoryResponseDto> getCategoriesByTeam(Long teamId) {
5965
// 각 카테고리 정보와 북마크 개수 조회
60-
List<CategoryDto.CategoryResponseDto> categoriesWithCount = categoryRepository.findAllWithBookmarkCountByTeam_Id(teamId);
66+
List<CategoryDto.CategoryResponseDto> categoriesWithCount = categoryRepository.findAllWithBookmarkCountByTeamId(teamId);
6167

6268
// 그룹 전체 북마크 개수 조회
6369
long totalBookmarkCount = bookmarkRepository.countByTeam_Id(teamId);
@@ -126,6 +132,26 @@ public List<CategoryDto.CategoryResponseDto> deleteCategory(Long userId, Long ca
126132
return getCategoriesByTeam(teamId);
127133
}
128134

135+
// 카테고리 순서 수정
136+
@Transactional
137+
public void updateCategoryPositions(Long userId, Long teamId, List<CategoryDto.UpdatePositionRequestDto> requests) {
138+
log.info("userId: {}, teamId: {} 카테고리 순서 수정 요청", userId, teamId);
139+
140+
// 요청자가 EDITOR 권한이 있는지 검사
141+
teamMemberService.validateEditor(userId, teamId);
142+
143+
// 요청된 모든 카테고리 ID 조회
144+
List<Long> categoryIds = requests.stream().map(CategoryDto.UpdatePositionRequestDto::getCategoryId).toList();
145+
Map<Long, Category> categoryMap = categoryRepository.findAllById(categoryIds).stream()
146+
.collect(Collectors.toMap(Category::getId, category -> category));
147+
148+
// 각 카테고리의 position 값 업데이트
149+
for (CategoryDto.UpdatePositionRequestDto request : requests) {
150+
Category category = categoryMap.get(request.getCategoryId());
151+
category.updatePosition(request.getPosition());
152+
}
153+
}
154+
129155
public Category getCategoryById(Long categoryId) {
130156
return categoryRepository.findById(categoryId)
131157
.orElseThrow(() -> new EntityNotFoundException("해당 카테고리를 찾을 수 없습니다."));

0 commit comments

Comments
 (0)