Repository navigation
Expand file tree
/
Copy pathgenerated_query_expression.cpp
More file actions
77 lines (74 loc) · 3.62 KB
/
Copy pathgenerated_query_expression.cpp
File metadata and controls
77 lines (74 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "generated/include/cpp_examples.hpp"
#include <iostream>
using namespace cpp_examples;
static_assert(Entity<Product> && LedgerEntity<Order> && VersionedEntity<Order>);
int main() {
using E = cpp_examples::E;
GlobalContext global;
auto user_context_owner = global.make_user_context();
auto& user_context = *user_context_owner;
try {
setup(user_context);
auto Q = user_context.queries<cpp_examples::query_factory>();
auto& product = Q.products()
.comment("Construct the example product")
.purpose("Prepare an audited product")
.newEntity(user_context);
product.updateName("keyboard");
auto product_id = product.id();
require_value(
AuditedSaveExt<Product>::save(
std::move(product).audit_as("Seed the generated example product"), user_context)
.wait());
auto& order = Q.orders()
.comment("Construct the order graph")
.purpose("Prepare an audited order")
.newEntity(user_context);
order.updateName("generated-order");
auto order_id = order.id();
auto& line = Q.orderLines()
.comment("Construct the order line")
.purpose("Prepare the order child")
.newEntity(user_context);
line.updateName("first-line").updateOrderId(order_id).updateProductId(product_id);
order.lines().data.emplace_back(std::move(line));
order.lines().loaded();
require_value(
AuditedSaveExt<Order>::save(
std::move(order).audit_as("Create the generated order graph"), user_context)
.wait());
auto products = Q.products().selectName();
auto lines = Q.orderLines().selectName().selectProductWith(products);
auto request = Q.orders()
.withNameIs("generated-order")
.selectName()
.selectLinesWith(lines)
.comment("Load the order with its product")
.purpose("Display the generated order graph");
const auto& orders = require_value(request.executeForList(user_context).wait());
if (orders.len() != 1) {
throw std::runtime_error("Expected one generated order");
}
const auto& one = require_value(request.executeForOne(user_context).wait());
if (!one || one->id() != order_id) {
throw std::runtime_error("Expected the generated order as the first result");
}
auto streaming_request = Q.orderLines()
.orderByIdAsc()
.stream(1)
.comment("Stream the saved order lines")
.purpose("Read order lines incrementally");
const auto& handle = require_value(streaming_request.executeForStream(user_context).wait());
auto& stream = handle.get();
auto* item = stream.wait_next();
if (!item || require_value(*item).name() != "first-line" || stream.wait_next()) {
throw std::runtime_error("Expected one streamed order line");
}
auto name = E::order(orders[0]).getName().unwrap();
auto product_name = E::order(orders[0]).getLines().first().getProduct().getName().unwrap();
std::cout << name << ": " << product_name << '\n';
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}