From 3dd55df491742749fe4b916fc7bce9583e722aaf Mon Sep 17 00:00:00 2001 From: Artem Grinev Date: Tue, 16 Jul 2024 12:10:31 +0000 Subject: [PATCH] fix(daemon): save entities instead of adding This fixes an issue that entity cannot be added if it already exists --- daemon/application.cpp | 2 +- .../core/application/services/UserService.cpp | 4 +-- .../application/services/LogService.cpp | 12 ++++----- daemon/infrastructure/PackageService.cpp | 26 +++++++++---------- .../alpm/ArchRepoSyncService.cpp | 6 ++--- 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/daemon/application.cpp b/daemon/application.cpp index 76ece0f8..54cbfa32 100644 --- a/daemon/application.cpp +++ b/daemon/application.cpp @@ -211,7 +211,7 @@ void setup_defaults(kgr::container& container) { default_user.set_permissions({Permission("*")}); const auto add_result = - coro::sync_wait(repository.add_async(default_user, uow)); + coro::sync_wait(repository.save_async(default_user, uow)); if (!add_result.has_value()) { bxt::loge(add_result.error().what()); coro::sync_wait(uow->rollback_async()); diff --git a/daemon/core/application/services/UserService.cpp b/daemon/core/application/services/UserService.cpp index 0d9e7c9b..60c0fdca 100644 --- a/daemon/core/application/services/UserService.cpp +++ b/daemon/core/application/services/UserService.cpp @@ -17,7 +17,7 @@ coro::task> auto uow = co_await m_uow_factory(true); - auto result = co_await m_repository.add_async(user_entity, uow); + auto result = co_await m_repository.save_async(user_entity, uow); if (!result.has_value()) { co_return bxt::make_error_with_source( @@ -85,7 +85,7 @@ coro::task> existing_user_entity->set_permissions(permission_entities); } - auto result = co_await m_repository.add_async(*existing_user_entity, uow); + auto result = co_await m_repository.save_async(*existing_user_entity, uow); if (!result.has_value()) { co_return bxt::make_error_with_source( diff --git a/daemon/event_log/application/services/LogService.cpp b/daemon/event_log/application/services/LogService.cpp index 144bab18..a6746c4a 100644 --- a/daemon/event_log/application/services/LogService.cpp +++ b/daemon/event_log/application/services/LogService.cpp @@ -67,8 +67,8 @@ void LogService::init() { coro::sync_wait([this, sync_log_entry = std::move( sync_log_entry)]() -> coro::task { auto uow = co_await m_uow_factory(true); - auto added = - co_await m_sync_repository.add_async(sync_log_entry, uow); + auto saved = + co_await m_sync_repository.save_async(sync_log_entry, uow); auto commited = co_await uow->commit_async(); @@ -97,8 +97,8 @@ void LogService::init() { coro::sync_wait([this, commit_log_entry = std::move( commit_log_entry)]() -> coro::task { auto uow = co_await m_uow_factory(true); - auto added = - co_await m_commit_repository.add_async(commit_log_entry, uow); + auto saved = + co_await m_commit_repository.save_async(commit_log_entry, uow); auto committed = co_await uow->commit_async(); @@ -114,8 +114,8 @@ void LogService::init() { coro::sync_wait([this, deploy_log_entry = std::move( deploy_log_entry)]() -> coro::task { auto uow = co_await m_uow_factory(true); - auto added = - co_await m_deploy_repository.add_async(deploy_log_entry, uow); + auto saved = + co_await m_deploy_repository.save_async(deploy_log_entry, uow); auto committed = co_await uow->commit_async(); co_return; }()); diff --git a/daemon/infrastructure/PackageService.cpp b/daemon/infrastructure/PackageService.cpp index 42795a3a..99edf416 100644 --- a/daemon/infrastructure/PackageService.cpp +++ b/daemon/infrastructure/PackageService.cpp @@ -107,12 +107,11 @@ coro::task> package_to_move->set_section(SectionDTOMapper::to_entity(to_section)); // Then, add the package to the to_section - auto add_result = - co_await m_repository.add_async(*package_to_move, unitofwork); + auto saved = co_await m_repository.save_async(*package_to_move, unitofwork); - if (!add_result.has_value()) { + if (!saved.has_value()) { co_return bxt::make_error_with_source( - std::move(add_result.error()), CrudError::ErrorType::InternalError); + std::move(saved.error()), CrudError::ErrorType::InternalError); } // Finally, remove the package from the from_section @@ -146,12 +145,11 @@ coro::task> package_to_copy->set_section(SectionDTOMapper::to_entity(to_section)); // Then, add the package to the to_section - auto add_result = - co_await m_repository.add_async(*package_to_copy, unitofwork); + auto saved = co_await m_repository.save_async(*package_to_copy, unitofwork); - if (!add_result.has_value()) { + if (!saved.has_value()) { co_return bxt::make_error_with_source( - std::move(add_result.error()), CrudError::ErrorType::InternalError); + std::move(saved.error()), CrudError::ErrorType::InternalError); } co_return {}; @@ -179,12 +177,12 @@ coro::task> CrudError::ErrorType::EntityAlreadyExists); } - auto add_ok = co_await m_repository.add_async( + auto saved = co_await m_repository.save_async( PackageDTOMapper::to_entity(package), unitofwork); - if (!add_ok.has_value()) { + if (!saved.has_value()) { co_return bxt::make_error_with_source( - std::move(add_ok.error()), CrudError::ErrorType::InternalError); + std::move(saved.error()), CrudError::ErrorType::InternalError); } co_return {}; @@ -247,10 +245,10 @@ coro::task> source_package.set_section(SectionDTOMapper::to_entity(to_section)); } - auto added = co_await m_repository.add_async(*source_packages, uow); - if (!added.has_value()) { + auto saved = co_await m_repository.save_async(*source_packages, uow); + if (!saved.has_value()) { co_return bxt::make_error_with_source( - std::move(added.error()), CrudError::ErrorType::InternalError); + std::move(saved.error()), CrudError::ErrorType::InternalError); } auto commit_ok = co_await uow->commit_async(); diff --git a/daemon/infrastructure/alpm/ArchRepoSyncService.cpp b/daemon/infrastructure/alpm/ArchRepoSyncService.cpp index 5042aa58..0959a2a1 100644 --- a/daemon/infrastructure/alpm/ArchRepoSyncService.cpp +++ b/daemon/infrastructure/alpm/ArchRepoSyncService.cpp @@ -111,10 +111,10 @@ coro::task> } auto uow = co_await m_uow_factory(true); - auto added = co_await m_package_repository.add_async(*all_packages, uow); - if (!added.has_value()) { + auto saved = co_await m_package_repository.save_async(*all_packages, uow); + if (!saved.has_value()) { co_return bxt::make_error_with_source( - std::move(added.error()), SyncError::RepositoryError); + std::move(saved.error()), SyncError::RepositoryError); } auto commit_ok = co_await uow->commit_async();