Skip to content

Commit

Permalink
fix(daemon): save entities instead of adding
Browse files Browse the repository at this point in the history
This fixes an issue that entity cannot be added if it already exists
  • Loading branch information
LordTermor committed Jul 16, 2024
1 parent 9b28b52 commit 3dd55df
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion daemon/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions daemon/core/application/services/UserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ coro::task<UserService::Result<void>>

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<CrudError>(
Expand Down Expand Up @@ -85,7 +85,7 @@ coro::task<UserService::Result<void>>
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<CrudError>(
Expand Down
12 changes: 6 additions & 6 deletions daemon/event_log/application/services/LogService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void LogService::init() {
coro::sync_wait([this, sync_log_entry = std::move(
sync_log_entry)]() -> coro::task<void> {
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();

Expand Down Expand Up @@ -97,8 +97,8 @@ void LogService::init() {
coro::sync_wait([this, commit_log_entry = std::move(
commit_log_entry)]() -> coro::task<void> {
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();

Expand All @@ -114,8 +114,8 @@ void LogService::init() {
coro::sync_wait([this, deploy_log_entry = std::move(
deploy_log_entry)]() -> coro::task<void> {
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;
}());
Expand Down
26 changes: 12 additions & 14 deletions daemon/infrastructure/PackageService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ coro::task<PackageService::Result<void>>
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<CrudError>(
std::move(add_result.error()), CrudError::ErrorType::InternalError);
std::move(saved.error()), CrudError::ErrorType::InternalError);
}

// Finally, remove the package from the from_section
Expand Down Expand Up @@ -146,12 +145,11 @@ coro::task<PackageService::Result<void>>
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<CrudError>(
std::move(add_result.error()), CrudError::ErrorType::InternalError);
std::move(saved.error()), CrudError::ErrorType::InternalError);
}

co_return {};
Expand Down Expand Up @@ -179,12 +177,12 @@ coro::task<PackageService::Result<void>>
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<CrudError>(
std::move(add_ok.error()), CrudError::ErrorType::InternalError);
std::move(saved.error()), CrudError::ErrorType::InternalError);
}

co_return {};
Expand Down Expand Up @@ -247,10 +245,10 @@ coro::task<PackageService::Result<void>>
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<CrudError>(
std::move(added.error()), CrudError::ErrorType::InternalError);
std::move(saved.error()), CrudError::ErrorType::InternalError);
}

auto commit_ok = co_await uow->commit_async();
Expand Down
6 changes: 3 additions & 3 deletions daemon/infrastructure/alpm/ArchRepoSyncService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ coro::task<SyncService::Result<void>>
}
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<SyncError>(
std::move(added.error()), SyncError::RepositoryError);
std::move(saved.error()), SyncError::RepositoryError);
}

auto commit_ok = co_await uow->commit_async();
Expand Down

0 comments on commit 3dd55df

Please sign in to comment.