Skip to content

Commit 7f49bbc

Browse files
committed
Apply clang-format
1 parent 66a1f75 commit 7f49bbc

20 files changed

+534
-558
lines changed

main.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
#include "rest/server.h"
44

55
int main(int argc, char *argv[]) {
6-
if (argc != 4) {
7-
std::cerr << "Usage: app <address> <kPort> <concurrency>\n"
8-
<< "Example:\n"
9-
<< " http-server-async 0.0.0.0 8080 1\n";
10-
return EXIT_FAILURE;
11-
}
12-
auto const address = argv[1];
13-
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
14-
auto const threads = std::max<int>(1, std::atoi(argv[3]));
15-
server server{threads};
16-
server.run(address, port);
17-
char wait;
18-
std::cin >> wait;
19-
return EXIT_SUCCESS;
6+
if (argc != 4) {
7+
std::cerr << "Usage: app <address> <kPort> <concurrency>\n"
8+
<< "Example:\n"
9+
<< " http-server-async 0.0.0.0 8080 1\n";
10+
return EXIT_FAILURE;
11+
}
12+
auto const address = argv[1];
13+
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
14+
auto const threads = std::max<int>(1, std::atoi(argv[3]));
15+
server server{threads};
16+
server.run(address, port);
17+
char wait;
18+
std::cin >> wait;
19+
return EXIT_SUCCESS;
2020
}

rest/configuration.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ constexpr auto ANDREAS_SMART_METER_ID = "smart-meter-3";
2121
constexpr auto ALEXS_SMART_METER_ID = "smart-meter-4";
2222

2323
std::vector<PricePlan> pricePlans() {
24-
std::vector<PricePlan> price_plans;
25-
price_plans.push_back(PricePlan(MOST_EVIL_PRICE_PLAN_ID, DR_EVILS_DARK_ENERGY_ENERGY_SUPPLIER, 10, {}));
26-
price_plans.push_back(PricePlan(RENEWABLES_PRICE_PLAN_ID, THE_GREEN_ECO_ENERGY_SUPPLIER, 2, {}));
27-
price_plans.push_back(PricePlan(STANDARD_PRICE_PLAN_ID, POWER_FOR_EVERYONE_ENERGY_SUPPLIER, 1, {}));
28-
return price_plans;
24+
std::vector<PricePlan> price_plans;
25+
price_plans.push_back(PricePlan(MOST_EVIL_PRICE_PLAN_ID, DR_EVILS_DARK_ENERGY_ENERGY_SUPPLIER, 10, {}));
26+
price_plans.push_back(PricePlan(RENEWABLES_PRICE_PLAN_ID, THE_GREEN_ECO_ENERGY_SUPPLIER, 2, {}));
27+
price_plans.push_back(PricePlan(STANDARD_PRICE_PLAN_ID, POWER_FOR_EVERYONE_ENERGY_SUPPLIER, 1, {}));
28+
return price_plans;
2929
}
3030

3131
std::unordered_map<std::string, std::string> smart_meter_to_price_plan_accounts() {
32-
return {{SARAHS_SMART_METER_ID, MOST_EVIL_PRICE_PLAN_ID},
33-
{PETERS_SMART_METER_ID, RENEWABLES_PRICE_PLAN_ID},
34-
{CHARLIES_SMART_METER_ID, MOST_EVIL_PRICE_PLAN_ID},
35-
{ANDREAS_SMART_METER_ID, STANDARD_PRICE_PLAN_ID},
36-
{ALEXS_SMART_METER_ID, RENEWABLES_PRICE_PLAN_ID}};
32+
return {{SARAHS_SMART_METER_ID, MOST_EVIL_PRICE_PLAN_ID},
33+
{PETERS_SMART_METER_ID, RENEWABLES_PRICE_PLAN_ID},
34+
{CHARLIES_SMART_METER_ID, MOST_EVIL_PRICE_PLAN_ID},
35+
{ANDREAS_SMART_METER_ID, STANDARD_PRICE_PLAN_ID},
36+
{ALEXS_SMART_METER_ID, RENEWABLES_PRICE_PLAN_ID}};
3737
};
3838

