diff --git a/Jupyter/API-tests/News/utils/NewsServiceInterface.py b/Jupyter/API-tests/News/utils/NewsServiceInterface.py index a69318b..49f2af7 100644 --- a/Jupyter/API-tests/News/utils/NewsServiceInterface.py +++ b/Jupyter/API-tests/News/utils/NewsServiceInterface.py @@ -1,6 +1,6 @@ from abc import ABC -from models.News import News +from News.models.News import News class NewsServiceInterface(ABC): diff --git a/Jupyter/API-tests/News/utils/mongodb/mongo.py b/Jupyter/API-tests/News/utils/mongodb/mongo.py index da55557..77cdc7d 100644 --- a/Jupyter/API-tests/News/utils/mongodb/mongo.py +++ b/Jupyter/API-tests/News/utils/mongodb/mongo.py @@ -1,6 +1,6 @@ import pymongo -from models.News import News -from utils.NewsServiceInterface import NewsServiceInterface +from News.models.News import News +from News.utils.NewsServiceInterface import NewsServiceInterface class MongoConnector: @@ -8,7 +8,7 @@ class MongoConnector: self, hostname, database: str, - port: int = 27017, + port: int | None, username: str | None = None, password: str | None = None, ): @@ -20,10 +20,13 @@ class MongoConnector: def connect(self, hostname, port, username, password) -> pymongo.MongoClient: if username is not None and password is not None: - connection_string = f"mongodb://{username}:{password}@{hostname}:{port}" + connection_string = f"mongodb+srv://{username}:{password}@{hostname}" else: - connection_string = f"mongodb://{hostname}:{port}" - + connection_string = f"mongodb+srv://{hostname}" + if port is not None: + connection_string += f":{port}" + connection_string = connection_string.replace("mongodb+srv", "mongodb") + print(connection_string) return pymongo.MongoClient(connection_string) diff --git a/Jupyter/API-tests/Unternehmensregister/models/Company.py b/Jupyter/API-tests/Unternehmensregister/models/Company.py index dbddee3..ae1a6f0 100644 --- a/Jupyter/API-tests/Unternehmensregister/models/Company.py +++ b/Jupyter/API-tests/Unternehmensregister/models/Company.py @@ -1,5 +1,5 @@ from abc import ABC -from dataclasses import dataclass +from dataclasses import asdict, dataclass from enum import Enum @@ -35,3 +35,6 @@ class Company: name: str last_update: str relationships: list[CompanyRelationship] + + def dict(self): + return asdict(self) diff --git a/Jupyter/API-tests/Unternehmensregister/notebook.ipynb b/Jupyter/API-tests/Unternehmensregister/notebook.ipynb index 950c0f7..f0eb465 100644 --- a/Jupyter/API-tests/Unternehmensregister/notebook.ipynb +++ b/Jupyter/API-tests/Unternehmensregister/notebook.ipynb @@ -4199,6 +4199,102 @@ " ) as export_file:\n", " json.dump(dataclasses.asdict(company), export_file, ensure_ascii=False)" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import sys\n", + "\n", + "module_path = os.path.abspath(os.path.join(\"..\"))\n", + "if module_path not in sys.path:\n", + " sys.path.append(module_path)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mongodb://root:pR0R0v2e2@trisnol.tech:27017\n" + ] + } + ], + "source": [ + "from News.utils.mongodb.mongo import MongoConnector\n", + "from Unternehmensregister.utils.CompanyMongoService import CompanyMongoService\n", + "\n", + "connector = MongoConnector(\n", + " hostname=\"trisnol.tech\",\n", + " database=\"transparenzregister\",\n", + " username=\"root\",\n", + " password=\"pR0R0v2e2\",\n", + " port=27017,\n", + ")\n", + "\n", + "service = CompanyMongoService(connector)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + " 0%| | 0/3147 [00:00 list[Company]: + result = self.collection.find() + return list(result) + + def get_by_id(self, id: str) -> Company | None: + result = list(self.collection.find({"id": id})) + if len(result) == 1: + return result[0] + return None + + def insert(self, company: Company): + return self.collection.insert_one(company.dict()) diff --git a/Jupyter/API-tests/Unternehmensregister/utils/CompanyServiceInterface.py b/Jupyter/API-tests/Unternehmensregister/utils/CompanyServiceInterface.py new file mode 100644 index 0000000..467560d --- /dev/null +++ b/Jupyter/API-tests/Unternehmensregister/utils/CompanyServiceInterface.py @@ -0,0 +1,14 @@ +from abc import ABC + +from models import Company + + +class CompanyServiceInterface(ABC): + def get_all(self) -> list[Company.Company]: + raise NotImplementedError() + + def get_by_id(self, id: Company.CompayID) -> Company.Company | None: + raise NotImplementedError() + + def insert(self, company: Company.Company): + raise NotImplementedError()