From 2ef43a27362f4bc4f7058c534547b9a6ebb79b07 Mon Sep 17 00:00:00 2001 From: pparkjs Date: Wed, 18 Sep 2024 19:05:17 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=8F=B4=EB=8D=94=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?CRUD=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=9D=B4=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=BD=9C=EB=A0=89=ED=8A=B8=ED=95=98?= =?UTF-8?q?=EA=B8=B0,=20=EC=BD=9C=EB=A0=89=EC=85=98=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/domain/Collect.java | 7 +- .../collection/application/domain/Folder.java | 46 +++++++++++++ .../application/domain/FolderName.java | 32 +++++++++ .../application/dto/CollectionResponse.java | 2 - .../application/dto/FolderCreateResponse.java | 8 +++ .../application/dto/FolderListResponse.java | 12 ++++ .../application/dto/FolderResponse.java | 8 +++ .../service/CollectionService.java | 18 +++-- .../application/service/FolderService.java | 67 +++++++++++++++++++ .../controller/CollectionController.java | 9 +-- .../controller/FolderController.java | 56 ++++++++++++++++ .../presentation/dto/FolderCreateRequest.java | 6 ++ .../presentation/dto/FolderUpdateRequest.java | 6 ++ .../repository/CollectionRepository.java | 9 ++- .../repository/FolderRepository.java | 17 +++++ .../custom/CustomCollectionRepository.java | 2 +- .../custom/CustomFolderRepository.java | 10 +++ .../impl/CustomCollectionRepositoryImpl.java | 17 ++--- .../impl/CustomFolderRepositoryImpl.java | 40 +++++++++++ .../listywave/common/exception/ErrorCode.java | 1 + .../user/application/service/UserService.java | 6 +- 21 files changed, 352 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/listywave/collection/application/domain/Folder.java create mode 100644 src/main/java/com/listywave/collection/application/domain/FolderName.java create mode 100644 src/main/java/com/listywave/collection/application/dto/FolderCreateResponse.java create mode 100644 src/main/java/com/listywave/collection/application/dto/FolderListResponse.java create mode 100644 src/main/java/com/listywave/collection/application/dto/FolderResponse.java create mode 100644 src/main/java/com/listywave/collection/application/service/FolderService.java create mode 100644 src/main/java/com/listywave/collection/presentation/controller/FolderController.java create mode 100644 src/main/java/com/listywave/collection/presentation/dto/FolderCreateRequest.java create mode 100644 src/main/java/com/listywave/collection/presentation/dto/FolderUpdateRequest.java create mode 100644 src/main/java/com/listywave/collection/repository/FolderRepository.java create mode 100644 src/main/java/com/listywave/collection/repository/custom/CustomFolderRepository.java create mode 100644 src/main/java/com/listywave/collection/repository/custom/impl/CustomFolderRepositoryImpl.java diff --git a/src/main/java/com/listywave/collection/application/domain/Collect.java b/src/main/java/com/listywave/collection/application/domain/Collect.java index bc923432..e980f954 100644 --- a/src/main/java/com/listywave/collection/application/domain/Collect.java +++ b/src/main/java/com/listywave/collection/application/domain/Collect.java @@ -31,11 +31,16 @@ public class Collect { @JoinColumn(name = "list_id") private ListEntity list; + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "folder_id") + private Folder folder; + @Column(name = "user_id", nullable = false) private Long userId; - public Collect(ListEntity list, Long userId) { + public Collect(ListEntity list, Long userId, Folder folder) { this.list = list; this.userId = userId; + this.folder = folder; } } diff --git a/src/main/java/com/listywave/collection/application/domain/Folder.java b/src/main/java/com/listywave/collection/application/domain/Folder.java new file mode 100644 index 00000000..6c675af8 --- /dev/null +++ b/src/main/java/com/listywave/collection/application/domain/Folder.java @@ -0,0 +1,46 @@ +package com.listywave.collection.application.domain; + +import com.listywave.common.BaseEntity; +import com.listywave.common.exception.CustomException; +import jakarta.persistence.Column; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import static com.listywave.common.exception.ErrorCode.INVALID_ACCESS; +import static lombok.AccessLevel.PROTECTED; + +@Entity +@Getter +@AllArgsConstructor +@Table(name = "folder") +@NoArgsConstructor(access = PROTECTED) +public class Folder extends BaseEntity { + + @Column(name = "user_id", nullable = false) + private Long userId; + + @Embedded + private FolderName name; + + public static Folder create(Long loginUserId, FolderName folderName) { + return new Folder(loginUserId, folderName); + } + + public void update(FolderName folderName, Long userId) { + validateOwner(userId); + this.name = folderName; + } + public String getFolderName(){ + return this.name.getValue(); + } + + public void validateOwner(Long userId) { + if (!this.userId.equals(userId)) { + throw new CustomException(INVALID_ACCESS); + } + } +} diff --git a/src/main/java/com/listywave/collection/application/domain/FolderName.java b/src/main/java/com/listywave/collection/application/domain/FolderName.java new file mode 100644 index 00000000..200a1012 --- /dev/null +++ b/src/main/java/com/listywave/collection/application/domain/FolderName.java @@ -0,0 +1,32 @@ +package com.listywave.collection.application.domain; + +import com.listywave.common.exception.CustomException; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import lombok.*; + +import static com.listywave.common.exception.ErrorCode.LENGTH_EXCEEDED_EXCEPTION; + +@Getter +@Builder +@Embeddable +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true) +public class FolderName { + + private static final int LENGTH_LIMIT = 30; + + @Column(name = "name", nullable = false, length = LENGTH_LIMIT) + private final String value; + + public FolderName(String value) { + validate(value); + this.value = value; + } + + private void validate(String value) { + if (value.length() > LENGTH_LIMIT) { + throw new CustomException(LENGTH_EXCEEDED_EXCEPTION, "폴더 이름은 " + LENGTH_LIMIT + "자를 넘을 수 없습니다."); + } + } +} diff --git a/src/main/java/com/listywave/collection/application/dto/CollectionResponse.java b/src/main/java/com/listywave/collection/application/dto/CollectionResponse.java index 8d75471b..bd43f39c 100644 --- a/src/main/java/com/listywave/collection/application/dto/CollectionResponse.java +++ b/src/main/java/com/listywave/collection/application/dto/CollectionResponse.java @@ -45,7 +45,6 @@ public record ListsResponse( Long ownerId, String ownerNickname, String ownerProfileImageUrl, - String representativeImageUrl, LocalDateTime updatedDate, List listItems ) { @@ -58,7 +57,6 @@ public static ListsResponse of(ListEntity list) { .ownerId(list.getUser().getId()) .ownerNickname(list.getUser().getNickname()) .ownerProfileImageUrl(list.getUser().getProfileImageUrl()) - .representativeImageUrl(list.getRepresentImageUrl()) .updatedDate(list.getUpdatedDate()) .listItems(toList(list.getTop3Items().getValues())) .build(); diff --git a/src/main/java/com/listywave/collection/application/dto/FolderCreateResponse.java b/src/main/java/com/listywave/collection/application/dto/FolderCreateResponse.java new file mode 100644 index 00000000..a5a74c0a --- /dev/null +++ b/src/main/java/com/listywave/collection/application/dto/FolderCreateResponse.java @@ -0,0 +1,8 @@ +package com.listywave.collection.application.dto; + +public record FolderCreateResponse(Long folderId) { + + public static FolderCreateResponse of(Long id) { + return new FolderCreateResponse(id); + } +} diff --git a/src/main/java/com/listywave/collection/application/dto/FolderListResponse.java b/src/main/java/com/listywave/collection/application/dto/FolderListResponse.java new file mode 100644 index 00000000..582dbc76 --- /dev/null +++ b/src/main/java/com/listywave/collection/application/dto/FolderListResponse.java @@ -0,0 +1,12 @@ +package com.listywave.collection.application.dto; + +import java.util.List; + +public record FolderListResponse( + List folders +) { + + public static FolderListResponse of(List list){ + return new FolderListResponse(list); + } +} diff --git a/src/main/java/com/listywave/collection/application/dto/FolderResponse.java b/src/main/java/com/listywave/collection/application/dto/FolderResponse.java new file mode 100644 index 00000000..c942e4b5 --- /dev/null +++ b/src/main/java/com/listywave/collection/application/dto/FolderResponse.java @@ -0,0 +1,8 @@ +package com.listywave.collection.application.dto; + +public record FolderResponse( + Long folderId, + String folderName, + Long listCount +) { +} diff --git a/src/main/java/com/listywave/collection/application/service/CollectionService.java b/src/main/java/com/listywave/collection/application/service/CollectionService.java index 19b0fbe6..3e623dd2 100644 --- a/src/main/java/com/listywave/collection/application/service/CollectionService.java +++ b/src/main/java/com/listywave/collection/application/service/CollectionService.java @@ -2,8 +2,10 @@ import com.listywave.alarm.application.domain.AlarmEvent; import com.listywave.collection.application.domain.Collect; +import com.listywave.collection.application.domain.Folder; import com.listywave.collection.application.dto.CollectionResponse; import com.listywave.collection.repository.CollectionRepository; +import com.listywave.collection.repository.FolderRepository; import com.listywave.list.application.domain.category.CategoryType; import com.listywave.list.application.domain.list.ListEntity; import com.listywave.list.application.dto.response.CategoryTypeResponse; @@ -26,24 +28,27 @@ public class CollectionService { private final UserRepository userRepository; private final ListRepository listRepository; + private final FolderRepository folderRepository; private final CollectionRepository collectionRepository; private final ApplicationEventPublisher applicationEventPublisher; - public void collectOrCancel(Long listId, Long loginUserId) { + public void collectOrCancel(Long listId, Long folderId, Long loginUserId) { User loginUser = userRepository.getById(loginUserId); ListEntity list = listRepository.getById(listId); + Folder folder = folderRepository.getById(folderId); + folder.validateOwner(loginUserId); list.validateNotOwner(loginUser); if (collectionRepository.existsByListAndUserId(list, loginUser.getId())) { cancelCollect(list, loginUser.getId()); } else { - addCollect(list, loginUser); + addCollect(list, loginUser, folder); } } - private void addCollect(ListEntity list, User user) { - Collect collection = new Collect(list, user.getId()); + private void addCollect(ListEntity list, User user, Folder folder) { + Collect collection = new Collect(list, user.getId(), folder); collectionRepository.save(collection); list.increaseCollectCount(); @@ -55,9 +60,10 @@ private void cancelCollect(ListEntity list, Long userId) { list.decreaseCollectCount(); } - public CollectionResponse getCollection(Long loginUserId, Long cursorId, Pageable pageable, CategoryType category) { + public CollectionResponse getCollection(Long loginUserId, Long cursorId, Pageable pageable, Long folderId) { User user = userRepository.getById(loginUserId); - Slice result = collectionRepository.getAllCollectionList(cursorId, pageable, user.getId(), category); + folderRepository.getById(folderId); + Slice result = collectionRepository.getAllCollectionList(cursorId, pageable, user.getId(), folderId); List collectionList = result.getContent(); cursorId = null; diff --git a/src/main/java/com/listywave/collection/application/service/FolderService.java b/src/main/java/com/listywave/collection/application/service/FolderService.java new file mode 100644 index 00000000..417e1d9c --- /dev/null +++ b/src/main/java/com/listywave/collection/application/service/FolderService.java @@ -0,0 +1,67 @@ +package com.listywave.collection.application.service; + +import com.listywave.collection.application.domain.Collect; +import com.listywave.collection.application.domain.Folder; +import com.listywave.collection.application.domain.FolderName; +import com.listywave.collection.application.dto.FolderCreateResponse; +import com.listywave.collection.application.dto.FolderListResponse; +import com.listywave.collection.repository.CollectionRepository; +import com.listywave.collection.repository.FolderRepository; +import com.listywave.common.exception.CustomException; +import com.listywave.user.application.domain.User; +import com.listywave.user.application.service.UserService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +import static com.listywave.common.exception.ErrorCode.DUPLICATE_FOLDER_NAME_EXCEPTION; + +@Service +@Transactional +@RequiredArgsConstructor +public class FolderService { + + private final FolderRepository folderRepository; + private final CollectionRepository collectionRepository; + private final UserService userService; + + public FolderCreateResponse createFolder(Long loginUserId, String folderName) { + User user = userService.getById(loginUserId); + if(folderRepository.existsByNameValueAndUserId(folderName, user.getId())){ + throw new CustomException(DUPLICATE_FOLDER_NAME_EXCEPTION); + } + Folder newFolder = Folder.create(user.getId(), new FolderName(folderName)); + Folder folder = folderRepository.save(newFolder); + return FolderCreateResponse.of(folder.getId()); + } + + public void updateFolder(Long loginUserId, Long folderId, String folderName) { + User user = userService.getById(loginUserId); + if(folderRepository.existsByNameValueAndUserId(folderName, user.getId())){ + throw new CustomException(DUPLICATE_FOLDER_NAME_EXCEPTION); + } + Folder folder = folderRepository.getById(folderId); + folder.update(new FolderName(folderName), user.getId()); + } + + public void deleteFolder(Long loginUserId, Long folderId) { + User user = userService.getById(loginUserId); + Folder folder = folderRepository.getById(folderId); + folder.validateOwner(user.getId()); + cancelCollectsByFolder(folder); + folderRepository.deleteById(folderId); + } + + private void cancelCollectsByFolder(Folder folder) { + List collects = collectionRepository.findAllByFolder(folder); + collects.forEach(collect -> collect.getList().decreaseCollectCount()); + collectionRepository.deleteAllByFolder(folder); + } + + public FolderListResponse getFolders(Long loginUserId) { + User user = userService.getById(loginUserId); + return FolderListResponse.of(folderRepository.findByFolders(user.getId())); + } +} diff --git a/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java b/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java index cded983e..90df8a88 100644 --- a/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java +++ b/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java @@ -22,23 +22,24 @@ public class CollectionController { private final CollectionService collectionService; - @PostMapping("/lists/{listId}/collect") + @PostMapping("/lists/{listId}/collect/{folderId}") ResponseEntity collectOrCancel( @PathVariable("listId") Long listId, + @PathVariable("folderId") Long folderId, @Auth Long loginUserId ) { - collectionService.collectOrCancel(listId, loginUserId); + collectionService.collectOrCancel(listId, folderId, loginUserId); return ResponseEntity.noContent().build(); } @GetMapping("/lists/collect") ResponseEntity getCollection( @Auth Long loginUserId, - @RequestParam(name = "category", defaultValue = "entire") CategoryType category, + @RequestParam(name = "folderId", required = true) Long folderId, @RequestParam(name = "cursorId", required = false) Long cursorId, @PageableDefault(size = 10) Pageable pageable ) { - CollectionResponse collection = collectionService.getCollection(loginUserId, cursorId, pageable, category); + CollectionResponse collection = collectionService.getCollection(loginUserId, cursorId, pageable, folderId); return ResponseEntity.ok(collection); } diff --git a/src/main/java/com/listywave/collection/presentation/controller/FolderController.java b/src/main/java/com/listywave/collection/presentation/controller/FolderController.java new file mode 100644 index 00000000..f930c262 --- /dev/null +++ b/src/main/java/com/listywave/collection/presentation/controller/FolderController.java @@ -0,0 +1,56 @@ +package com.listywave.collection.presentation.controller; + +import com.listywave.collection.application.dto.FolderCreateResponse; +import com.listywave.collection.application.dto.FolderListResponse; +import com.listywave.collection.application.service.FolderService; +import com.listywave.collection.presentation.dto.FolderCreateRequest; +import com.listywave.collection.presentation.dto.FolderUpdateRequest; +import com.listywave.common.auth.Auth; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import static org.springframework.http.HttpStatus.CREATED; + +@RestController +@RequiredArgsConstructor +public class FolderController { + + private final FolderService folderService; + + @PostMapping("/collect/folder") + ResponseEntity create( + @Auth Long loginUserId, + @RequestBody FolderCreateRequest request + ) { + FolderCreateResponse response = folderService.createFolder(loginUserId, request.folderName()); + return ResponseEntity.status(CREATED).body(response); + } + + @PutMapping("/collect/folder/{folderId}") + ResponseEntity update( + @Auth Long loginUserId, + @PathVariable("folderId") Long folderId, + @RequestBody FolderUpdateRequest request + ) { + folderService.updateFolder(loginUserId, folderId, request.folderName()); + return ResponseEntity.noContent().build(); + } + + @DeleteMapping("/collect/folder/{folderId}") + ResponseEntity delete( + @Auth Long loginUserId, + @PathVariable("folderId") Long folderId + ) { + folderService.deleteFolder(loginUserId, folderId); + return ResponseEntity.noContent().build(); + } + + @GetMapping("/collect/folder") + ResponseEntity getFolders( + @Auth Long loginUserId + ) { + FolderListResponse response = folderService.getFolders(loginUserId); + return ResponseEntity.ok(response); + } +} diff --git a/src/main/java/com/listywave/collection/presentation/dto/FolderCreateRequest.java b/src/main/java/com/listywave/collection/presentation/dto/FolderCreateRequest.java new file mode 100644 index 00000000..7e7b1070 --- /dev/null +++ b/src/main/java/com/listywave/collection/presentation/dto/FolderCreateRequest.java @@ -0,0 +1,6 @@ +package com.listywave.collection.presentation.dto; + +public record FolderCreateRequest( + String folderName +) { +} diff --git a/src/main/java/com/listywave/collection/presentation/dto/FolderUpdateRequest.java b/src/main/java/com/listywave/collection/presentation/dto/FolderUpdateRequest.java new file mode 100644 index 00000000..5e21dd58 --- /dev/null +++ b/src/main/java/com/listywave/collection/presentation/dto/FolderUpdateRequest.java @@ -0,0 +1,6 @@ +package com.listywave.collection.presentation.dto; + +public record FolderUpdateRequest( + String folderName +) { +} diff --git a/src/main/java/com/listywave/collection/repository/CollectionRepository.java b/src/main/java/com/listywave/collection/repository/CollectionRepository.java index 15897678..d87c5ee9 100644 --- a/src/main/java/com/listywave/collection/repository/CollectionRepository.java +++ b/src/main/java/com/listywave/collection/repository/CollectionRepository.java @@ -1,13 +1,14 @@ package com.listywave.collection.repository; import com.listywave.collection.application.domain.Collect; +import com.listywave.collection.application.domain.Folder; import com.listywave.collection.repository.custom.CustomCollectionRepository; import com.listywave.list.application.domain.list.ListEntity; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.util.List; public interface CollectionRepository extends JpaRepository, CustomCollectionRepository { @@ -20,4 +21,10 @@ public interface CollectionRepository extends JpaRepository, Cust @Modifying @Query("delete from Collect c where c.list in :lists") void deleteAllByListIn(@Param("lists") List lists); + + @Modifying + @Query("delete from Collect c where c.folder =:folder") + void deleteAllByFolder(@Param("folder") Folder folder); + + List findAllByFolder(Folder folder); } diff --git a/src/main/java/com/listywave/collection/repository/FolderRepository.java b/src/main/java/com/listywave/collection/repository/FolderRepository.java new file mode 100644 index 00000000..7235c4ea --- /dev/null +++ b/src/main/java/com/listywave/collection/repository/FolderRepository.java @@ -0,0 +1,17 @@ +package com.listywave.collection.repository; + +import static com.listywave.common.exception.ErrorCode.RESOURCE_NOT_FOUND; + +import com.listywave.collection.application.domain.Folder; +import com.listywave.collection.repository.custom.CustomFolderRepository; +import com.listywave.common.exception.CustomException; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FolderRepository extends JpaRepository, CustomFolderRepository { + + boolean existsByNameValueAndUserId(String nameValue, Long loginUserId); + + default Folder getById(Long folderId) { + return findById(folderId).orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND)); + } +} diff --git a/src/main/java/com/listywave/collection/repository/custom/CustomCollectionRepository.java b/src/main/java/com/listywave/collection/repository/custom/CustomCollectionRepository.java index 66aa94a6..7451a3fb 100644 --- a/src/main/java/com/listywave/collection/repository/custom/CustomCollectionRepository.java +++ b/src/main/java/com/listywave/collection/repository/custom/CustomCollectionRepository.java @@ -9,7 +9,7 @@ public interface CustomCollectionRepository { - Slice getAllCollectionList(Long cursorId, Pageable pageable, Long userId, CategoryType category); + Slice getAllCollectionList(Long cursorId, Pageable pageable, Long userId, Long folderId); List getCategoriesByCollect(User user); } diff --git a/src/main/java/com/listywave/collection/repository/custom/CustomFolderRepository.java b/src/main/java/com/listywave/collection/repository/custom/CustomFolderRepository.java new file mode 100644 index 00000000..7079829f --- /dev/null +++ b/src/main/java/com/listywave/collection/repository/custom/CustomFolderRepository.java @@ -0,0 +1,10 @@ +package com.listywave.collection.repository.custom; + +import com.listywave.collection.application.dto.FolderResponse; + +import java.util.List; + +public interface CustomFolderRepository { + + List findByFolders(Long loginUserId); +} diff --git a/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java b/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java index e05ab9c5..c2d4d864 100644 --- a/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java +++ b/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java @@ -13,10 +13,10 @@ import com.listywave.user.application.domain.User; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQueryFactory; -import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; +import java.util.List; @RequiredArgsConstructor public class CustomCollectionRepositoryImpl implements CustomCollectionRepository { @@ -24,7 +24,7 @@ public class CustomCollectionRepositoryImpl implements CustomCollectionRepositor private final JPAQueryFactory queryFactory; @Override - public Slice getAllCollectionList(Long cursorId, Pageable pageable, Long userId, CategoryType category) { + public Slice getAllCollectionList(Long cursorId, Pageable pageable, Long userId, Long folderId) { List fetch = queryFactory .selectFrom(collect) .join(collect.list, listEntity).fetchJoin() @@ -33,7 +33,7 @@ public Slice getAllCollectionList(Long cursorId, Pageable pageable, Lon .where( collectIdLt(cursorId), userIdEq(userId), - categoryEq(category) + folderIdEq(folderId) ) .distinct() .limit(pageable.getPageSize() + 1) @@ -42,15 +42,12 @@ public Slice getAllCollectionList(Long cursorId, Pageable pageable, Lon return checkEndPage(pageable, fetch); } - private BooleanExpression collectIdLt(Long cursorId) { - return cursorId == null ? null : collect.id.lt(cursorId); + private BooleanExpression folderIdEq(Long folderId) { + return collect.folder.id.eq(folderId); } - private BooleanExpression categoryEq(CategoryType category) { - if (category.equals(ENTIRE)) { - return null; - } - return listEntity.category.eq(category); + private BooleanExpression collectIdLt(Long cursorId) { + return cursorId == null ? null : collect.id.lt(cursorId); } @Override diff --git a/src/main/java/com/listywave/collection/repository/custom/impl/CustomFolderRepositoryImpl.java b/src/main/java/com/listywave/collection/repository/custom/impl/CustomFolderRepositoryImpl.java new file mode 100644 index 00000000..8eb66424 --- /dev/null +++ b/src/main/java/com/listywave/collection/repository/custom/impl/CustomFolderRepositoryImpl.java @@ -0,0 +1,40 @@ +package com.listywave.collection.repository.custom.impl; + +import static com.listywave.collection.application.domain.QCollect.*; +import static com.listywave.collection.application.domain.QFolder.*; +import static com.querydsl.jpa.JPAExpressions.*; + +import com.listywave.collection.application.dto.FolderResponse; +import com.listywave.collection.repository.custom.CustomFolderRepository; +import com.querydsl.core.types.ExpressionUtils; +import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.Expressions; +import com.querydsl.core.types.dsl.NumberPath; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import java.util.List; + +@RequiredArgsConstructor +public class CustomFolderRepositoryImpl implements CustomFolderRepository { + + private final JPAQueryFactory queryFactory; + + @Override + public List findByFolders(Long loginUserId) { + NumberPath listCountAlias = Expressions.numberPath(Long.class, "listCount"); + return queryFactory + .select(Projections.constructor(FolderResponse.class, + folder.id.as("folderId"), + folder.name.value.as("folderName"), + ExpressionUtils.as( + select(collect.count()) + .from(collect) + .where(collect.folder.id.eq(folder.id)), listCountAlias) + ) + ) + .from(folder) + .where(folder.userId.eq(loginUserId)) + .orderBy(folder.updatedDate.desc()) + .fetch(); + } +} diff --git a/src/main/java/com/listywave/common/exception/ErrorCode.java b/src/main/java/com/listywave/common/exception/ErrorCode.java index 4f2a654f..e8ccb21f 100644 --- a/src/main/java/com/listywave/common/exception/ErrorCode.java +++ b/src/main/java/com/listywave/common/exception/ErrorCode.java @@ -44,6 +44,7 @@ public enum ErrorCode { ALREADY_LOGOUT_EXCEPTION(BAD_REQUEST, "이미 로그아웃 처리가 된 상태입니다."), DUPLICATE_NICKNAME_EXCEPTION(BAD_REQUEST, "중복된 닉네임입니다."), DUPLICATE_COLLABORATOR_EXCEPTION(BAD_REQUEST, "이미 동일한 콜라보레이터가 존재합니다"), + DUPLICATE_FOLDER_NAME_EXCEPTION(BAD_REQUEST, "중복된 폴더명입니다."), // S3 S3_DELETE_OBJECTS_EXCEPTION(INTERNAL_SERVER_ERROR, "S3의 이미지를 삭제 요청하는 과정에서 에러가 발생했습니다."), diff --git a/src/main/java/com/listywave/user/application/service/UserService.java b/src/main/java/com/listywave/user/application/service/UserService.java index d4f1afe5..398ccb20 100644 --- a/src/main/java/com/listywave/user/application/service/UserService.java +++ b/src/main/java/com/listywave/user/application/service/UserService.java @@ -7,8 +7,6 @@ import com.listywave.alarm.application.domain.AlarmEvent; import com.listywave.common.exception.CustomException; -import com.listywave.list.application.domain.list.ListEntity; -import com.listywave.list.repository.list.ListRepository; import com.listywave.user.application.domain.Follow; import com.listywave.user.application.domain.User; import com.listywave.user.application.dto.FollowersResponse; @@ -182,4 +180,8 @@ public UserElasticSearchResponse searchUserByElastic(@Nullable Long loginUserId, User user = userRepository.getById(loginUserId); return userElasticRepository.findAll(user.getId(), keyword, pageable); } + + public User getById(Long userId){ + return userRepository.getById(userId); + } }