Skip to content

Commit

Permalink
fix(daemon): implement missing methods bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
LordTermor committed Jun 10, 2024
1 parent 2edfdee commit a7661e0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 5 deletions.
79 changes: 76 additions & 3 deletions daemon/persistence/box/BoxRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <fmt/format.h>
#include <functional>
#include <kangaru/operator.hpp>
#include <optional>
#include <ranges>
#include <string>
#include <vector>
Expand All @@ -44,20 +45,92 @@ BoxRepository::BoxRepository(BoxOptions options,
coro::task<BoxRepository::TResult>
BoxRepository::find_by_id_async(TId id,
std::shared_ptr<UnitOfWorkBase> uow) {
std::optional<BoxRepository::TResult> result;
auto accept_ok = co_await m_package_store.accept(
[&](std::string_view key, const PackageRecord &value) {
if (value.id.section == SectionDTOMapper::to_dto(id.section)
&& value.id.name == id.package_name) {
result = RecordMapper::to_entity(value);
return Utilities::NavigationAction::Stop;
}
return Utilities::NavigationAction::Next;
},
uow);

if (!result.has_value()) {
co_return bxt::make_error<ReadError>(ReadError::EntityNotFound);
}

if (!accept_ok.has_value()) {
co_return bxt::make_error_with_source<ReadError>(
std::move(accept_ok.error()), ReadError::EntityFindError);
}

co_return *result;
}

coro::task<BoxRepository::TResult>
BoxRepository::find_first_async(std::function<bool(const Package &)>,
std::shared_ptr<UnitOfWorkBase> uow) {
coro::task<BoxRepository::TResult> BoxRepository::find_first_async(
std::function<bool(const Package &)> condition,
std::shared_ptr<UnitOfWorkBase> uow) {
std::optional<BoxRepository::TResult> result;
auto accept_ok = co_await m_package_store.accept(
[&](std::string_view key, const PackageRecord &value) {
if (condition(RecordMapper::to_entity(value))) {
return Utilities::NavigationAction::Stop;
}
return Utilities::NavigationAction::Next;
},
uow);

if (!result.has_value()) {
co_return bxt::make_error<ReadError>(ReadError::EntityNotFound);
}

if (!accept_ok.has_value()) {
co_return bxt::make_error_with_source<ReadError>(
std::move(accept_ok.error()), ReadError::EntityFindError);
}

co_return result.value();
}

coro::task<BoxRepository::TResults>
BoxRepository::find_async(std::function<bool(const Package &)> condition,
std::shared_ptr<UnitOfWorkBase> uow) {
std::vector<Package> packages;
auto accept_ok = co_await m_package_store.accept(
[&](std::string_view key, const PackageRecord &value) {
if (condition(RecordMapper::to_entity(value))) {
packages.push_back(RecordMapper::to_entity(value));
}
return Utilities::NavigationAction::Next;
},
uow);

if (!accept_ok.has_value()) {
co_return bxt::make_error_with_source<ReadError>(
std::move(accept_ok.error()), ReadError::EntityFindError);
}

co_return packages;
}

coro::task<BoxRepository::TResults>
BoxRepository::all_async(std::shared_ptr<UnitOfWorkBase> uow) {
std::vector<Package> packages;
auto accept_ok = co_await m_package_store.accept(
[&](std::string_view key, const PackageRecord &value) {
packages.push_back(RecordMapper::to_entity(value));
return Utilities::NavigationAction::Next;
},
uow);

if (!accept_ok.has_value()) {
co_return bxt::make_error_with_source<ReadError>(
std::move(accept_ok.error()), ReadError::EntityFindError);
}

co_return packages;
}

coro::task<BoxRepository::WriteResult<void>>
Expand Down
47 changes: 45 additions & 2 deletions daemon/persistence/lmdb/LmdbRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <coro/coro.hpp>
#include <memory>
#include <optional>
#include <vector>

namespace bxt::Persistence {
Expand Down Expand Up @@ -49,6 +50,7 @@ class LmdbRepositoryBase
using WriteResult = typename bxt::Core::Domain::ReadWriteRepositoryBase<
TEntity>::template Result<T>;

using ReadError = typename bxt::Core::Domain::ReadError;
using WriteError = typename bxt::Core::Domain::WriteError;

using TMapper = Utilities::StaticDTOMapper<TEntity, TDTO>;
Expand All @@ -72,12 +74,53 @@ class LmdbRepositoryBase
co_return TMapper::to_entity(*entity);
}
coro::task<TResult> find_first_async(
std::function<bool(const TEntity &)>,
std::shared_ptr<Core::Domain::UnitOfWorkBase> uow) override {}
std::function<bool(const TEntity &)> condition,
std::shared_ptr<Core::Domain::UnitOfWorkBase> uow) override {
auto lmdb_uow = std::dynamic_pointer_cast<LmdbUnitOfWork>(uow);
if (!lmdb_uow) {
co_return bxt::make_error<ReadError>(ReadError::InvalidArgument);
}

std::optional<TResult> result;

co_await m_db.accept(
lmdb_uow->txn().value,
[&result, &condition](std::string_view key, const TDTO &e) {
TEntity entity = TMapper::to_entity(e);
if (condition(entity)) {
result = entity;
return Utilities::NavigationAction::Stop;
}
return Utilities::NavigationAction::Next;
});

if (!result.has_value()) {
co_return bxt::make_error<ReadError>(ReadError::EntityNotFound);
}

co_return *result;
}

coro::task<TResults>
find_async(std::function<bool(const TEntity &)> condition,
std::shared_ptr<Core::Domain::UnitOfWorkBase> uow) override {
TResults results = {};
auto lmdb_uow = std::dynamic_pointer_cast<LmdbUnitOfWork>(uow);
if (!lmdb_uow) {
co_return bxt::make_error<ReadError>(ReadError::InvalidArgument);
}

co_await m_db.accept(
lmdb_uow->txn().value,
[&results, &condition](std::string_view key, const TDTO &e) {
TEntity entity = TMapper::to_entity(e);
if (condition(entity)) { results->push_back(entity); }
return Utilities::NavigationAction::Next;
});

co_return results;
}

coro::task<TResults>
all_async(std::shared_ptr<Core::Domain::UnitOfWorkBase> uow) override {
TEntities results;
Expand Down

0 comments on commit a7661e0

Please sign in to comment.