Skip to content

Commit

Permalink
Merge pull request #196 from Team-Tiki/feat/#194-time-block
Browse files Browse the repository at this point in the history
[FEAT] 타임 블록 api 수정
  • Loading branch information
Chan531 authored Dec 13, 2024
2 parents a6e6ad6 + 0433a9f commit 7dc8a09
Show file tree
Hide file tree
Showing 27 changed files with 490 additions and 320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.tiki.server.common.support.RepositoryAdapter;
import com.tiki.server.document.entity.Document;
import com.tiki.server.document.repository.DocumentRepository;
import com.tiki.server.document.vo.DocumentVO;

import lombok.RequiredArgsConstructor;

Expand All @@ -15,15 +14,7 @@ public class DocumentDeleter {

private final DocumentRepository documentRepository;

public void delete(Document document) {
documentRepository.delete(document);
}

public void deleteAll(List<Document> documents) {
documentRepository.deleteAll(documents);
}

public void deleteAllByTimeBlockId(long timeBlockId) {
documentRepository.deleteAllByTimeBlockId(timeBlockId);
}
}
24 changes: 3 additions & 21 deletions src/main/java/com/tiki/server/document/adapter/DocumentFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import static com.tiki.server.document.message.ErrorCode.INVALID_DOCUMENT;

import java.util.List;
import java.util.Objects;

import com.tiki.server.common.entity.Position;
import com.tiki.server.common.support.RepositoryAdapter;
import com.tiki.server.document.entity.Document;
import com.tiki.server.document.exception.DocumentException;
import com.tiki.server.document.repository.DocumentRepository;
import com.tiki.server.document.vo.DocumentVO;

import lombok.RequiredArgsConstructor;

Expand All @@ -26,24 +23,9 @@ public List<Document> findAllByIdAndTeamId(final List<Long> documentIds, final l
.toList();
}

public Document findByIdOrElseThrow(final long documentId) {
return documentRepository.findById(documentId).orElseThrow(() -> new DocumentException(INVALID_DOCUMENT));
}

public Document findByIdWithTimeBlock(long documentId) {
Document document = documentRepository.findByIdWithTimeBlock(documentId);
if (Objects.isNull(document)) {
throw new DocumentException(INVALID_DOCUMENT);
}
return document;
}

public List<DocumentVO> findAllByTimeBlockId(long timeBlockId) {
return documentRepository.findAllByTimeBlockId(timeBlockId).stream().map(DocumentVO::from).toList();
}

public List<Document> findAllByTeamIdAndAccessiblePosition(long teamId, Position accessiblePosition) {
return documentRepository.findAllByTeamIdAndAccessiblePosition(teamId, accessiblePosition);
public Document findById(final long documentId) {
return documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentException(INVALID_DOCUMENT));
}

