Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Team-Tiki/TIKI_SERVER in…
Browse files Browse the repository at this point in the history
…to feat/#177-kick-out-member
  • Loading branch information
paragon0107 committed Nov 29, 2024
2 parents f34882a + d786c06 commit 361fc7e
Show file tree
Hide file tree
Showing 24 changed files with 479 additions and 82 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/tiki/server/common/handler/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tiki.server.auth.exception.AuthException;
import com.tiki.server.common.dto.ErrorCodeResponse;
import com.tiki.server.emailverification.exception.EmailVerificationException;
import com.tiki.server.folder.exception.FolderException;
import com.tiki.server.note.exception.NoteException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -79,14 +80,21 @@ public ResponseEntity<BaseResponse> externalException(ExternalException exceptio
}

@ExceptionHandler(EmailVerificationException.class)
public ResponseEntity<BaseResponse> MailException(EmailVerificationException exception) {
public ResponseEntity<BaseResponse> mailException(EmailVerificationException exception) {
log.error(exception.getMessage());
val errorCode = exception.getErrorCode();
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage()));
}

@ExceptionHandler(FolderException.class)
public ResponseEntity<BaseResponse> folderException(FolderException exception) {
log.error(exception.getMessage());
val errorCode = exception.getErrorCode();
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage()));
}

