20#ifndef ORES_REFDATA_CORE_MESSAGING_BOOK_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_BOOK_HANDLER_HPP
24#include <boost/uuid/string_generator.hpp>
25#include "ores.logging/make_logger.hpp"
26#include "ores.nats/domain/message.hpp"
27#include "ores.nats/service/client.hpp"
28#include "ores.database/domain/context.hpp"
29#include "ores.security/jwt/jwt_authenticator.hpp"
30#include "ores.service/messaging/handler_helpers.hpp"
31#include "ores.service/service/request_context.hpp"
32#include "ores.refdata.api/messaging/book_protocol.hpp"
33#include "ores.refdata.core/service/book_service.hpp"
35namespace ores::refdata::messaging {
38inline auto& book_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.book_handler");
45using ores::service::messaging::reply;
46using ores::service::messaging::decode;
47using ores::service::messaging::error_reply;
48using ores::service::messaging::has_permission;
49using ores::service::messaging::log_handler_entry;
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
60 [[maybe_unused]]
const auto correlation_id =
61 log_handler_entry(book_handler_lg(), msg);
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
65 error_reply(nats_, msg, ctx_expected.error());
68 const auto& ctx = *ctx_expected;
69 service::book_service svc(ctx);
70 get_books_response resp;
72 resp.books = svc.list_books();
73 resp.total_available_count =
74 static_cast<int>(resp.books.size());
75 BOOST_LOG_SEV(book_handler_lg(), debug)
77 }
catch (
const std::exception& e) {
78 BOOST_LOG_SEV(book_handler_lg(), error)
79 << msg.
subject <<
" failed: " << e.what();
81 reply(nats_, msg, resp);
85 [[maybe_unused]]
const auto correlation_id =
86 log_handler_entry(book_handler_lg(), msg);
87 auto ctx_expected = ores::service::service::make_request_context(
88 ctx_, msg, verifier_);
90 error_reply(nats_, msg, ctx_expected.error());
93 const auto& ctx = *ctx_expected;
94 if (!has_permission(ctx,
"refdata::books:write")) {
98 service::book_service svc(ctx);
99 auto req = decode<save_book_request>(msg);
101 BOOST_LOG_SEV(book_handler_lg(), warn)
102 <<
"Failed to decode: " << msg.
subject;
106 svc.save_book(req->data);
107 BOOST_LOG_SEV(book_handler_lg(), debug)
108 <<
"Completed " << msg.
subject;
109 reply(nats_, msg, save_book_response{.success =
true});
110 }
catch (
const std::exception& e) {
111 BOOST_LOG_SEV(book_handler_lg(), error)
112 << msg.
subject <<
" failed: " << e.what();
113 reply(nats_, msg, save_book_response{
114 .success =
false, .message = e.what()});
119 [[maybe_unused]]
const auto correlation_id =
120 log_handler_entry(book_handler_lg(), msg);
121 auto ctx_expected = ores::service::service::make_request_context(
122 ctx_, msg, verifier_);
124 error_reply(nats_, msg, ctx_expected.error());
127 const auto& ctx = *ctx_expected;
128 if (!has_permission(ctx,
"refdata::books:delete")) {
132 service::book_service svc(ctx);
133 auto req = decode<delete_book_request>(msg);
135 BOOST_LOG_SEV(book_handler_lg(), warn)
136 <<
"Failed to decode: " << msg.
subject;
140 boost::uuids::string_generator gen;
141 for (
const auto& id_str : req->ids)
142 svc.remove_book(gen(id_str));
143 BOOST_LOG_SEV(book_handler_lg(), debug)
144 <<
"Completed " << msg.
subject;
145 reply(nats_, msg, delete_book_response{.success =
true});
146 }
catch (
const std::exception& e) {
147 BOOST_LOG_SEV(book_handler_lg(), error)
148 << msg.
subject <<
" failed: " << e.what();
149 reply(nats_, msg, delete_book_response{
150 .success =
false, .message = e.what()});
155 [[maybe_unused]]
const auto correlation_id =
156 log_handler_entry(book_handler_lg(), msg);
157 auto ctx_expected = ores::service::service::make_request_context(
158 ctx_, msg, verifier_);
160 error_reply(nats_, msg, ctx_expected.error());
163 const auto& ctx = *ctx_expected;
164 service::book_service svc(ctx);
165 auto req = decode<get_book_history_request>(msg);
167 BOOST_LOG_SEV(book_handler_lg(), warn)
168 <<
"Failed to decode: " << msg.
subject;
172 boost::uuids::string_generator gen;
173 auto h = svc.get_book_history(gen(req->id));
174 BOOST_LOG_SEV(book_handler_lg(), debug)
175 <<
"Completed " << msg.
subject;
176 reply(nats_, msg, get_book_history_response{
177 .success =
true, .versions = std::move(h)});
178 }
catch (
const std::exception& e) {
179 BOOST_LOG_SEV(book_handler_lg(), error)
180 << msg.
subject <<
" failed: " << e.what();
181 reply(nats_, msg, get_book_history_response{
182 .success =
false, .message = e.what()});
189 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73