Skip to content

Commit

Permalink
Revert "Refactor: 폴더 삭제 요청시 EntityManager로 인해 발생한 이슈 해결 및 폴더 조회 요청 개선 (
Browse files Browse the repository at this point in the history
…#33)" (#34)

This reverts commit 0fd2383.
  • Loading branch information
yugyeom-ghim authored Nov 11, 2024
1 parent 0fd2383 commit 5be216f
Show file tree
Hide file tree
Showing 30 changed files with 243 additions and 462 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import notai.annotation.domain.AnnotationRepository;
import notai.annotation.presentation.response.AnnotationResponse;
import notai.common.exception.type.NotFoundException;
import notai.document.domain.Document;
import notai.document.domain.DocumentRepository;
import notai.member.domain.Member;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -24,11 +22,8 @@ public class AnnotationQueryService {
private final DocumentRepository documentRepository;

@Transactional(readOnly = true)
public List<AnnotationResponse> getAnnotationsByDocumentAndPageNumbers(
Member member, Long documentId, List<Integer> pageNumbers
) {
Document document = documentRepository.getById(documentId);
document.validateOwner(member);
public List<AnnotationResponse> getAnnotationsByDocumentAndPageNumbers(Long documentId, List<Integer> pageNumbers) {
documentRepository.getById(documentId);

List<Annotation> annotations = annotationRepository.findByDocumentIdAndPageNumberIn(documentId, pageNumbers);
if (annotations.isEmpty()) {
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/notai/annotation/application/AnnotationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import notai.annotation.presentation.response.AnnotationResponse;
import notai.document.domain.Document;
import notai.document.domain.DocumentRepository;
import notai.member.domain.Member;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -18,32 +17,25 @@ public class AnnotationService {
private final DocumentRepository documentRepository;

@Transactional
public AnnotationResponse createAnnotation(
Member member, Long documentId, int pageNumber, int x, int y, int width, int height, String content
) {
public AnnotationResponse createAnnotation(Long documentId, int pageNumber, int x, int y, int width, int height, String content) {
Document document = documentRepository.getById(documentId);
document.validateOwner(member);

Annotation annotation = new Annotation(document, pageNumber, x, y, width, height, content);
Annotation savedAnnotation = annotationRepository.save(annotation);
return AnnotationResponse.from(savedAnnotation);
}

@Transactional
public AnnotationResponse updateAnnotation(
Member member, Long documentId, Long annotationId, int x, int y, int width, int height, String content
) {
Document document = documentRepository.getById(documentId);
document.validateOwner(member);
public AnnotationResponse updateAnnotation(Long documentId, Long annotationId, int x, int y, int width, int height, String content) {
documentRepository.getById(documentId);
Annotation annotation = annotationRepository.getById(annotationId);
annotation.updateAnnotation(x, y, width, height, content);
return AnnotationResponse.from(annotation);
}

@Transactional
public void deleteAnnotation(Member member, Long documentId, Long annotationId) {
Document document = documentRepository.getById(documentId);
document.validateOwner(member);
public void deleteAnnotation(Long documentId, Long annotationId) {
documentRepository.getById(documentId);
Annotation annotation = annotationRepository.getById(annotationId);
annotationRepository.delete(annotation);
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/notai/auth/AuthArgumentResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import notai.member.domain.Member;
import notai.member.domain.MemberRepository;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
Expand All @@ -24,14 +23,14 @@ public boolean supportsParameter(MethodParameter parameter) {
}

@Override
public Member resolveArgument(
public Long resolveArgument(
MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory
) {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
Long memberId = (Long) request.getAttribute("memberId");
return memberRepository.getById(memberId);
return memberRepository.getById(memberId).getId();
}
}
13 changes: 7 additions & 6 deletions src/main/java/notai/common/exception/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum ErrorMessages {

// folder
FOLDER_NOT_FOUND("폴더를 찾을 수 없습니다."),
FOLDER_AND_DOCUMENT_INVALID_RESPONSE("허용하지 않는 데이터 타입입니다."), // llm task

// llm task
LLM_TASK_LOG_NOT_FOUND("AI 작업 기록을 찾을 수 없습니다."),
LLM_TASK_RESULT_ERROR("AI 요약 및 문제 생성 중에 문제가 발생했습니다."),

Expand All @@ -36,15 +37,15 @@ public enum ErrorMessages {
RECORDING_NOT_FOUND("녹음 파일을 찾을 수 없습니다."),

// external api call
KAKAO_API_ERROR("카카오 API 호출에 예외가 발생했습니다."),
KAKAO_API_ERROR("카카오 API 호출에 예외가 발생했습니다."),
AI_SERVER_ERROR("AI 서버 API 호출에 예외가 발생했습니다."),
SLACK_API_ERROR("슬랙 API 호출에 예외가 발생했습니다."),

// auth
INVALID_ACCESS_TOKEN("유효하지 않은 토큰입니다."),
INVALID_REFRESH_TOKEN("유요하지 않은 Refresh Token입니다."),
EXPIRED_REFRESH_TOKEN("만료된 Refresh Token입니다."),
INVALID_LOGIN_TYPE("지원하지 않는 소셜 로그인 타입입니다."),
INVALID_ACCESS_TOKEN("유효하지 않은 토큰입니다."),
INVALID_REFRESH_TOKEN("유요하지 않은 Refresh Token입니다."),
EXPIRED_REFRESH_TOKEN("만료된 Refresh Token입니다."),
INVALID_LOGIN_TYPE("지원하지 않는 소셜 로그인 타입입니다."),
NOTFOUND_ACCESS_TOKEN("토큰 정보가 존재하지 않습니다."),

// stt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import notai.document.domain.Document;
import notai.document.domain.DocumentRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class DocumentQueryService {

private final DocumentRepository documentRepository;
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/notai/document/application/DocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,51 @@
import notai.folder.domain.Folder;
import notai.folder.domain.FolderRepository;
import notai.member.domain.Member;
import notai.member.domain.MemberRepository;
import notai.ocr.application.OCRService;
import notai.pdf.PdfService;
import notai.pdf.result.PdfSaveResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class DocumentService {

private final PdfService pdfService;
private final OCRService ocrService;
private final DocumentRepository documentRepository;
private final FolderRepository folderRepository;
private final MemberRepository memberRepository;

private static final Long ROOT_FOLDER_ID = -1L;


public DocumentSaveResult saveDocument(
Member member, Long folderId, MultipartFile pdfFile, DocumentSaveRequest documentSaveRequest
Long memberId, Long folderId, MultipartFile pdfFile, DocumentSaveRequest documentSaveRequest
) {
PdfSaveResult pdfSaveResult = pdfService.savePdf(pdfFile);
Document document = saveAndReturnDocument(member, folderId, documentSaveRequest, pdfSaveResult);
Document document = saveAndReturnDocument(memberId, folderId, documentSaveRequest, pdfSaveResult);
ocrService.saveOCR(document, pdfSaveResult.pdf());
return DocumentSaveResult.of(document.getId(), document.getName(), document.getUrl());
}

public DocumentSaveResult saveRootDocument(
Member member, MultipartFile pdfFile, DocumentSaveRequest documentSaveRequest
Long memberId, MultipartFile pdfFile, DocumentSaveRequest documentSaveRequest
) {
PdfSaveResult pdfSaveResult = pdfService.savePdf(pdfFile);
Document document = saveAndReturnRootDocument(member, documentSaveRequest, pdfSaveResult);
Document document = saveAndReturnRootDocument(memberId, documentSaveRequest, pdfSaveResult);
ocrService.saveOCR(document, pdfSaveResult.pdf());
return DocumentSaveResult.of(document.getId(), document.getName(), document.getUrl());
}

public DocumentUpdateResult updateDocument(
Member member, Long folderId, Long documentId, DocumentUpdateRequest documentUpdateRequest
Long memberId, Long folderId, Long documentId, DocumentUpdateRequest documentUpdateRequest
) {
Document document = documentRepository.getById(documentId);
Member member = memberRepository.getById(memberId);

document.validateOwner(member);

Expand All @@ -66,9 +67,10 @@ public DocumentUpdateResult updateDocument(
}

public void deleteDocument(
Member member, Long folderId, Long documentId
Long memberId, Long folderId, Long documentId
) {
Document document = documentRepository.getById(documentId);
Member member = memberRepository.getById(memberId);

document.validateOwner(member);

Expand All @@ -80,17 +82,18 @@ public void deleteDocument(
}

public void deleteAllByFolder(
Member member, Folder folder
Long memberId, Folder folder
) {
List<Document> documents = documentRepository.findAllByFolderId(folder.getId());
for (Document document : documents) {
deleteDocument(member, folder.getId(), document.getId());
deleteDocument(memberId, folder.getId(), document.getId());
}
}

private Document saveAndReturnDocument(
Member member, Long folderId, DocumentSaveRequest documentSaveRequest, PdfSaveResult pdfSaveResult
Long memberId, Long folderId, DocumentSaveRequest documentSaveRequest, PdfSaveResult pdfSaveResult
) {
Member member = memberRepository.getById(memberId);
Folder folder = folderRepository.getById(folderId);
Document document = new Document(folder,
member,
Expand All @@ -102,8 +105,9 @@ private Document saveAndReturnDocument(
}

private Document saveAndReturnRootDocument(
Member member, DocumentSaveRequest documentSaveRequest, PdfSaveResult pdfSaveResult
Long memberId, DocumentSaveRequest documentSaveRequest, PdfSaveResult pdfSaveResult
) {
Member member = memberRepository.getById(memberId);
Document document = new Document(member,
documentSaveRequest.name(),
pdfSaveResult.pdfUrl(),
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/notai/document/presentation/DocumentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import notai.document.presentation.response.DocumentFindResponse;
import notai.document.presentation.response.DocumentSaveResponse;
import notai.document.presentation.response.DocumentUpdateResponse;
import notai.member.domain.Member;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -36,18 +35,18 @@ public class DocumentController {

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<DocumentSaveResponse> saveDocument(
@Auth Member member,
@Auth Long memberId,
@PathVariable Long folderId,
@Parameter(content = @Content(mediaType = MediaType.APPLICATION_PDF_VALUE)) @RequestPart
MultipartFile pdfFile,
@Parameter(content = @Content(mediaType = MediaType.APPLICATION_PDF_VALUE))
@RequestPart MultipartFile pdfFile,
@RequestPart DocumentSaveRequest documentSaveRequest
) {

DocumentSaveResult documentSaveResult;
if (folderId.equals(ROOT_FOLDER_ID)) {
documentSaveResult = documentService.saveRootDocument(member, pdfFile, documentSaveRequest);
documentSaveResult = documentService.saveRootDocument(memberId, pdfFile, documentSaveRequest);
} else {
documentSaveResult = documentService.saveDocument(member, folderId, pdfFile, documentSaveRequest);
documentSaveResult = documentService.saveDocument(memberId, folderId, pdfFile, documentSaveRequest);
}
DocumentSaveResponse response = DocumentSaveResponse.from(documentSaveResult);
String url = String.format(FOLDER_URL_FORMAT, folderId, response.id());
Expand All @@ -56,12 +55,13 @@ public ResponseEntity<DocumentSaveResponse> saveDocument(

@PutMapping(value = "/{id}")
public ResponseEntity<DocumentUpdateResponse> updateDocument(
@Auth Member member,
@Auth Long memberId,
@PathVariable Long folderId,
@PathVariable Long id,
@RequestBody DocumentUpdateRequest documentUpdateRequest
) {
DocumentUpdateResult documentUpdateResult = documentService.updateDocument(member,
DocumentUpdateResult documentUpdateResult = documentService.updateDocument(
memberId,
folderId,
id,
documentUpdateRequest
Expand All @@ -72,11 +72,11 @@ public ResponseEntity<DocumentUpdateResponse> updateDocument(

@GetMapping
public ResponseEntity<List<DocumentFindResponse>> getDocuments(
@Auth Member member, @PathVariable Long folderId
@Auth Long memberId, @PathVariable Long folderId
) {
List<DocumentFindResult> documentResults;
if (folderId.equals(ROOT_FOLDER_ID)) {
documentResults = documentQueryService.findRootDocuments(member.getId());
documentResults = documentQueryService.findRootDocuments(memberId);
} else {
documentResults = documentQueryService.findDocuments(folderId);
}
Expand All @@ -87,9 +87,9 @@ public ResponseEntity<List<DocumentFindResponse>> getDocuments(

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteDocument(
@Auth Member member, @PathVariable Long folderId, @PathVariable Long id
@Auth Long memberId, @PathVariable Long folderId, @PathVariable Long id
) {
documentService.deleteDocument(member, folderId, id);
documentService.deleteDocument(memberId, folderId, id);
return ResponseEntity.noContent().build();
}
}
16 changes: 6 additions & 10 deletions src/main/java/notai/folder/application/FolderQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@
import notai.folder.application.result.FolderFindResult;
import notai.folder.domain.Folder;
import notai.folder.domain.FolderRepository;
import notai.member.domain.Member;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class FolderQueryService {

private final FolderRepository folderRepository;
private static final Long ROOT_ID = -1L;

public List<FolderFindResult> getFolders(Member member, Long folderId) {
List<Folder> folders = getFoldersWithMemberAndParent(member, folderId);
public List<FolderFindResult> getFolders(Long memberId, Long parentFolderId) {
List<Folder> folders = getFoldersWithMemberAndParent(memberId, parentFolderId);
// document read
return folders.stream().map(this::getFolderResult).toList();
}

private List<Folder> getFoldersWithMemberAndParent(Member member, Long folderId) {
if (folderId == null || folderId.equals(ROOT_ID)) {
return folderRepository.findAllByMemberIdAndParentFolderIsNull(member.getId());
private List<Folder> getFoldersWithMemberAndParent(Long memberId, Long parentFolderId) {
if (parentFolderId == null) {
return folderRepository.findAllByMemberIdAndParentFolderIsNull(memberId);
}
return folderRepository.findAllByMemberIdAndParentFolderId(member.getId(), folderId);
return folderRepository.findAllByMemberIdAndParentFolderId(memberId, parentFolderId);
}

private FolderFindResult getFolderResult(Folder folder) {
Expand Down
Loading

0 comments on commit 5be216f

Please sign in to comment.