|
2 | 2 | #define DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H |
3 | 3 |
|
4 | 4 | #include <service/ElectricityReadingService.h> |
| 5 | +#include <service/PricePlanService.h> |
| 6 | +#include <configuration.h> |
5 | 7 |
|
6 | 8 | #include <ctime> |
7 | 9 | #include <iomanip> |
8 | 10 |
|
9 | 11 | #include <boost/beast/http.hpp> |
10 | 12 | #include <nlohmann/json.hpp> |
11 | 13 |
|
12 | | -#include <service/PricePlanService.h> |
| 14 | + |
13 | 15 | #include <iostream> |
14 | 16 |
|
15 | 17 | namespace http = boost::beast::http; |
16 | 18 |
|
17 | | -namespace price_detail { |
18 | | - auto renderCostsAsJson(const ElectricityReading &r) { |
19 | | - return nlohmann::json{{"hehe"}, |
20 | | - {"reading"}}; |
| 19 | +class PricePlanComparatorController { |
| 20 | +public: |
| 21 | + PricePlanComparatorController(PricePlanService &pricePlanService) : pricePlanService(pricePlanService) {} |
| 22 | + |
| 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); |
| 27 | + |
| 28 | + if (!costs) { |
| 29 | + return {http::status::not_found, req.version()}; |
21 | 30 | } |
22 | | - } // namespace price_detail |
23 | | - |
24 | | - class PricePlanComparatorController { |
25 | | - public: |
26 | | - PricePlanComparatorController(PricePlanService &pricePlanService) : pricePlanService(pricePlanService) {} |
27 | | - |
28 | | -// http::response<http::string_body> |
29 | | -// Read(const http::request<http::string_body> &req, const std::vector<std::string> &queries) { |
30 | | -// const auto &meterId = queries[0]; |
31 | | -// auto costs = pricePlanService.getConsumptionCostOfElectricityReadingsForEachPricePlan(meterId); |
32 | | -// for(auto &i:*costs){ |
33 | | -// std::cout<<i.first<<" "<<i.second<<std::endl; |
34 | | -// } |
35 | | -// |
36 | | -// if (!costs) { |
37 | | -// return {http::status::not_found, req.version()}; |
38 | | -// } |
39 | | -// http::response<http::string_body> res{http::status::ok, req.version()}; |
40 | | -// res.set(http::field::content_type, "application/json"); |
41 | | -// res.keep_alive(req.keep_alive()); |
42 | | -// auto results = nlohmann::json::array(); |
43 | | -// std::transform(costs->begin(), costs->end(), std::back_inserter(results), &price_detail::renderCostsAsJson); |
44 | | -// nlohmann::json j; |
45 | | -// j["readings"] = results; |
46 | | -// res.body() = j.dump(); |
47 | | -// res.prepare_payload(); |
48 | | -// return res; |
49 | | -// } |
50 | | - |
51 | | - private: |
52 | | - PricePlanService &pricePlanService; |
53 | | - }; |
| 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; |
| 43 | + } |
| 44 | + |
| 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); |
| 50 | + |
| 51 | + if (!costs) { |
| 52 | + return {http::status::not_found, req.version()}; |
| 53 | + } |
| 54 | + |
| 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()))); |
| 60 | + |
| 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, cost.second}}; }); |
| 67 | + nlohmann::json j; |
| 68 | + j["recommend"] = results; |
| 69 | + res.body() = j.dump(); |
| 70 | + res.prepare_payload(); |
| 71 | + return res; |
| 72 | + } |
| 73 | + |
| 74 | +private: |
| 75 | + PricePlanService &pricePlanService; |
| 76 | +}; |
54 | 77 |
|
55 | 78 | #endif //DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H |
0 commit comments