3939
auto readings() {
40-
std::unordered_map<std::string, std::vector<ElectricityReading>> result;
41-
for (auto &[meter, plan] : smart_meter_to_price_plan_accounts()) {
42-
result[meter] = generator{}.generate(21);
43-
}
44-
return result;
40+
std::unordered_map<std::string, std::vector<ElectricityReading>> result;
41+
for (auto &[meter, plan] : smart_meter_to_price_plan_accounts()) {
42+
result[meter] = generator{}.generate(21);
43+
}
44+
return result;
4545
}
4646

4747
#endif // DEVELOPER_JOYOFENERGY_CPP_BEAST_CONFIGURATION_H

rest/controller/MeterReadingController.h

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,74 @@
66

77
#include <ctime>
88
#include <iomanip>
9+
#include <iostream>
910
#include <string>
1011

1112
#include <boost/beast/http.hpp>
1213
#include <nlohmann/json.hpp>
1314

14-
#include <iostream>
15-
1615
namespace http = boost::beast::http;
1716

1817
namespace detail {
19-
auto toRfc3339(std::chrono::time_point<std::chrono::system_clock> time) {
20-
std::stringstream ss;
21-
const auto ctime = std::chrono::system_clock::to_time_t(time);
22-
ss << std::put_time(std::gmtime(&ctime), "%FT%T");
23-
return ss.str();
24-
}
18+
auto toRfc3339(std::chrono::time_point<std::chrono::system_clock> time) {
19+
std::stringstream ss;
20+
const auto ctime = std::chrono::system_clock::to_time_t(time);
21+
ss << std::put_time(std::gmtime(&ctime), "%FT%T");
22+
return ss.str();
23+
}
2524

26-
auto fromRfc3339(const std::string &time) {
27-
std::tm tm = {};
28-
std::stringstream ss(time);
29-
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
30-
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
31-
return tp;
32-
}
25+
auto fromRfc3339(const std::string &time) {
26+
std::tm tm = {};
27+
std::stringstream ss(time);
28+
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
29+
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
30+
return tp;
31+
}
3332

34-
auto renderReadingAsJson(const ElectricityReading &r) {
35-
return nlohmann::json{{"time", toRfc3339(r.getTime())},
36-
{"reading", double(r.getReading()) / 10000}};
37-
}
33+
auto renderReadingAsJson(const ElectricityReading &r) {
34+
return nlohmann::json{{"time", toRfc3339(r.getTime())}, {"reading", double(r.getReading()) / 10000}};
35+
}
3836
} // namespace detail
3937

