Skip to content

Commit

Permalink
Merge branch 'release/0.0.3' of https://github.com/29ana-notai/Team29_BE
Browse files Browse the repository at this point in the history
 into release/0.0.3
  • Loading branch information
yugyeom-ghim committed Nov 11, 2024
2 parents b10bc40 + 7909d8c commit 835090f
Show file tree
Hide file tree
Showing 30 changed files with 473 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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 @@ -22,8 +24,11 @@ public class AnnotationQueryService {
private final DocumentRepository documentRepository;

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

List<Annotation> annotations = annotationRepository.findByDocumentIdAndPageNumberIn(documentId, pageNumbers);
if (annotations.isEmpty()) {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/notai/annotation/application/AnnotationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 @@ -17,25 +18,32 @@ public class AnnotationService {
private final DocumentRepository documentRepository;

@Transactional
public AnnotationResponse createAnnotation(Long documentId, int pageNumber, int x, int y, int width, int height, String content) {
public AnnotationResponse createAnnotation(
Member member, 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(Long documentId, Long annotationId, int x, int y, int width, int height, String content) {
documentRepository.getById(documentId);
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);
Annotation annotation = annotationRepository.getById(annotationId);
annotation.updateAnnotation(x, y, width, height, content);
return AnnotationResponse.from(annotation);
}

@Transactional
public void deleteAnnotation(Long documentId, Long annotationId) {
documentRepository.getById(documentId);
public void deleteAnnotation(Member member, Long documentId, Long annotationId) {
Document document = documentRepository.getById(documentId);
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 @@ -6,6 +6,8 @@
import notai.annotation.application.AnnotationService;
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 @@ -22,10 +24,12 @@ public class AnnotationController {

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

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

@GetMapping
public ResponseEntity<List<AnnotationResponse>> getAnnotations(
@PathVariable Long documentId, @RequestParam List<Integer> pageNumbers
@Auth Member member, @PathVariable Long documentId, @RequestParam List<Integer> pageNumbers
) {

List<AnnotationResponse> response = annotationQueryService.getAnnotationsByDocumentAndPageNumbers(
member,
documentId,
pageNumbers
);
Expand All @@ -53,12 +57,15 @@ public ResponseEntity<List<AnnotationResponse>> getAnnotations(

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

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

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

annotationService.deleteAnnotation(documentId, annotationId);
annotationService.deleteAnnotation(member, documentId, annotationId);
return new ResponseEntity<>(HttpStatus.OK);
}
}
13 changes: 6 additions & 7 deletions src/main/java/notai/common/exception/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public enum ErrorMessages {

// folder
FOLDER_NOT_FOUND("폴더를 찾을 수 없습니다."),

// llm task
FOLDER_AND_DOCUMENT_INVALID_RESPONSE("허용하지 않는 데이터 타입입니다."), // llm task
LLM_TASK_LOG_NOT_FOUND("AI 작업 기록을 찾을 수 없습니다."),
LLM_TASK_RESULT_ERROR("AI 요약 및 문제 생성 중에 문제가 발생했습니다."),

Expand All @@ -37,15 +36,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,11 +5,13 @@
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: 12 additions & 16 deletions src/main/java/notai/document/application/DocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,50 @@
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(
Long memberId, Long folderId, MultipartFile pdfFile, DocumentSaveRequest documentSaveRequest
Member member, Long folderId, MultipartFile pdfFile, DocumentSaveRequest documentSaveRequest
) {
PdfSaveResult pdfSaveResult = pdfService.savePdf(pdfFile);
Document document = saveAndReturnDocument(memberId, folderId, documentSaveRequest, pdfSaveResult);
Document document = saveAndReturnDocument(member, folderId, documentSaveRequest, pdfSaveResult);
ocrService.saveOCR(document, pdfSaveResult.pdf());
return DocumentSaveResult.of(document.getId(), document.getName(), document.getUrl());
}

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

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

document.validateOwner(member);

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

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

document.validateOwner(member);

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

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

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

private Document saveAndReturnRootDocument(
Long memberId, DocumentSaveRequest documentSaveRequest, PdfSaveResult pdfSaveResult
Member member, 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,6 +14,7 @@
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 @@ -35,18 +36,18 @@ public class DocumentController {

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<DocumentSaveResponse> saveDocument(
@Auth Long memberId,
@Auth Member member,
@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(memberId, pdfFile, documentSaveRequest);
documentSaveResult = documentService.saveRootDocument(member, pdfFile, documentSaveRequest);
} else {
documentSaveResult = documentService.saveDocument(memberId, folderId, pdfFile, documentSaveRequest);
documentSaveResult = documentService.saveDocument(member, folderId, pdfFile, documentSaveRequest);
}
DocumentSaveResponse response = DocumentSaveResponse.from(documentSaveResult);
String url = String.format(FOLDER_URL_FORMAT, folderId, response.id());
Expand All @@ -55,13 +56,12 @@ public ResponseEntity<DocumentSaveResponse> saveDocument(

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

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

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteDocument(
@Auth Long memberId, @PathVariable Long folderId, @PathVariable Long id
@Auth Member member, @PathVariable Long folderId, @PathVariable Long id
) {
documentService.deleteDocument(memberId, folderId, id);
documentService.deleteDocument(member, folderId, id);
return ResponseEntity.noContent().build();
}
}
16 changes: 10 additions & 6 deletions src/main/java/notai/folder/application/FolderQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@
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(Long memberId, Long parentFolderId) {
List<Folder> folders = getFoldersWithMemberAndParent(memberId, parentFolderId);
public List<FolderFindResult> getFolders(Member member, Long folderId) {
List<Folder> folders = getFoldersWithMemberAndParent(member, folderId);
// document read
return folders.stream().map(this::getFolderResult).toList();
}

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

private FolderFindResult getFolderResult(Folder folder) {
Expand Down
Loading

0 comments on commit 835090f

Please sign in to comment.