File size: 2,464 Bytes
47b5f0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from fastapi import HTTPException, UploadFile
from fastapi.responses import JSONResponse

from app.infrastructure.repository.document_handeler_repository import (
    DocumentHandelerRepository,
)
from app.modules.documentHandeler.features.createEmbeddings_feature import (
    CreateEmbeddingsFeature,
)
from app.modules.documentHandeler.features.deleteDocument_feature import (
    DeleteDocumentFeature,
)
from app.modules.documentHandeler.features.extractText_feature import ExtractTextFeature
from app.modules.documentHandeler.features.getAllChunkedText_feature import (
    GetAllChunkedTextFeature,
)


class DocumentHandelerController:

    def __init__(
        self,
        delete_document_feature: DeleteDocumentFeature,
        create_embeddings_feature: CreateEmbeddingsFeature,
        get_all_chunked_text_feature: GetAllChunkedTextFeature,
    ):
        self.create_embeddings_feature = create_embeddings_feature
        self.delete_document_feature = delete_document_feature
        self.get_all_chunked_text_feature = get_all_chunked_text_feature

    async def handle_file_upload(self, file: UploadFile) -> JSONResponse:
        try:

            text_file = await ExtractTextFeature.extract_text_from_pdf(file)
            result = await self.create_embeddings_feature.create_embeddings(
                text_file, file.filename
            )

            return JSONResponse(status_code=200, content=result.model_dump())

        except Exception as e:
            raise HTTPException(status_code=500, detail="Probelm on controller")

    async def delete_document(self, text: str) -> JSONResponse:
        try:
            result = await self.delete_document_feature.delete_document_by_filename(
                text
            )
            if result:
                return JSONResponse(
                    status_code=200, content={"message": "Document deleted"}
                )
            return JSONResponse(
                status_code=404, content={"message": "Document not found"}
            )
            
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

    async def get_all_chunks(self) -> JSONResponse:
        try:
            result = await self.get_all_chunked_text_feature.get_all_chunked_text()
            return JSONResponse(status_code=200, content=result.model_dump())

        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))