4038
class MeterReadingController {
41-
public:
42-
MeterReadingController(ElectricityReadingService &electricityReadingService,
43-
MeterReadingService &meterReadingService) :
44-
electricityReadingService(electricityReadingService), meterReadingService(meterReadingService) {}
39+
public:
40+
MeterReadingController(ElectricityReadingService &electricityReadingService, MeterReadingService &meterReadingService)
41+
: electricityReadingService(electricityReadingService), meterReadingService(meterReadingService) {}
4542

46-
http::response<http::string_body>
47-
Read(const http::request<http::string_body> &req, const std::vector<std::string> &queries) {
48-
const auto &meterId = queries[0];
49-
auto readings = electricityReadingService.GetReading(meterId);
43+
http::response<http::string_body> Read(const http::request<http::string_body> &req, const std::vector<std::string> &queries) {
44+
const auto &meterId = queries[0];
45+
auto readings = electricityReadingService.GetReading(meterId);
5046

51-
if (!readings) {
52-
return {http::status::not_found, req.version()};
53-
}
54-
http::response<http::string_body> res{http::status::ok, req.version()};
55-
res.set(http::field::content_type, "application/json");
56-
res.keep_alive(req.keep_alive());
57-
auto results = nlohmann::json::array();
58-
std::transform(readings->begin(), readings->end(), std::back_inserter(results), &detail::renderReadingAsJson);
59-
nlohmann::json j;
60-
j["readings"] = results;
61-
res.body() = j.dump();
62-
res.prepare_payload();
63-
return res;
47+
if (!readings) {
48+
return {http::status::not_found, req.version()};
6449
}
50+
http::response<http::string_body> res{http::status::ok, req.version()};
51+
res.set(http::field::content_type, "application/json");
52+
res.keep_alive(req.keep_alive());
53+
auto results = nlohmann::json::array();
54+
std::transform(readings->begin(), readings->end(), std::back_inserter(results), &detail::renderReadingAsJson);
55+
nlohmann::json j;
56+
j["readings"] = results;
57+
res.body() = j.dump();
58+
res.prepare_payload();
59+
return res;
60+
}
6561

66-
http::response<http::string_body>
67-
Store(const http::request<http::string_body> &req, const std::vector<std::string> &queries) {
68-
auto body = nlohmann::json::parse(req.body());
69-
auto smartMeterId = body["smartMeterId"];
70-
std::vector<ElectricityReading> electricityReadings;
71-
for (auto &electricityReading : body["electricityReadings"]) {
72-
electricityReadings.emplace_back(detail::fromRfc3339(electricityReading["time"]),
73-
electricityReading["reading"]);
74-
}
75-
meterReadingService.storeReadings(smartMeterId, electricityReadings);
76-
return {};
62+
http::response<http::string_body> Store(const http::request<http::string_body> &req,
63+
const std::vector<std::string> &queries) {
64+
auto body = nlohmann::json::parse(req.body());
65+
auto smartMeterId = body["smartMeterId"];
66+
std::vector<ElectricityReading> electricityReadings;
67+
for (auto &electricityReading : body["electricityReadings"]) {
68+
electricityReadings.emplace_back(detail::fromRfc3339(electricityReading["time"]), electricityReading["reading"]);
7769
}
70+
meterReadingService.storeReadings(smartMeterId, electricityReadings);
71+
return {};
72+
}
7873

79-
private:
80-
ElectricityReadingService &electricityReadingService;
81-
MeterReadingService &meterReadingService;
74+
private:
75+
ElectricityReadingService &electricityReadingService;
76+
MeterReadingService &meterReadingService;
8277
};
8378

8479
#endif // DEVELOPER_JOYOFENERGY_CPP_BEAST_METERREADINGCONTROLLER_H
Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,76 @@
11
#ifndef DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H
22
#define DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H
33

4+
#include <configuration.h>
45
#include <service/ElectricityReadingService.h>
56
#include <service/PricePlanService.h>
6-
#include <configuration.h>
77

88
#include <ctime>
99
#include <iomanip>
10+
#include <iostream>
1011

1112
#include <boost/beast/http.hpp>
1213
#include <nlohmann/json.hpp>
1314

14-
15-
#include <iostream>
16-
1715
namespace http = boost::beast::http;
1816

1917
class PricePlanComparatorController {
20-
public:
21-
PricePlanComparatorController(PricePlanService &pricePlanService) : pricePlanService(pricePlanService) {}
18+
public:
19+
PricePlanComparatorController(PricePlanService &pricePlanService) : pricePlanService(pricePlanService) {}
2220

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);
2725

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()};
4328
}
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+
}
4442

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);
5048

51-
if (!costs) {
52-
return {http::status::not_found, req.version()};
53-
}
49+
if (!costs) {
50+
return {http::status::not_found, req.version()};
51+
}
5452

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())));
6057

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+
}
7371

74-
private:
75-
PricePlanService &pricePlanService;
72+
private:
73+
PricePlanService &pricePlanService;
7674
};
7775

78-
#endif //DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H
76+
#endif // DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANCOMPARATORCONTROLLER_H

rest/domain/ElectricityReading.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
#include <ctime>
66

77
class ElectricityReading {
8-
public:
9-
using time_point_type = std::chrono::time_point<std::chrono::system_clock>;
8+
public:
9+
using time_point_type = std::chrono::time_point<std::chrono::system_clock>;
1010

11-
ElectricityReading(time_point_type time, size_t reading) : time(time), reading(reading) {}
11+
ElectricityReading(time_point_type time, size_t reading) : time(time), reading(reading) {}
1212

13-
time_point_type getTime() const { return time; }
13+
time_point_type getTime() const { return time; }
1414

15-
size_t getReading() const { return reading; }
15+
size_t getReading() const { return reading; }
1616

17-
private:
18-
time_point_type time;
19-
size_t reading;
17+
private:
18+
time_point_type time;
19+
size_t reading;
2020
};
2121

2222
#endif // DEVELOPER_JOYOFENERGY_CPP_BEAST_ELECTRICITYREADING_H

0 commit comments

Comments
 (0)