Skip to content

Commit 143a9b4

Browse files
authored
Merge pull request #2 from WeiJiLab/refactor
some refactorings
2 parents 72310ae + 5462b75 commit 143a9b4

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

rest/configuration.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@
88

99
#include <unordered_map>
1010

11-
constexpr auto DR_EVILS_DARK_ENERGY_ENERGY_SUPPLIER = "Dr Evil's Dark Energy";
12-
constexpr auto THE_GREEN_ECO_ENERGY_SUPPLIER = "The Green Eco";
13-
constexpr auto POWER_FOR_EVERYONE_ENERGY_SUPPLIER = "Power for Everyone";
11+
inline constexpr auto DR_EVILS_DARK_ENERGY_ENERGY_SUPPLIER = "Dr Evil's Dark Energy";
12+
inline constexpr auto THE_GREEN_ECO_ENERGY_SUPPLIER = "The Green Eco";
13+
inline constexpr auto POWER_FOR_EVERYONE_ENERGY_SUPPLIER = "Power for Everyone";
1414

15-
constexpr auto MOST_EVIL_PRICE_PLAN_ID = "price-plan-0";
16-
constexpr auto RENEWABLES_PRICE_PLAN_ID = "price-plan-1";
17-
constexpr auto STANDARD_PRICE_PLAN_ID = "price-plan-2";
15+
inline constexpr auto MOST_EVIL_PRICE_PLAN_ID = "price-plan-0";
16+
inline constexpr auto RENEWABLES_PRICE_PLAN_ID = "price-plan-1";
17+
inline constexpr auto STANDARD_PRICE_PLAN_ID = "price-plan-2";
1818

19-
constexpr auto SARAHS_SMART_METER_ID = "smart-meter-0";
20-
constexpr auto PETERS_SMART_METER_ID = "smart-meter-1";
21-
constexpr auto CHARLIES_SMART_METER_ID = "smart-meter-2";
22-
constexpr auto ANDREAS_SMART_METER_ID = "smart-meter-3";
23-
constexpr auto ALEXS_SMART_METER_ID = "smart-meter-4";
19+
inline constexpr auto SARAHS_SMART_METER_ID = "smart-meter-0";
20+
inline constexpr auto PETERS_SMART_METER_ID = "smart-meter-1";
21+
inline constexpr auto CHARLIES_SMART_METER_ID = "smart-meter-2";
22+
inline constexpr auto ANDREAS_SMART_METER_ID = "smart-meter-3";
23+
inline constexpr auto ALEXS_SMART_METER_ID = "smart-meter-4";
2424

25-
std::vector<PricePlan> pricePlans() {
25+
inline std::vector<PricePlan> pricePlans() {
2626
std::vector<PricePlan> price_plans;
2727
price_plans.push_back(PricePlan(MOST_EVIL_PRICE_PLAN_ID, DR_EVILS_DARK_ENERGY_ENERGY_SUPPLIER, 10, {}));
2828
price_plans.push_back(PricePlan(RENEWABLES_PRICE_PLAN_ID, THE_GREEN_ECO_ENERGY_SUPPLIER, 2, {}));
2929
price_plans.push_back(PricePlan(STANDARD_PRICE_PLAN_ID, POWER_FOR_EVERYONE_ENERGY_SUPPLIER, 1, {}));
3030
return price_plans;
3131
}
3232

33-
std::unordered_map<std::string, std::string> smart_meter_to_price_plan_accounts() {
33+
inline std::unordered_map<std::string, std::string> smart_meter_to_price_plan_accounts() {
3434
return {{SARAHS_SMART_METER_ID, MOST_EVIL_PRICE_PLAN_ID},
3535
{PETERS_SMART_METER_ID, RENEWABLES_PRICE_PLAN_ID},
3636
{CHARLIES_SMART_METER_ID, MOST_EVIL_PRICE_PLAN_ID},
3737
{ANDREAS_SMART_METER_ID, STANDARD_PRICE_PLAN_ID},
3838
{ALEXS_SMART_METER_ID, RENEWABLES_PRICE_PLAN_ID}};
3939
};
4040

41-
auto readings() {
41+
inline auto readings() {
4242
std::unordered_map<std::string, std::vector<ElectricityReading>> result;
4343
for (auto &[meter, plan] : smart_meter_to_price_plan_accounts()) {
4444
result[meter] = generator{}.generate(21);

rest/server.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class impl {
1313
explicit impl(int concurrency) : ioc_(concurrency) {
1414
using reading = MeterReadingController;
1515
using price_plan = PricePlanComparatorController;
16-
router_.to<reading, &reading::Read>(R"(/readings/read/([a-zA-Z0-9_-]+))", electricityReadingService_, meterReadingService);
17-
router_.to<reading, &reading::Store>(R"(/readings/store)", electricityReadingService_, meterReadingService);
16+
router_.to<reading, &reading::Read>(R"(/readings/read/([a-zA-Z0-9_-]+))", electricityReadingService_, meterReadingService_);
17+
router_.to<reading, &reading::Store>(R"(/readings/store)", electricityReadingService_, meterReadingService_);
1818
router_.to<price_plan, &price_plan::Compare>(R"(/price-plans/compare-all/([a-zA-Z0-9_-]+))", pricePlanService_);
1919
router_.to<price_plan, &price_plan::Recommend>(R"(/price-plans/recommend/([a-zA-Z0-9_-]+)\?(limit)=([0-9]+))",
2020
pricePlanService_);
@@ -23,7 +23,7 @@ class impl {
2323
void launch(const char *address, unsigned short port) {
2424
using tcp = boost::asio::ip::tcp;
2525
auto endpoint = tcp::endpoint{boost::asio::ip::make_address(address), port};
26-
std::make_shared<listener>(ioc_, endpoint, handler)->run();
26+
std::make_shared<listener>(ioc_, endpoint, handler_)->run();
2727
}
2828

2929
void run() { ioc_.run(); }
@@ -34,11 +34,11 @@ class impl {
3434
boost::asio::io_context ioc_;
3535
std::unordered_map<std::string, std::vector<ElectricityReading>> meterAssociatedReadings_{readings()};
3636
ElectricityReadingService electricityReadingService_{meterAssociatedReadings_};
37-
MeterReadingService meterReadingService{meterAssociatedReadings_};
37+
MeterReadingService meterReadingService_{meterAssociatedReadings_};
3838
std::vector<PricePlan> price_plans_{pricePlans()};
39-
PricePlanService pricePlanService_{price_plans_, meterReadingService};
39+
PricePlanService pricePlanService_{price_plans_, meterReadingService_};
4040
router router_;
41-
std::function<http::response<http::string_body>(const http::request<http::string_body> &)> handler = router_.handler();
41+
std::function<http::response<http::string_body>(const http::request<http::string_body> &)> handler_ = router_.handler();
4242
};
4343
} // namespace server_detail
4444

0 commit comments

Comments
 (0)