|
1 | 1 | #ifndef DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H |
2 | 2 | #define DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H |
3 | 3 |
|
| 4 | +#include <configuration.h> |
4 | 5 | #include <service/ElectricityReadingService.h> |
5 | 6 | #include <service/PricePlanService.h> |
6 | | -#include <configuration.h> |
7 | 7 |
|
8 | 8 | #include <ctime> |
9 | 9 | #include <iomanip> |
| 10 | +#include <iostream> |
10 | 11 |
|
11 | 12 | #include <boost/beast/http.hpp> |
12 | 13 | #include <nlohmann/json.hpp> |
13 | 14 |
|
14 | | - |
15 | | -#include <iostream> |
16 | | - |
17 | 15 | namespace http = boost::beast::http; |
18 | 16 |
|
19 | 17 | class PricePlanComparatorController { |
20 | | -public: |
21 | | - PricePlanComparatorController(PricePlanService &pricePlanService) : pricePlanService(pricePlanService) {} |
| 18 | + public: |
| 19 | + PricePlanComparatorController(PricePlanService &pricePlanService) : pricePlanService(pricePlanService) {} |
22 | 20 |
|
23 | | - http::response<http::string_body> |
24 | | - Compare(const http::request<http::string_body> &req, const std::vector<std::string> &queries) { |
25 | | - const auto &meterId = queries[0]; |
26 | | - auto costs = pricePlanService.getConsumptionCostOfElectricityReadingsForEachPricePlan(meterId); |
| 21 | + http::response<http::string_body> Compare(const http::request<http::string_body> &req, |
| 22 | + const std::vector<std::string> &queries) { |
| 23 | + const auto &meterId = queries[0]; |
| 24 | + auto costs = pricePlanService.getConsumptionCostOfElectricityReadingsForEachPricePlan(meterId); |
27 | 25 |
|
28 | | - if (!costs) { |
29 | | - return {http::status::not_found, req.version()}; |
30 | | - } |
31 | | - auto current_price_plans = smart_meter_to_price_plan_accounts(); |
32 | | - http::response<http::string_body> res{http::status::ok, req.version()}; |
33 | | - res.set(http::field::content_type, "application/json"); |
34 | | - res.keep_alive(req.keep_alive()); |
35 | | - nlohmann::json j; |
36 | | - j["pricePlanComparisons"] = {{"price-plan-0", double(costs.value()["price-plan-0"]) / 10000}, |
37 | | - {"price-plan-1", double(costs.value()["price-plan-1"]) / 10000}, |
38 | | - {"price-plan-2", double(costs.value()["price-plan-2"]) / 10000}}; |
39 | | - j["pricePlanId"] = current_price_plans[meterId]; |
40 | | - res.body() = j.dump(); |
41 | | - res.prepare_payload(); |
42 | | - return res; |
| 26 | + if (!costs) { |
| 27 | + return {http::status::not_found, req.version()}; |
43 | 28 | } |
| 29 | + auto current_price_plans = smart_meter_to_price_plan_accounts(); |
| 30 | + http::response<http::string_body> res{http::status::ok, req.version()}; |
| 31 | + res.set(http::field::content_type, "application/json"); |
| 32 | + res.keep_alive(req.keep_alive()); |
| 33 | + nlohmann::json j; |
| 34 | + j["pricePlanComparisons"] = {{"price-plan-0", double(costs.value()["price-plan-0"]) / 10000}, |
| 35 | + {"price-plan-1", double(costs.value()["price-plan-1"]) / 10000}, |
| 36 | + {"price-plan-2", double(costs.value()["price-plan-2"]) / 10000}}; |
| 37 | + j["pricePlanId"] = current_price_plans[meterId]; |
| 38 | + res.body() = j.dump(); |
| 39 | + res.prepare_payload(); |
| 40 | + return res; |
| 41 | + } |
44 | 42 |
|
45 | | - http::response<http::string_body> |
46 | | - Recommend(const http::request<http::string_body> &req, const std::vector<std::string> &queries) { |
47 | | - const auto &meterId = queries[0]; |
48 | | - int limit = std::stoi(queries[2]); |
49 | | - auto costs = pricePlanService.getConsumptionCostOfElectricityReadingsForEachPricePlan(meterId); |
| 43 | + http::response<http::string_body> Recommend(const http::request<http::string_body> &req, |
| 44 | + const std::vector<std::string> &queries) { |
| 45 | + const auto &meterId = queries[0]; |
| 46 | + int limit = std::stoi(queries[2]); |
| 47 | + auto costs = pricePlanService.getConsumptionCostOfElectricityReadingsForEachPricePlan(meterId); |
50 | 48 |
|
51 | | - if (!costs) { |
52 | | - return {http::status::not_found, req.version()}; |
53 | | - } |
| 49 | + if (!costs) { |
| 50 | + return {http::status::not_found, req.version()}; |
| 51 | + } |
54 | 52 |
|
55 | | - std::vector<std::pair<std::string, float>> ordered_costs{costs->begin(), costs->end()}; |
56 | | - std::sort(ordered_costs.begin(), ordered_costs.end(), [](auto &cost_a, auto &cost_b) { |
57 | | - return cost_a.second < cost_b.second; |
58 | | - }); |
59 | | - ordered_costs.resize(std::min(limit, int(ordered_costs.size()))); |
| 53 | + std::vector<std::pair<std::string, float>> ordered_costs{costs->begin(), costs->end()}; |
| 54 | + std::sort(ordered_costs.begin(), ordered_costs.end(), |
| 55 | + [](auto &cost_a, auto &cost_b) { return cost_a.second < cost_b.second; }); |
| 56 | + ordered_costs.resize(std::min(limit, int(ordered_costs.size()))); |
60 | 57 |
|
61 | | - http::response<http::string_body> res{http::status::ok, req.version()}; |
62 | | - res.set(http::field::content_type, "application/json"); |
63 | | - res.keep_alive(req.keep_alive()); |
64 | | - auto results = nlohmann::json::array(); |
65 | | - std::transform(ordered_costs.begin(), ordered_costs.end(), std::back_inserter(results), |
66 | | - [](auto &cost) { return nlohmann::json{{cost.first, double(cost.second) / 10000}}; }); |
67 | | - nlohmann::json j; |
68 | | - j["recommend"] = results; |
69 | | - res.body() = j.dump(); |
70 | | - res.prepare_payload(); |
71 | | - return res; |
72 | | - } |
| 58 | + http::response<http::string_body> res{http::status::ok, req.version()}; |
| 59 | + res.set(http::field::content_type, "application/json"); |
| 60 | + res.keep_alive(req.keep_alive()); |
| 61 | + auto results = nlohmann::json::array(); |
| 62 | + std::transform(ordered_costs.begin(), ordered_costs.end(), std::back_inserter(results), [](auto &cost) { |
| 63 | + return nlohmann::json{{cost.first, double(cost.second) / 10000}}; |
| 64 | + }); |
| 65 | + nlohmann::json j; |
| 66 | + j["recommend"] = results; |
| 67 | + res.body() = j.dump(); |
| 68 | + res.prepare_payload(); |
| 69 | + return res; |
| 70 | + } |
73 | 71 |
|
74 | | -private: |
75 | | - PricePlanService &pricePlanService; |
| 72 | + private: |
| 73 | + PricePlanService &pricePlanService; |
76 | 74 | }; |
77 | 75 |
|
78 | | -#endif //DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H |
| 76 | +#endif // DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H |
0 commit comments