public List<Document> findAllByTeamId(long teamId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public void restore(final List<DeletedDocument> deletedDocuments) {
}

private Document create(final DeletedDocument deletedDocument) {
return Document.restore(
deletedDocument.getFileName(),
deletedDocument.getFileUrl(),
deletedDocument.getCapacity(),
deletedDocument.getTeamId()
);
return Document.restore(deletedDocument);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ public ResponseEntity<SuccessResponse<DocumentsGetResponse>> getAllDocuments(
return ResponseEntity.ok(SuccessResponse.success(SUCCESS_GET_DOCUMENTS.getMessage(), response));
}

@Override
@DeleteMapping("/documents/team/{teamId}/document/{documentId}")
public ResponseEntity<?> deleteDocument(
final Principal principal,
@PathVariable final long teamId,
@PathVariable final long documentId
) {
long memberId = Long.parseLong(principal.getName());
documentService.deleteDocument(memberId, teamId, documentId);
return ResponseEntity.noContent().build();
}

@PostMapping("/teams/{teamId}/documents")
public ResponseEntity<SuccessResponse<?>> createDocuments(
final Principal principal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,6 @@ ResponseEntity<SuccessResponse<DocumentsGetResponse>> getAllDocuments(
) @RequestParam String type
);

@Operation(
summary = "문서 삭제",
description = "문서를 삭제한다.",
responses = {
@ApiResponse(responseCode = "204", description = "성공"),
@ApiResponse(
responseCode = "403",
description = "접근 권한 없음",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(
responseCode = "404",
description = "팀에 존재하지 않는 회원, 유효하지 않은 문서",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(
responseCode = "4xx",
description = "클라이언트(요청) 오류",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(
responseCode = "500",
description = "서버 내부 오류",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))}
)
ResponseEntity<?> deleteDocument(
@Parameter(hidden = true) Principal principal,
@Parameter(
name = "teamId",
description = "팀 id",
in = ParameterIn.PATH,
example = "1"
) @PathVariable long teamId,
@Parameter(
name = "documentId",
description = "문서 id",
in = ParameterIn.PATH,
example = "1"
) @PathVariable long documentId
);

@Operation(
summary = "문서 생성",
description = "문서를 여러 개 생성한다.",
Expand All @@ -119,13 +81,13 @@ ResponseEntity<?> deleteDocument(
ResponseEntity<SuccessResponse<?>> createDocuments(
@Parameter(hidden = true) Principal principal,
@Parameter(
name = "팀 id",
name = "teamId",
description = "팀 id",
in = ParameterIn.PATH,
example = "1"
) @PathVariable long teamId,
@Parameter(
name = "폴더 id",
name = "folderId",
description = "생성할 파일이 속할 폴더 id",
in = ParameterIn.QUERY,
example = "1"
Expand All @@ -150,13 +112,13 @@ ResponseEntity<SuccessResponse<?>> createDocuments(
ResponseEntity<SuccessResponse<DocumentsGetResponse>> getDocuments(
@Parameter(hidden = true) Principal principal,
@Parameter(
name = "팀 id",
name = "teamId",
description = "팀 id",
in = ParameterIn.PATH,
example = "1"
) @PathVariable long teamId,
@Parameter(
name = "폴더 id",
name = "folderId",
description = "조회할 폴더 id",
in = ParameterIn.QUERY,
example = "1"
Expand All @@ -180,13 +142,13 @@ ResponseEntity<SuccessResponse<DocumentsGetResponse>> getDocuments(
ResponseEntity<?> delete(
@Parameter(hidden = true) Principal principal,
@Parameter(
name = "팀 id",
name = "teamId",
description = "팀 id",
in = ParameterIn.PATH,
example = "1"
) @PathVariable long teamId,
@Parameter(
name = "파일 id",
name = "documentId",
description = "삭제할 파일 id 리스트",
in = ParameterIn.QUERY,
required = true,
Expand All @@ -211,13 +173,13 @@ ResponseEntity<?> delete(
ResponseEntity<?> deleteTrash(
@Parameter(hidden = true) Principal principal,
@Parameter(
name = "팀 id",
name = "teamId",
description = "팀 id",
in = ParameterIn.PATH,
example = "1"
) @PathVariable long teamId,
@Parameter(
name = "파일 id",
name = "documentId",
description = "삭제할 파일 id 리스트",
in = ParameterIn.QUERY,
required = true,
Expand All @@ -242,13 +204,13 @@ ResponseEntity<?> deleteTrash(
ResponseEntity<?> restore(
@Parameter(hidden = true) Principal principal,
@Parameter(
name = "팀 id",
name = "teamId",
description = "팀 id",
in = ParameterIn.PATH,
example = "1"
) @PathVariable long teamId,
@Parameter(
name = "파일 id",
name = "documentId",
description = "복구할 파일 id 리스트",
in = ParameterIn.QUERY,
required = true,
Expand All @@ -273,7 +235,7 @@ ResponseEntity<?> restore(
ResponseEntity<SuccessResponse<DeletedDocumentsGetResponse>> getTrash(
@Parameter(hidden = true) Principal principal,
@Parameter(
name = "팀 id",
name = "teamId",
description = "팀 id",
in = ParameterIn.PATH,
example = "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public record DocumentCreateRequest(
@NonNull String fileName,
@Schema(description = "파일 url", example = "https://.../tiki.jpg")
@NonNull String fileUrl,
@Schema(description = "파일 key", example = "....jpg")
@NonNull String fileKey,
@Schema(description = "파일 용량", example = "1.23")
double capacity
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class DeletedDocument extends BaseTime {

private String fileUrl;

private String fileKey;

private long teamId;

private double capacity;
Expand Down
32 changes: 9 additions & 23 deletions src/main/java/com/tiki/server/document/entity/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,33 @@ public class Document extends BaseTime {

private String fileUrl;

private String fileKey;

private double capacity;

private long teamId;

private Long folderId;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "block_id")
private TimeBlock timeBlock;

public static Document of(final String fileName, final String fileUrl, final TimeBlock timeBlock) {
return Document.builder()
.fileName(fileName)
.fileUrl(fileUrl)
.capacity(0) // TODO : 타임 블록 생성 api 수정 후 제거 예정
.teamId(1) // TODO : 타임 블록 생성 api 수정 후 제거 예정
.folderId(null) // TODO : 타임 블록 생성 api 수정 후 제거 예정
.timeBlock(timeBlock)
.build();
}

public static Document of(final DocumentCreateRequest request, final long teamId, final Long folderId) {
return Document.builder()
.fileName(request.fileName())
.fileUrl(request.fileUrl())
.capacity(request.capacity())
.fileKey(request.fileKey())
.teamId(teamId)
.folderId(folderId)
.timeBlock(null) // TODO : 타임 블록 생성 api 수정 후 제거 예정
.build();
}

public static Document restore(final String fileName, final String fileUrl,
final double capacity, final long teamId) {
public static Document restore(final DeletedDocument deletedDocument) {
return Document.builder()
.fileName(fileName)
.fileUrl(fileUrl)
.capacity(capacity)
.teamId(teamId)
.fileName(deletedDocument.getFileName())
.fileUrl(deletedDocument.getFileUrl())
.capacity(deletedDocument.getCapacity())
.fileKey(deletedDocument.getFileKey())
.teamId(deletedDocument.getTeamId())
.folderId(null)
.timeBlock(null)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,10 @@
import com.tiki.server.document.entity.Document;

public interface DocumentRepository extends JpaRepository<Document, Long> {

List<Document> findAllByTimeBlockId(long timeBlockId);

List<Document> findAllByFolderId(long folderId);

@Query("select d from Document d join fetch d.timeBlock t "
+ "where t.team.id = :teamId and t.accessiblePosition = :position order by d.createdAt asc")
List<Document> findAllByTeamIdAndAccessiblePosition(long teamId, Position position);

@Query("select d from Document d join d.timeBlock t where t.team.id = :teamId")
List<Document> findAllByTeamId(long teamId);

@Query("select d from Document d join fetch d.timeBlock where d.id = :documentId")
Document findByIdWithTimeBlock(long documentId);

void deleteAllByTimeBlockId(long timeBlockId);

List<Document> findAllByTeamIdAndFolderIdOrderByCreatedAtDesc(long teamId, Long folderId);

Optional<Document> findByIdAndTeamId(long id, long teamId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.tiki.server.document.entity.DeletedDocument;
import com.tiki.server.document.entity.Document;
import com.tiki.server.document.exception.DocumentException;
import com.tiki.server.documenttimeblockmanager.adapter.DTBAdapter;
import com.tiki.server.external.util.S3Handler;
import com.tiki.server.folder.adapter.FolderFinder;
import com.tiki.server.folder.entity.Folder;
Expand All @@ -41,21 +42,15 @@ public class DocumentService {
private final MemberTeamManagerFinder memberTeamManagerFinder;
private final DeletedDocumentAdapter deletedDocumentAdapter;
private final TeamFinder teamFinder;
private final DTBAdapter dtbAdapter;
private final S3Handler s3Handler;

public DocumentsGetResponse getAllDocuments(final long memberId, final long teamId, final String type) {
MemberTeamManager memberTeamManager = memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
Position accessiblePosition = Position.getAccessiblePosition(type);
memberTeamManager.checkMemberAccessible(accessiblePosition);
return getAllDocumentsByType(teamId, accessiblePosition);
}

@Transactional
public void deleteDocument(final long memberId, final long teamId, final long documentId) {
MemberTeamManager memberTeamManager = memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
Document document = documentFinder.findByIdWithTimeBlock(documentId);
memberTeamManager.checkMemberAccessible(document.getTimeBlock().getAccessiblePosition());
documentDeleter.delete(document);
List<Document> documents = documentFinder.findAllByTeamId(teamId);
return DocumentsGetResponse.from(documents);
}

@Transactional
Expand All @@ -77,6 +72,7 @@ public DocumentsGetResponse get(final long memberId, final long teamId, final Lo
public void delete(final long memberId, final long teamId, final List<Long> documentIds) {
memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
List<Document> documents = documentFinder.findAllByIdAndTeamId(documentIds, teamId);
dtbAdapter.deleteAllByDocuments(documentIds);
deletedDocumentAdapter.save(documents);
documentDeleter.deleteAll(documents);
}
Expand All @@ -86,7 +82,7 @@ public void deleteTrash(final long memberId, final long teamId, final List<Long>
memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
List<DeletedDocument> deletedDocuments = deletedDocumentAdapter.get(documentIds, teamId);
restoreTeamUsage(teamId, deletedDocuments);
deletedDocuments.forEach(deletedDocument -> s3Handler.deleteFile(deletedDocument.getFileName()));
deletedDocuments.forEach(deletedDocument -> s3Handler.deleteFile(deletedDocument.getFileKey()));
deletedDocumentAdapter.deleteAll(deletedDocuments);
}

Expand All @@ -104,11 +100,6 @@ public DeletedDocumentsGetResponse getTrash(final long memberId, final long team
return DeletedDocumentsGetResponse.from(deletedDocuments);
}

private DocumentsGetResponse getAllDocumentsByType(final long teamId, final Position accessiblePosition) {
List<Document> documents = documentFinder.findAllByTeamIdAndAccessiblePosition(teamId, accessiblePosition);
return DocumentsGetResponse.from(documents);
}

private void validateFolder(final Long folderId, final long teamId) {
if (folderId == null) {
return;
Expand Down
Loading

0 comments on commit 7dc8a09

Please sign in to comment.