Updated docs
This commit is contained in:
@@ -10,7 +10,6 @@ from model.user_repo import UserRepo
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
ApplicationABC.__init__(self, config, services)
|
||||
|
||||
@@ -20,19 +19,19 @@ class Application(ApplicationABC):
|
||||
self._logger = self._services.get_service(LoggerABC)
|
||||
|
||||
def main(self):
|
||||
self._logger.header(f'{self._configuration.environment.application_name}:')
|
||||
self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}')
|
||||
self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}')
|
||||
self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
|
||||
self._logger.header(f"{self._configuration.environment.application_name}:")
|
||||
self._logger.debug(__name__, f"Host: {self._configuration.environment.host_name}")
|
||||
self._logger.debug(__name__, f"Environment: {self._configuration.environment.environment_name}")
|
||||
self._logger.debug(__name__, f"Customer: {self._configuration.environment.customer}")
|
||||
|
||||
user_repo: UserRepo = self._services.get_service(UserRepoABC)
|
||||
if len(user_repo.get_users()) == 0:
|
||||
user_repo.add_test_user()
|
||||
|
||||
Console.write_line('Users:')
|
||||
|
||||
Console.write_line("Users:")
|
||||
for user in user_repo.get_users():
|
||||
Console.write_line(user.UserId, user.Name, user.City)
|
||||
|
||||
Console.write_line('Cities:')
|
||||
Console.write_line("Cities:")
|
||||
for city in user_repo.get_cities():
|
||||
Console.write_line(city.CityId, city.Name, city.ZIP)
|
||||
|
@@ -10,5 +10,5 @@ def main():
|
||||
app_builder.build().run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
@@ -2,46 +2,53 @@ from cpl_core.database import TableABC
|
||||
|
||||
|
||||
class CityModel(TableABC):
|
||||
|
||||
def __init__(self, name: str, zip_code: str, id = 0):
|
||||
def __init__(self, name: str, zip_code: str, id=0):
|
||||
self.CityId = id
|
||||
self.Name = name
|
||||
self.ZIP = zip_code
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_create_string() -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
CREATE TABLE IF NOT EXISTS `City` (
|
||||
`CityId` INT(30) NOT NULL AUTO_INCREMENT,
|
||||
`Name` VARCHAR(64) NOT NULL,
|
||||
`ZIP` VARCHAR(5) NOT NULL,
|
||||
PRIMARY KEY(`CityId`)
|
||||
);
|
||||
""")
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def insert_string(self) -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
INSERT INTO `City` (
|
||||
`Name`, `ZIP`
|
||||
) VALUES (
|
||||
'{self.Name}',
|
||||
'{self.ZIP}'
|
||||
);
|
||||
""")
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def udpate_string(self) -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
UPDATE `City`
|
||||
SET `Name` = '{self.Name}',
|
||||
`ZIP` = '{self.ZIP}',
|
||||
WHERE `CityId` = {self.Id};
|
||||
""")
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def delete_string(self) -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
DELETE FROM `City`
|
||||
WHERE `CityId` = {self.Id};
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
@@ -3,6 +3,5 @@ from cpl_core.database.context import DatabaseContext
|
||||
|
||||
|
||||
class DBContext(DatabaseContext):
|
||||
|
||||
def __init__(self, db_settings: DatabaseSettings):
|
||||
DatabaseContext.__init__(self, db_settings)
|
||||
|
@@ -4,8 +4,7 @@ from .city_model import CityModel
|
||||
|
||||
|
||||
class UserModel(TableABC):
|
||||
|
||||
def __init__(self, name: str, city: CityModel, id = 0):
|
||||
def __init__(self, name: str, city: CityModel, id=0):
|
||||
self.UserId = id
|
||||
self.Name = name
|
||||
self.CityId = city.CityId if city is not None else 0
|
||||
@@ -13,7 +12,8 @@ class UserModel(TableABC):
|
||||
|
||||
@staticmethod
|
||||
def get_create_string() -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
CREATE TABLE IF NOT EXISTS `User` (
|
||||
`UserId` INT(30) NOT NULL AUTO_INCREMENT,
|
||||
`Name` VARCHAR(64) NOT NULL,
|
||||
@@ -21,30 +21,37 @@ class UserModel(TableABC):
|
||||
FOREIGN KEY (`UserId`) REFERENCES City(`CityId`),
|
||||
PRIMARY KEY(`UserId`)
|
||||
);
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def insert_string(self) -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
INSERT INTO `User` (
|
||||
`Name`
|
||||
) VALUES (
|
||||
'{self.Name}'
|
||||
);
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def udpate_string(self) -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
UPDATE `User`
|
||||
SET `Name` = '{self.Name}',
|
||||
`CityId` = {self.CityId},
|
||||
WHERE `UserId` = {self.UserId};
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def delete_string(self) -> str:
|
||||
return str(f"""
|
||||
return str(
|
||||
f"""
|
||||
DELETE FROM `User`
|
||||
WHERE `UserId` = {self.UserId};
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
@@ -7,36 +7,35 @@ from .user_repo_abc import UserRepoABC
|
||||
|
||||
|
||||
class UserRepo(UserRepoABC):
|
||||
|
||||
def __init__(self, db_context: DatabaseContextABC):
|
||||
UserRepoABC.__init__(self)
|
||||
|
||||
self._db_context: DatabaseContextABC = db_context
|
||||
|
||||
def add_test_user(self):
|
||||
city = CityModel('Haren', '49733')
|
||||
city2 = CityModel('Meppen', '49716')
|
||||
city = CityModel("Haren", "49733")
|
||||
city2 = CityModel("Meppen", "49716")
|
||||
self._db_context.cursor.execute(city2.insert_string)
|
||||
user = UserModel('TestUser', city)
|
||||
user = UserModel("TestUser", city)
|
||||
self._db_context.cursor.execute(user.insert_string)
|
||||
self._db_context.save_changes()
|
||||
|
||||
def get_users(self) -> list[UserModel]:
|
||||
users = []
|
||||
results = self._db_context.select('SELECT * FROM `User`')
|
||||
users = []
|
||||
results = self._db_context.select("SELECT * FROM `User`")
|
||||
for result in results:
|
||||
users.append(UserModel(result[1], self.get_city_by_id(result[2]), id = result[0]))
|
||||
users.append(UserModel(result[1], self.get_city_by_id(result[2]), id=result[0]))
|
||||
return users
|
||||
|
||||
def get_cities(self) -> list[CityModel]:
|
||||
cities = []
|
||||
results = self._db_context.select('SELECT * FROM `City`')
|
||||
cities = []
|
||||
results = self._db_context.select("SELECT * FROM `City`")
|
||||
for result in results:
|
||||
cities.append(CityModel(result[1], result[2], id = result[0]))
|
||||
cities.append(CityModel(result[1], result[2], id=result[0]))
|
||||
return cities
|
||||
|
||||
|
||||
def get_city_by_id(self, id: int) -> CityModel:
|
||||
if id is None:
|
||||
return None
|
||||
result = self._db_context.select(f'SELECT * FROM `City` WHERE `Id` = {id}')
|
||||
return CityModel(result[1], result[2], id = result[0])
|
||||
result = self._db_context.select(f"SELECT * FROM `City` WHERE `Id` = {id}")
|
||||
return CityModel(result[1], result[2], id=result[0])
|
||||
|
@@ -5,15 +5,18 @@ from .user_model import UserModel
|
||||
|
||||
|
||||
class UserRepoABC(ABC):
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
def get_users(self) -> list[UserModel]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_users(self) -> list[UserModel]: pass
|
||||
|
||||
def get_cities(self) -> list[CityModel]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_cities(self) -> list[CityModel]: pass
|
||||
|
||||
@abstractmethod
|
||||
def get_city_by_id(self, id: int) -> CityModel: pass
|
||||
def get_city_by_id(self, id: int) -> CityModel:
|
||||
pass
|
||||
|
@@ -1,8 +1,7 @@
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.database import DatabaseSettings
|
||||
from cpl_core.dependency_injection import (ServiceCollectionABC,
|
||||
ServiceProviderABC)
|
||||
from cpl_core.dependency_injection import ServiceCollectionABC, ServiceProviderABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.logging import Logger, LoggerABC
|
||||
|
||||
@@ -12,25 +11,28 @@ from model.user_repo_abc import UserRepoABC
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
|
||||
self._configuration = None
|
||||
|
||||
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironmentABC) -> ConfigurationABC:
|
||||
configuration.add_environment_variables('PYTHON_')
|
||||
configuration.add_environment_variables('CPL_')
|
||||
def configure_configuration(
|
||||
self, configuration: ConfigurationABC, environment: ApplicationEnvironmentABC
|
||||
) -> ConfigurationABC:
|
||||
configuration.add_environment_variables("PYTHON_")
|
||||
configuration.add_environment_variables("CPL_")
|
||||
configuration.parse_console_arguments()
|
||||
configuration.add_json_file(f'appsettings.json')
|
||||
configuration.add_json_file(f'appsettings.{configuration.environment.environment_name}.json')
|
||||
configuration.add_json_file(f'appsettings.{configuration.environment.host_name}.json', optional=True)
|
||||
configuration.add_json_file(f"appsettings.json")
|
||||
configuration.add_json_file(f"appsettings.{configuration.environment.environment_name}.json")
|
||||
configuration.add_json_file(f"appsettings.{configuration.environment.host_name}.json", optional=True)
|
||||
|
||||
self._configuration = configuration
|
||||
|
||||
|
||||
return configuration
|
||||
|
||||
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironmentABC) -> ServiceProviderABC:
|
||||
def configure_services(
|
||||
self, services: ServiceCollectionABC, environment: ApplicationEnvironmentABC
|
||||
) -> ServiceProviderABC:
|
||||
# Create and connect to database
|
||||
db_settings: DatabaseSettings = self._configuration.get_configuration(DatabaseSettings)
|
||||
services.add_db_context(DBContext, db_settings)
|
||||
|
@@ -1 +1 @@
|
||||
# imports:
|
||||
# imports:
|
||||
|
Reference in New Issue
Block a user