-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 폴더관련 CRUD 추가 및 이에 따른 콜렉트하기, 콜렉션조회 수정
- Loading branch information
Showing
21 changed files
with
352 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/listywave/collection/application/domain/Folder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/listywave/collection/application/domain/FolderName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + "자를 넘을 수 없습니다."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/listywave/collection/application/dto/FolderCreateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/listywave/collection/application/dto/FolderListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.listywave.collection.application.dto; | ||
|
||
import java.util.List; | ||
|
||
public record FolderListResponse( | ||
List<FolderResponse> folders | ||
) { | ||
|
||
public static FolderListResponse of(List<FolderResponse> list){ | ||
return new FolderListResponse(list); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/listywave/collection/application/dto/FolderResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.listywave.collection.application.dto; | ||
|
||
public record FolderResponse( | ||
Long folderId, | ||
String folderName, | ||
Long listCount | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/listywave/collection/application/service/FolderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Collect> 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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/listywave/collection/presentation/controller/FolderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<FolderCreateResponse> 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<Void> 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<Void> delete( | ||
@Auth Long loginUserId, | ||
@PathVariable("folderId") Long folderId | ||
) { | ||
folderService.deleteFolder(loginUserId, folderId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@GetMapping("/collect/folder") | ||
ResponseEntity<FolderListResponse> getFolders( | ||
@Auth Long loginUserId | ||
) { | ||
FolderListResponse response = folderService.getFolders(loginUserId); | ||
return ResponseEntity.ok(response); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/listywave/collection/presentation/dto/FolderCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.listywave.collection.presentation.dto; | ||
|
||
public record FolderCreateRequest( | ||
String folderName | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/listywave/collection/presentation/dto/FolderUpdateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.listywave.collection.presentation.dto; | ||
|
||
public record FolderUpdateRequest( | ||
String folderName | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.