@ExceptionHandler(AuthException.class)
public ResponseEntity<BaseResponse> AuthException(AuthException exception) {
public ResponseEntity<BaseResponse> authException(AuthException exception) {
log.error(exception.getMessage());
val errorCode = exception.getErrorCode();
return ResponseEntity.status(errorCode.getHttpStatus()).body(
Expand All @@ -101,7 +109,7 @@ public ResponseEntity<BaseResponse> httpMessageNotReadableException(HttpMessageN
}

@ExceptionHandler(Exception.class)
public ResponseEntity<BaseResponse> Exception(Exception exception) {
public ResponseEntity<BaseResponse> exception(Exception exception) {
log.error(exception.getMessage());
val errorCode = UNCAUGHT_SERVER_EXCEPTION;
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.tiki.server.document.adapter;

import static com.tiki.server.document.message.ErrorCode.INVALID_DOCUMENT;

import java.util.List;

import com.tiki.server.common.support.RepositoryAdapter;
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.document.repository.DeletedDocumentRepository;

import lombok.RequiredArgsConstructor;

@RepositoryAdapter
@RequiredArgsConstructor
public class DeletedDocumentAdapter {

private final DeletedDocumentRepository deletedDocumentRepository;

public List<DeletedDocument> get(final long teamId) {
return deletedDocumentRepository.findAllByTeamId(teamId);
}

public void save(final List<Document> documents) {
documents.forEach(document -> deletedDocumentRepository.save(create(document, document.getTeamId())));
}

public List<DeletedDocument> get(final List<Long> deletedDocumentIds, final long teamId) {
return deletedDocumentIds.stream()
.map(id -> find(id, teamId))
.toList();
}

public void deleteAll(final List<DeletedDocument> deletedDocuments) {
deletedDocumentRepository.deleteAll(deletedDocuments);
}

private DeletedDocument create(final Document document, final long teamId) {
return DeletedDocument.of(document.getFileName(), document.getFileUrl(), teamId, document.getCapacity());
}

private DeletedDocument find(final long id, final long teamId) {
return deletedDocumentRepository.findByIdAndTeamId(id, teamId)
.orElseThrow(() -> new DocumentException(INVALID_DOCUMENT));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tiki.server.document.adapter;

import static com.tiki.server.document.message.ErrorCode.INVALID_DOCUMENT;
import static com.tiki.server.folder.constant.Constant.ROOT_PATH;

import java.util.List;
import java.util.Objects;
Expand All @@ -21,6 +20,12 @@ public class DocumentFinder {

private final DocumentRepository documentRepository;

public List<Document> findAllByIdAndTeamId(final List<Long> documentIds, final long teamId) {
return documentIds.stream()
.map(id -> findByIdAndTeamId(id, teamId))
.toList();
}

public Document findByIdOrElseThrow(final long documentId) {
return documentRepository.findById(documentId).orElseThrow(() -> new DocumentException(INVALID_DOCUMENT));
}
Expand Down Expand Up @@ -52,4 +57,13 @@ public boolean existsById(Long timeBlockId) {
public List<Document> findByTeamIdAndFolderId(final long teamId, final Long folderId) {
return documentRepository.findAllByTeamIdAndFolderIdOrderByCreatedAtDesc(teamId, folderId);
}

public List<Document> findAllByFolderId(final long folderId) {
return documentRepository.findAllByFolderId(folderId);
}

private Document findByIdAndTeamId(long documentId, long teamId) {
return documentRepository.findByIdAndTeamId(documentId, teamId)
.orElseThrow(() -> new DocumentException(INVALID_DOCUMENT));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.tiki.server.document.adapter;

import java.util.List;

import com.tiki.server.common.support.RepositoryAdapter;
import com.tiki.server.document.entity.DeletedDocument;
import com.tiki.server.document.entity.Document;
import com.tiki.server.document.repository.DocumentRepository;

Expand All @@ -12,7 +15,20 @@ public class DocumentSaver {

private final DocumentRepository documentRepository;

public Document save(Document document) {
public Document save(final Document document) {
return documentRepository.save(document);
}

public void restore(final List<DeletedDocument> deletedDocuments) {
deletedDocuments.forEach(document -> documentRepository.save(create(document)));
}

private Document create(final DeletedDocument deletedDocument) {
return Document.restore(
deletedDocument.getFileName(),
deletedDocument.getFileUrl(),
deletedDocument.getCapacity(),
deletedDocument.getTeamId()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import static com.tiki.server.document.message.SuccessMessage.SUCCESS_CREATE_DOCUMENTS;
import static com.tiki.server.document.message.SuccessMessage.SUCCESS_GET_DOCUMENTS;
import static com.tiki.server.document.message.SuccessMessage.SUCCESS_GET_TRASH;

import java.security.Principal;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -20,6 +21,7 @@
import com.tiki.server.common.support.UriGenerator;
import com.tiki.server.document.controller.docs.DocumentControllerDocs;
import com.tiki.server.document.dto.request.DocumentsCreateRequest;
import com.tiki.server.document.dto.response.DeletedDocumentsGetResponse;
import com.tiki.server.document.dto.response.DocumentsCreateResponse;
import com.tiki.server.document.dto.response.DocumentsGetResponse;
import com.tiki.server.document.service.DocumentService;
Expand All @@ -36,9 +38,9 @@ public class DocumentController implements DocumentControllerDocs {
@Override
@GetMapping("/documents/team/{teamId}/timeline")
public ResponseEntity<SuccessResponse<DocumentsGetResponse>> getAllDocuments(
Principal principal,
@PathVariable long teamId,
@RequestParam String type
final Principal principal,
@PathVariable final long teamId,
@RequestParam final String type
) {
long memberId = Long.parseLong(principal.getName());
DocumentsGetResponse response = documentService.getAllDocuments(memberId, teamId, type);
Expand All @@ -48,35 +50,79 @@ public ResponseEntity<SuccessResponse<DocumentsGetResponse>> getAllDocuments(
@Override
@DeleteMapping("/documents/team/{teamId}/document/{documentId}")
public ResponseEntity<?> deleteDocument(
Principal principal,
@PathVariable long teamId,
@PathVariable long documentId
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("/documents")
public ResponseEntity<SuccessResponse<DocumentsCreateResponse>> createDocuments(
Principal principal,
@RequestHeader("team-id") long teamId,
@RequestBody DocumentsCreateRequest request
@PostMapping("/teams/{teamId}/documents")
public ResponseEntity<SuccessResponse<?>> createDocuments(
final Principal principal,
@PathVariable final long teamId,
@RequestParam(required = false) final Long folderId,
@RequestBody final DocumentsCreateRequest request
) {
long memberId = Long.parseLong(principal.getName());
DocumentsCreateResponse response = documentService.createDocuments(memberId, teamId, request);
return ResponseEntity.created(UriGenerator.getUri("api/v1/documents"))
.body(SuccessResponse.success(SUCCESS_CREATE_DOCUMENTS.getMessage(), response));
documentService.createDocuments(memberId, teamId, folderId, request);
return ResponseEntity.created(UriGenerator.getUri("teams/" + teamId + "/documents"))
.body(SuccessResponse.success(SUCCESS_CREATE_DOCUMENTS.getMessage()));
}

@GetMapping("/teams/{teamId}/documents")
public ResponseEntity<SuccessResponse<DocumentsGetResponse>> getDocuments(
final Principal principal,
@PathVariable long teamId,
@RequestParam(required = false) Long folderId
@PathVariable final long teamId,
@RequestParam(required = false) final Long folderId
) {
long memberId = Long.parseLong(principal.getName());
DocumentsGetResponse response = documentService.get(memberId, teamId, folderId);
return ResponseEntity.ok(SuccessResponse.success(SUCCESS_GET_DOCUMENTS.getMessage(), response));
}

@DeleteMapping("/teams/{teamId}/documents")
public ResponseEntity<?> delete(
final Principal principal,
@PathVariable final long teamId,
@RequestParam("documentId") final List<Long> documentIds
) {
long memberId = Long.parseLong(principal.getName());
documentService.delete(memberId, teamId, documentIds);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/teams/{teamId}/trash")
public ResponseEntity<?> deleteTrash(
final Principal principal,
@PathVariable final long teamId,
@RequestParam("documentId") final List<Long> deletedDocumentIds
) {
long memberId = Long.parseLong(principal.getName());
documentService.deleteTrash(memberId, teamId, deletedDocumentIds);
return ResponseEntity.noContent().build();
}

@PostMapping("/teams/{teamId}/trash")
public ResponseEntity<?> restore(
final Principal principal,
@PathVariable final long teamId,
@RequestParam("documentId") final List<Long> deletedDocumentIds
) {
long memberId = Long.parseLong(principal.getName());
documentService.restore(memberId, teamId, deletedDocumentIds);
return ResponseEntity.noContent().build();
}

@GetMapping("/teams/{teamId}/trash")
public ResponseEntity<SuccessResponse<DeletedDocumentsGetResponse>> getTrash(
final Principal principal,
@PathVariable final long teamId
) {
long memberId = Long.parseLong(principal.getName());
DeletedDocumentsGetResponse response = documentService.getTrash(memberId, teamId);
return ResponseEntity.ok(SuccessResponse.success(SUCCESS_GET_TRASH.getMessage(), response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;

public record DocumentsCreateRequest(
List<DocumentCreateRequest> documents,
Long folderId
List<DocumentCreateRequest> documents
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tiki.server.document.dto.response;

import static lombok.AccessLevel.PRIVATE;

import java.util.List;

import com.tiki.server.document.entity.DeletedDocument;

import lombok.Builder;
import lombok.NonNull;

@Builder(access = PRIVATE)
public record DeletedDocumentsGetResponse(
List<DeletedDocumentGetResponse> deletedDocuments
) {

public static DeletedDocumentsGetResponse from(final List<DeletedDocument> deletedDocuments) {
return DeletedDocumentsGetResponse.builder()
.deletedDocuments(deletedDocuments.stream().map(DeletedDocumentGetResponse::from).toList())
.build();
}

@Builder(access = PRIVATE)
private record DeletedDocumentGetResponse(
long documentId,
@NonNull String name,
@NonNull String url,
double capacity
) {

private static DeletedDocumentGetResponse from(final DeletedDocument deletedDocument) {
return DeletedDocumentGetResponse.builder()
.documentId(deletedDocument.getId())
.name(deletedDocument.getFileName())
.url(deletedDocument.getFileUrl())
.capacity(deletedDocument.getCapacity())
.build();
}
}
}
24 changes: 14 additions & 10 deletions src/main/java/com/tiki/server/document/entity/DeletedDocument.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package com.tiki.server.document.entity;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PRIVATE;
import static lombok.AccessLevel.PROTECTED;

import java.time.LocalDate;
import com.tiki.server.common.entity.BaseTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
public class DeletedDocument {
@NoArgsConstructor(access = PROTECTED)
@Builder(access = PRIVATE)
@AllArgsConstructor(access = PRIVATE)
public class DeletedDocument extends BaseTime {

@Id
@GeneratedValue(strategy = IDENTITY)
Expand All @@ -26,18 +31,17 @@ public class DeletedDocument {

private String fileUrl;

@Column(name = "block_id")
private long timeBlockId;
private long teamId;

private LocalDate deletedDate;
private double capacity;

@Builder
public static DeletedDocument of(String fileName, String fileUrl, long timeBlockId, LocalDate deletedDate) {
public static DeletedDocument of(final String fileName, final String fileUrl, final long teamId,
final double capacity) {
return DeletedDocument.builder()
.fileName(fileName)
.fileUrl(fileUrl)
.timeBlockId(timeBlockId)
.deletedDate(deletedDate)
.teamId(teamId)
.capacity(capacity)
.build();
}
}
Loading

0 comments on commit 361fc7e

Please sign in to comment.