Added dao base
All checks were successful
All checks were successful
This commit is contained in:
@@ -5,6 +5,7 @@ from cpl.core.console import Console
|
||||
from cpl.core.environment import Environment
|
||||
from cpl.core.log import LoggerABC
|
||||
from cpl.dependency import ServiceProviderABC
|
||||
from model.user_dao import UserDao
|
||||
from model.user_repo import UserRepo
|
||||
from model.user_repo_abc import UserRepoABC
|
||||
|
||||
@@ -15,21 +16,28 @@ class Application(ApplicationABC):
|
||||
|
||||
self._logger: Optional[LoggerABC] = None
|
||||
|
||||
def configure(self):
|
||||
self._logger = self._services.get_service(LoggerABC)
|
||||
|
||||
def main(self):
|
||||
self._logger.debug(f"Host: {Environment.get_host_name()}")
|
||||
self._logger.debug(f"Environment: {Environment.get_environment()}")
|
||||
|
||||
async def test_repos(self):
|
||||
user_repo: UserRepo = self._services.get_service(UserRepoABC)
|
||||
if len(user_repo.get_users()) == 0:
|
||||
if len(await user_repo.get_users()) == 0:
|
||||
user_repo.add_test_user()
|
||||
|
||||
Console.write_line("Users:")
|
||||
for user in user_repo.get_users():
|
||||
for user in await user_repo.get_users():
|
||||
Console.write_line(user.UserId, user.Name, user.City)
|
||||
|
||||
Console.write_line("Cities:")
|
||||
for city in user_repo.get_cities():
|
||||
for city in await user_repo.get_cities():
|
||||
Console.write_line(city.CityId, city.Name, city.ZIP)
|
||||
|
||||
async def test_daos(self):
|
||||
userDao: UserDao = self._services.get_service(UserDao)
|
||||
Console.write_line(await userDao.get_all())
|
||||
|
||||
async def configure(self):
|
||||
self._logger = self._services.get_service(LoggerABC)
|
||||
|
||||
async def main(self):
|
||||
self._logger.debug(f"Host: {Environment.get_host_name()}")
|
||||
self._logger.debug(f"Environment: {Environment.get_environment()}")
|
||||
|
||||
await self.test_daos()
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
|
||||
"DatabaseSettings": {
|
||||
"AuthPlugin": "mysql_native_password",
|
||||
"ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
|
||||
"Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
||||
"ConnectionString": "mysql+mysqlconnector://cpl:$credentials@localhost/cpl",
|
||||
"Credentials": "Y3Bs",
|
||||
"Encoding": "utf8mb4"
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,10 @@
|
||||
|
||||
"DatabaseSettings": {
|
||||
"Host": "localhost",
|
||||
"User": "sh_cpl",
|
||||
"Password": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
||||
"Database": "sh_cpl",
|
||||
"User": "cpl",
|
||||
"Port": 3306,
|
||||
"Password": "Y3Bs",
|
||||
"Database": "cpl",
|
||||
"Charset": "utf8mb4",
|
||||
"UseUnicode": "true",
|
||||
"Buffered": "true"
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
from cpl.application import ApplicationBuilder
|
||||
|
||||
from application import Application
|
||||
from cpl.application import ApplicationBuilder
|
||||
from startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
async def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
app_builder.build().run()
|
||||
app = await app_builder.build_async()
|
||||
await app.run_async()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
import asyncio
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from cpl.database import TableABC
|
||||
from cpl.database.abc.table_abc import TableABC
|
||||
|
||||
|
||||
class CityModel(TableABC):
|
||||
|
||||
16
tests/custom/database/src/model/user.py
Normal file
16
tests/custom/database/src/model/user.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from cpl.database.abc.db_model_abc import DbModelABC
|
||||
|
||||
|
||||
class User(DbModelABC):
|
||||
def __init__(self, id: int, name: str, city_id: int = 0):
|
||||
DbModelABC.__init__(self, id)
|
||||
self._name = name
|
||||
self._city_id = city_id
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def city_id(self) -> int:
|
||||
return self._city_id
|
||||
14
tests/custom/database/src/model/user_dao.py
Normal file
14
tests/custom/database/src/model/user_dao.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from cpl.database import InternalTables
|
||||
from cpl.database.abc import DbModelDaoABC
|
||||
from model.user import User
|
||||
|
||||
|
||||
class UserDao(DbModelDaoABC[User]):
|
||||
|
||||
def __init__(self):
|
||||
DbModelDaoABC.__init__(self, __name__, User, InternalTables.users)
|
||||
|
||||
self.attribute(User.name, str)
|
||||
self.attribute(User.city_id, int, db_name="CityId")
|
||||
|
||||
self.reference("city", "id", User.city_id, "city")
|
||||
@@ -1,5 +1,4 @@
|
||||
from cpl.database import TableABC
|
||||
|
||||
from cpl.database.abc.table_abc import TableABC
|
||||
from .city_model import CityModel
|
||||
|
||||
|
||||
|
||||
@@ -1,41 +1,38 @@
|
||||
from cpl.core.console import Console
|
||||
from cpl.database.mysql.context import DatabaseContextABC
|
||||
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from .city_model import CityModel
|
||||
from .user_model import UserModel
|
||||
from .user_repo_abc import UserRepoABC
|
||||
|
||||
|
||||
class UserRepo(UserRepoABC):
|
||||
def __init__(self, db_context: DatabaseContextABC):
|
||||
def __init__(self, db_context: DBContextABC):
|
||||
UserRepoABC.__init__(self)
|
||||
|
||||
self._db_context: DatabaseContextABC = db_context
|
||||
self._db_context: DBContextABC = db_context
|
||||
|
||||
def add_test_user(self):
|
||||
city = CityModel("Haren", "49733")
|
||||
city2 = CityModel("Meppen", "49716")
|
||||
self._db_context.cursor.execute(city2.insert_string)
|
||||
self._db_context.execute(city2.insert_string)
|
||||
user = UserModel("TestUser", city)
|
||||
self._db_context.cursor.execute(user.insert_string)
|
||||
self._db_context.save_changes()
|
||||
self._db_context.execute(user.insert_string)
|
||||
|
||||
def get_users(self) -> list[UserModel]:
|
||||
async def get_users(self) -> list[UserModel]:
|
||||
users = []
|
||||
results = self._db_context.select("SELECT * FROM `User`")
|
||||
results = await 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], await self.get_city_by_id(result[2]), id=result[0]))
|
||||
return users
|
||||
|
||||
def get_cities(self) -> list[CityModel]:
|
||||
async def get_cities(self) -> list[CityModel]:
|
||||
cities = []
|
||||
results = self._db_context.select("SELECT * FROM `City`")
|
||||
results = await self._db_context.select("SELECT * FROM `City`")
|
||||
for result in results:
|
||||
cities.append(CityModel(result[1], result[2], id=result[0]))
|
||||
return cities
|
||||
|
||||
def get_city_by_id(self, id: int) -> CityModel:
|
||||
async 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}")
|
||||
result = await self._db_context.select(f"SELECT * FROM `City` WHERE `Id` = {id}")
|
||||
return CityModel(result[1], result[2], id=result[0])
|
||||
|
||||
14
tests/custom/database/src/scripts/0-initial.sql
Normal file
14
tests/custom/database/src/scripts/0-initial.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS `city` (
|
||||
`id` INT(30) NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(64) NOT NULL,
|
||||
`zip` VARCHAR(5) NOT NULL,
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `users` (
|
||||
`id` INT(30) NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(64) NOT NULL,
|
||||
`cityId` INT(30),
|
||||
FOREIGN KEY (`cityId`) REFERENCES city(`id`),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
@@ -1,28 +1,35 @@
|
||||
from cpl.application import StartupABC
|
||||
from cpl.application.async_startup_abc import AsyncStartupABC
|
||||
from cpl.core.configuration import Configuration
|
||||
from cpl.core.environment import Environment
|
||||
from cpl.core.log import Logger, LoggerABC
|
||||
from cpl.database import mysql
|
||||
from cpl.database.abc.data_access_object_abc import DataAccessObjectABC
|
||||
from cpl.database.service.migration_service import MigrationService
|
||||
from cpl.dependency import ServiceCollection
|
||||
from model.user_dao import UserDao
|
||||
from model.user_repo import UserRepo
|
||||
from model.user_repo_abc import UserRepoABC
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
class Startup(AsyncStartupABC):
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
AsyncStartupABC.__init__(self)
|
||||
|
||||
self._configuration = None
|
||||
|
||||
def configure_configuration(self, configuration: Configuration, environment: Environment):
|
||||
async def configure_configuration(self, configuration: Configuration, environment: Environment):
|
||||
configuration.add_json_file(f"appsettings.json")
|
||||
configuration.add_json_file(f"appsettings.{environment.get_environment()}.json")
|
||||
configuration.add_json_file(f"appsettings.{environment.get_host_name()}.json", optional=True)
|
||||
|
||||
self._configuration = configuration
|
||||
|
||||
def configure_services(self, services: ServiceCollection, environment: Environment):
|
||||
async def configure_services(self, services: ServiceCollection, environment: Environment):
|
||||
services.add_module(mysql)
|
||||
|
||||
services.add_transient(DataAccessObjectABC, UserDao)
|
||||
|
||||
services.add_singleton(UserRepoABC, UserRepo)
|
||||
services.add_singleton(LoggerABC, Logger)
|
||||
|
||||
provider = services.build_service_provider()
|
||||
migration_service: MigrationService = provider.get_service(MigrationService)
|
||||
|
||||
migration_service.with_directory("./scripts")
|
||||
await migration_service.migrate()
|
||||
|
||||
Reference in New Issue
Block a user