diff --git a/delivery1/server/models/file.py b/delivery1/server/models/file.py index bf850e8..0b7f63a 100644 --- a/delivery1/server/models/file.py +++ b/delivery1/server/models/file.py @@ -9,8 +9,8 @@ class File(Base): __tablename__ = 'files' id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) - file_handle: Mapped[str] = mapped_column(String, nullable=False) - document_handle: Mapped[str] = mapped_column(String, nullable=False) + file_handle: Mapped[str] = mapped_column(String, unique=True, nullable=False) + document_handle: Mapped[str] = mapped_column(String, unique=True, nullable=True) name: Mapped[str] = mapped_column(String, nullable=False) created_at: Mapped[int] = mapped_column(Integer, nullable=False) org_id: Mapped[int] = mapped_column(Integer, ForeignKey('organizations.id'), nullable=False) diff --git a/delivery1/server/repository/.gitkeep b/delivery1/server/repository/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/delivery1/server/services/__init__.py b/delivery1/server/services/__init__.py index c40adf9..6412401 100644 --- a/delivery1/server/services/__init__.py +++ b/delivery1/server/services/__init__.py @@ -1,2 +1,3 @@ from .orgs import OrganizationService -from .users import UserService \ No newline at end of file +from .users import UserService +from .files import FileService \ No newline at end of file diff --git a/delivery1/server/services/files.py b/delivery1/server/services/files.py new file mode 100644 index 0000000..4a72d0d --- /dev/null +++ b/delivery1/server/services/files.py @@ -0,0 +1,56 @@ +import os +from datetime import datetime +from typing import List, Type + +from sqlalchemy.orm import Session +from models import File, Organization, User + + +class FileService: + def __init__(self, db: Session): + self.db = db + self.repos = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "repository") + + def create_dummy_file(self, org: Organization, user: User) -> File: + file = File( + file_handle = "dummy_file", + document_handle = "org/dummy_file.txt", + name = "dummy_file", + created_at = int(datetime.now().timestamp()), + org_id = 1, + creator_id = 1, + org = org, + creator = user + ) + + file_path = os.path.join(self.repos, file.document_handle) + with open(file_path, "w") as f: + f.write("Dummy file content") + + self.db.add(file) + self.db.commit() + self.db.refresh(file) + return file + + def get_file(self, file_id: int) -> File | None: + return self.db.query(File).filter(File.id == file_id).first() + + def get_file_by_document_handle(self, document_handle: str) -> File | None: + return self.db.query(File).filter(File.document_handle == document_handle).first() + + def get_file_by_file_handle(self, file_handle: str) -> File | None: + return self.db.query(File).filter(File.file_handle == file_handle).first() + + def list_files(self) -> list[Type[File]]: + return self.db.query(File).all() + + def list_files_in_org(self, org: Organization) -> list[Type[File]]: + return self.db.query(File).filter(File.org_id == org.id).all() + + def delete_file(self, file: File) -> File: + file_path = os.path.join(self.repos, file.document_handle) + os.remove(file_path) + file.document_handle = None + self.db.commit() + self.db.refresh(file) + return file \ No newline at end of file diff --git a/delivery1/server/services/orgs.py b/delivery1/server/services/orgs.py index 2f4019b..bdd668d 100644 --- a/delivery1/server/services/orgs.py +++ b/delivery1/server/services/orgs.py @@ -1,3 +1,5 @@ +import os.path + from sqlalchemy.orm import Session from models import Organization @@ -12,6 +14,11 @@ class OrganizationService: if not user: user = UserService(self.db).create_user(username, full_name, email, public_key) + project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + repos = os.path.join(project_root, "repository") + if not os.path.exists(os.path.join(repos, name)): + os.mkdir(os.path.join(repos, name)) + organization = Organization( name=name, owner_id=user.id, diff --git a/delivery1/server/tests/test_db.py b/delivery1/server/tests/test_db.py index c480a3d..c90c5e2 100644 --- a/delivery1/server/tests/test_db.py +++ b/delivery1/server/tests/test_db.py @@ -1,14 +1,16 @@ +import os from pprint import PrettyPrinter import database.setup_db from database import SessionLocal -from services import OrganizationService, UserService +from services import OrganizationService, UserService, FileService database.setup_db.setup_db() db = SessionLocal() org_service = OrganizationService(db) user_service = UserService(db) +file_service = FileService(db) def test_create_organization(): org = org_service.create_organization( @@ -133,4 +135,78 @@ def test_get_user(): db.delete(org) db.delete(user) + db.commit() + +def test_create_dummy_file(): + + org = org_service.create_organization( + name="org", + username="testuser", + full_name="Test User", + email="test@mail.com", + public_key="abc123" + ) + + file = file_service.create_dummy_file(org, org.owner) + + assert file.file_handle == "dummy_file" + assert file.document_handle == "org/dummy_file.txt" + assert file.name == "dummy_file" + project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + repos = os.path.join(project_root, "repository") + with open(os.path.join(repos, file.document_handle), "r") as f: + assert f.read() == "Dummy file content" + + + os.remove(os.path.join(repos, file.document_handle)) + db.delete(file) + db.delete(org.owner) + db.delete(org) + db.commit() + + +def test_get_file(): + org = org_service.create_organization( + name="org", + username="testuser", + full_name="Test User", + email="test@mail.com", + public_key="abc123" + ) + + file = file_service.create_dummy_file(org, org.owner) + file2 = file_service.get_file_by_file_handle("dummy_file") + + assert file2.file_handle == "dummy_file" + assert file2.document_handle == "org/dummy_file.txt" + assert file2.name == "dummy_file" + + project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + repos = os.path.join(project_root, "repository") + os.remove(os.path.join(repos, file.document_handle)) + db.delete(file) + db.delete(org.owner) + db.delete(org) + db.commit() + + +def test_delete_file(): + org = org_service.create_organization( + name="org", + username="testuser", + full_name="Test User", + email="test@mail.com", + public_key="abc123" + ) + + file = file_service.create_dummy_file(org, org.owner) + file2 = file_service.delete_file(file) + + assert file2.file_handle == "dummy_file" + assert file2.document_handle is None + assert os.path.exists(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "repository", "org", "dummy_file.txt")) == False + + db.delete(file) + db.delete(org.owner) + db.delete(org) db.commit() \ No newline at end of file