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;
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
59 BOOST_LOG_SEV(book_handler_lg(), debug) <<
"Handling " << msg.
subject;
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
63 error_reply(nats_, msg, ctx_expected.error());
66 const auto& ctx = *ctx_expected;
67 service::book_service svc(ctx);
68 get_books_response resp;
70 resp.books = svc.list_books();
71 resp.total_available_count =
72 static_cast<int>(resp.books.size());
73 BOOST_LOG_SEV(book_handler_lg(), debug)
75 }
catch (
const std::exception& e) {
76 BOOST_LOG_SEV(book_handler_lg(), error)
77 << msg.
subject <<
" failed: " << e.what();
79 reply(nats_, msg, resp);
83 BOOST_LOG_SEV(book_handler_lg(), debug) <<
"Handling " << msg.
subject;
84 auto ctx_expected = ores::service::service::make_request_context(
85 ctx_, msg, verifier_);
87 error_reply(nats_, msg, ctx_expected.error());
90 const auto& ctx = *ctx_expected;
91 if (!has_permission(ctx,
"refdata::books:write")) {
95 service::book_service svc(ctx);
96 auto req = decode<save_book_request>(msg);
98 BOOST_LOG_SEV(book_handler_lg(), warn)
99 <<
"Failed to decode: " << msg.
subject;
103 svc.save_book(req->data);
104 BOOST_LOG_SEV(book_handler_lg(), debug)
105 <<
"Completed " << msg.
subject;
106 reply(nats_, msg, save_book_response{.success =
true});
107 }
catch (
const std::exception& e) {
108 BOOST_LOG_SEV(book_handler_lg(), error)
109 << msg.
subject <<
" failed: " << e.what();
110 reply(nats_, msg, save_book_response{
111 .success =
false, .message = e.what()});
116 BOOST_LOG_SEV(book_handler_lg(), debug) <<
"Handling " << msg.
subject;
117 auto ctx_expected = ores::service::service::make_request_context(
118 ctx_, msg, verifier_);
120 error_reply(nats_, msg, ctx_expected.error());
123 const auto& ctx = *ctx_expected;
124 if (!has_permission(ctx,
"refdata::books:delete")) {
128 service::book_service svc(ctx);
129 auto req = decode<delete_book_request>(msg);
131 BOOST_LOG_SEV(book_handler_lg(), warn)
132 <<
"Failed to decode: " << msg.
subject;
136 boost::uuids::string_generator gen;
137 for (
const auto& id_str : req->ids)
138 svc.remove_book(gen(id_str));
139 BOOST_LOG_SEV(book_handler_lg(), debug)
140 <<
"Completed " << msg.
subject;
141 reply(nats_, msg, delete_book_response{.success =
true});
142 }
catch (
const std::exception& e) {
143 BOOST_LOG_SEV(book_handler_lg(), error)
144 << msg.
subject <<
" failed: " << e.what();
145 reply(nats_, msg, delete_book_response{
146 .success =
false, .message = e.what()});
151 BOOST_LOG_SEV(book_handler_lg(), debug) <<
"Handling " << msg.
subject;
152 auto ctx_expected = ores::service::service::make_request_context(
153 ctx_, msg, verifier_);
155 error_reply(nats_, msg, ctx_expected.error());
158 const auto& ctx = *ctx_expected;
159 service::book_service svc(ctx);
160 auto req = decode<get_book_history_request>(msg);
162 BOOST_LOG_SEV(book_handler_lg(), warn)
163 <<
"Failed to decode: " << msg.
subject;
167 boost::uuids::string_generator gen;
168 auto h = svc.get_book_history(gen(req->id));
169 BOOST_LOG_SEV(book_handler_lg(), debug)
170 <<
"Completed " << msg.
subject;
171 reply(nats_, msg, get_book_history_response{
172 .success =
true, .versions = std::move(h)});
173 }
catch (
const std::exception& e) {
174 BOOST_LOG_SEV(book_handler_lg(), error)
175 << msg.
subject <<
" failed: " << e.what();
176 reply(nats_, msg, get_book_history_response{
177 .success =
false, .message = e.what()});
184 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