Skip to content

Commit

Permalink
Fix: member 가 영속성 컨텍스트에 존재하지 않은 채로 조회해 생기는 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
yugyeom-ghim committed Nov 11, 2024
1 parent 835090f commit aa30af5
Show file tree
Hide file tree
Showing 24 changed files with 167 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import notai.document.domain.Document;
import notai.document.domain.DocumentRepository;
import notai.member.domain.Member;
import notai.member.domain.MemberRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,12 +23,14 @@ public class AnnotationQueryService {

private final AnnotationRepository annotationRepository;
private final DocumentRepository documentRepository;
private final MemberRepository memberRepository;

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

List<Annotation> annotations = annotationRepository.findByDocumentIdAndPageNumberIn(documentId, pageNumbers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import notai.document.domain.Document;
import notai.document.domain.DocumentRepository;
import notai.member.domain.Member;
import notai.member.domain.MemberRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,12 +17,14 @@ public class AnnotationService {

private final AnnotationRepository annotationRepository;
private final DocumentRepository documentRepository;
private final MemberRepository memberRepository;

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

Annotation annotation = new Annotation(document, pageNumber, x, y, width, height, content);
Expand All @@ -31,18 +34,20 @@ public AnnotationResponse createAnnotation(

@Transactional
public AnnotationResponse updateAnnotation(
Member member, Long documentId, Long annotationId, int x, int y, int width, int height, String content
Long memberId, Long documentId, Long annotationId, int x, int y, int width, int height, String content
) {
Document document = documentRepository.getById(documentId);
Member member = memberRepository.getById(memberId);
document.validateOwner(member);
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) {
public void deleteAnnotation(Long memberId, Long documentId, Long annotationId) {
Document document = documentRepository.getById(documentId);
Member member = memberRepository.getById(memberId);
document.validateOwner(member);
Annotation annotation = annotationRepository.getById(annotationId);
annotationRepository.delete(annotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import notai.annotation.presentation.request.CreateAnnotationRequest;
import notai.annotation.presentation.response.AnnotationResponse;
import notai.auth.Auth;
import notai.member.domain.Member;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -24,11 +23,11 @@ public class AnnotationController {

@PostMapping
public ResponseEntity<AnnotationResponse> createAnnotation(
@Auth Member member, @PathVariable Long documentId, @RequestBody @Valid CreateAnnotationRequest request
@Auth Long memberId, @PathVariable Long documentId, @RequestBody @Valid CreateAnnotationRequest request
) {

AnnotationResponse response = annotationService.createAnnotation(
member,
memberId,
documentId,
request.pageNumber(),
request.x(),
Expand All @@ -44,10 +43,10 @@ public ResponseEntity<AnnotationResponse> createAnnotation(

@GetMapping
public ResponseEntity<List<AnnotationResponse>> getAnnotations(
@Auth Member member, @PathVariable Long documentId, @RequestParam List<Integer> pageNumbers
@Auth Long memberId, @PathVariable Long documentId, @RequestParam List<Integer> pageNumbers
) {
List<AnnotationResponse> response = annotationQueryService.getAnnotationsByDocumentAndPageNumbers(
member,
memberId,
documentId,
pageNumbers
);
Expand All @@ -57,14 +56,14 @@ public ResponseEntity<List<AnnotationResponse>> getAnnotations(

@PutMapping("/{annotationId}")
public ResponseEntity<AnnotationResponse> updateAnnotation(
@Auth Member member,
@Auth Long memberId,
@PathVariable Long documentId,
@PathVariable Long annotationId,
@RequestBody @Valid CreateAnnotationRequest request
) {

AnnotationResponse response = annotationService.updateAnnotation(
member,
memberId,
documentId,
annotationId,
request.x(),
Expand All @@ -79,10 +78,10 @@ public ResponseEntity<AnnotationResponse> updateAnnotation(

@DeleteMapping("/{annotationId}")
public ResponseEntity<Void> deleteAnnotation(
@Auth Member member, @PathVariable Long documentId, @PathVariable Long annotationId
@Auth Long memberId, @PathVariable Long documentId, @PathVariable Long annotationId
) {

annotationService.deleteAnnotation(member, documentId, annotationId);
annotationService.deleteAnnotation(memberId, documentId, annotationId);
return new ResponseEntity<>(HttpStatus.OK);
}
}
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 @@ -25,15 +25,14 @@ public boolean supportsParameter(MethodParameter parameter) {
}

@Override
@Transactional(readOnly = true)
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();
}
}
27 changes: 16 additions & 11 deletions src/main/java/notai/document/application/DocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -28,32 +29,33 @@ public class DocumentService {
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 +68,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 +83,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 +106,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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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,
Expand All @@ -45,9 +45,9 @@ public ResponseEntity<DocumentSaveResponse> saveDocument(

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 +56,12 @@ 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();
}
}
10 changes: 5 additions & 5 deletions src/main/java/notai/folder/application/FolderQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ 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 folderId) {
List<Folder> folders = getFoldersWithMemberAndParent(memberId, folderId);
// document read
return folders.stream().map(this::getFolderResult).toList();
}

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

private FolderFindResult getFolderResult(Folder folder) {
Expand Down
Loading

0 comments on commit aa30af5

Please sign in to comment.