ORE Studio 0.0.4
Loading...
Searching...
No Matches
currency_market_tier_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_REFDATA_CORE_MESSAGING_CURRENCY_MARKET_TIER_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_CURRENCY_MARKET_TIER_HANDLER_HPP
22
23#include <optional>
24#include "ores.logging/make_logger.hpp"
25#include "ores.nats/domain/message.hpp"
26#include "ores.nats/service/client.hpp"
27#include "ores.database/domain/context.hpp"
28#include "ores.security/jwt/jwt_authenticator.hpp"
29#include "ores.service/messaging/handler_helpers.hpp"
30#include "ores.service/service/request_context.hpp"
31#include "ores.refdata.api/messaging/currency_market_tier_protocol.hpp"
32#include "ores.refdata.core/service/currency_market_tier_service.hpp"
33
34namespace ores::refdata::messaging {
35
36namespace {
37inline auto& currency_market_tier_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.currency_market_tier_handler");
40 return instance;
41}
42} // namespace
43
44using ores::service::messaging::reply;
45using ores::service::messaging::decode;
46using ores::service::messaging::error_reply;
47using ores::service::messaging::has_permission;
48using namespace ores::logging;
49
50class currency_market_tier_handler {
51public:
52 currency_market_tier_handler(ores::nats::service::client& nats,
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
56
57 void list(ores::nats::message msg) {
58 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
59 << "Handling " << msg.subject;
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
62 if (!ctx_expected) {
63 error_reply(nats_, msg, ctx_expected.error());
64 return;
65 }
66 const auto& ctx = *ctx_expected;
67 service::currency_market_tier_service svc(ctx);
68 get_currency_market_tiers_response resp;
69 auto req = decode<get_currency_market_tiers_request>(msg);
70 if (!req) {
71 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
72 << "Failed to decode: " << msg.subject;
73 reply(nats_, msg, resp);
74 return;
75 }
76 try {
77 resp.currency_market_tiers = svc.list_types();
78 resp.total_available_count =
79 static_cast<int>(resp.currency_market_tiers.size());
80 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
81 << "Completed " << msg.subject;
82 } catch (const std::exception& e) {
83 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
84 << msg.subject << " failed: " << e.what();
85 }
86 reply(nats_, msg, resp);
87 }
88
89 void save(ores::nats::message msg) {
90 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
91 << "Handling " << msg.subject;
92 auto ctx_expected = ores::service::service::make_request_context(
93 ctx_, msg, verifier_);
94 if (!ctx_expected) {
95 error_reply(nats_, msg, ctx_expected.error());
96 return;
97 }
98 const auto& ctx = *ctx_expected;
99 if (!has_permission(ctx, "refdata::currency_market_tiers:write")) {
100 error_reply(nats_, msg, ores::service::error_code::forbidden);
101 return;
102 }
103 service::currency_market_tier_service svc(ctx);
104 auto req = decode<save_currency_market_tier_request>(msg);
105 if (!req) {
106 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
107 << "Failed to decode: " << msg.subject;
108 return;
109 }
110 try {
111 svc.save_type(req->data);
112 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
113 << "Completed " << msg.subject;
114 reply(nats_, msg,
115 save_currency_market_tier_response{.success = true});
116 } catch (const std::exception& e) {
117 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
118 << msg.subject << " failed: " << e.what();
119 reply(nats_, msg, save_currency_market_tier_response{
120 .success = false, .message = e.what()});
121 }
122 }
123
124 void del(ores::nats::message msg) {
125 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
126 << "Handling " << msg.subject;
127 auto ctx_expected = ores::service::service::make_request_context(
128 ctx_, msg, verifier_);
129 if (!ctx_expected) {
130 error_reply(nats_, msg, ctx_expected.error());
131 return;
132 }
133 const auto& ctx = *ctx_expected;
134 if (!has_permission(ctx, "refdata::currency_market_tiers:delete")) {
135 error_reply(nats_, msg, ores::service::error_code::forbidden);
136 return;
137 }
138 service::currency_market_tier_service svc(ctx);
139 auto req = decode<delete_currency_market_tier_request>(msg);
140 if (!req) {
141 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
142 << "Failed to decode: " << msg.subject;
143 return;
144 }
145 try {
146 svc.remove_type(req->tier);
147 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
148 << "Completed " << msg.subject;
149 reply(nats_, msg,
150 delete_currency_market_tier_response{.success = true});
151 } catch (const std::exception& e) {
152 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
153 << msg.subject << " failed: " << e.what();
154 reply(nats_, msg, delete_currency_market_tier_response{
155 .success = false, .message = e.what()});
156 }
157 }
158
159 void history(ores::nats::message msg) {
160 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
161 << "Handling " << msg.subject;
162 auto ctx_expected = ores::service::service::make_request_context(
163 ctx_, msg, verifier_);
164 if (!ctx_expected) {
165 error_reply(nats_, msg, ctx_expected.error());
166 return;
167 }
168 const auto& ctx = *ctx_expected;
169 service::currency_market_tier_service svc(ctx);
170 auto req = decode<get_currency_market_tier_history_request>(msg);
171 if (!req) {
172 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
173 << "Failed to decode: " << msg.subject;
174 return;
175 }
176 try {
177 auto h = svc.get_type_history(req->tier);
178 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
179 << "Completed " << msg.subject;
180 reply(nats_, msg, get_currency_market_tier_history_response{
181 .success = true, .history = std::move(h)});
182 } catch (const std::exception& e) {
183 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
184 << msg.subject << " failed: " << e.what();
185 reply(nats_, msg, get_currency_market_tier_history_response{
186 .success = false, .message = e.what()});
187 }
188 }
189
190private:
193 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
194};
195
196} // namespace ores::refdata::messaging
197#endif
STL namespace.
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