Skip to content

Commit 6f98355

Browse files
committed
Fix
1 parent 44698eb commit 6f98355

File tree

7 files changed

+99
-21
lines changed

7 files changed

+99
-21
lines changed

sprint4/problems/leave_game/solution/conanfile.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ boost/1.78.0
33

44
[generators]
55
cmake_multi
6+
7+
[options]
8+
boost:header_only=True

sprint4/problems/leave_game/solution/src/application.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,22 +330,26 @@ Application::Application(model::Game& game, db::ConnectionPool& pool)
330330
}
331331

332332

333-
std::string Application::GetGameRecords() const {
334-
333+
std::string Application::GetGameRecords(int from, int to) const {
335334
try {
336-
auto connection_wrap =
337-
connection_pool_.GetConnection();
338-
335+
if (from < 0 || to <= from) {
336+
throw std::invalid_argument("Invalid range: 'from' must be >= 0 and 'to' > 'from'");
337+
}
338+
339+
auto connection_wrap = connection_pool_.GetConnection();
339340
pqxx::work work{*connection_wrap};
340341

341-
pqxx::result result = work.exec(R"(
342+
// Запрос с учетом офсета и лимита
343+
pqxx::result result = work.exec_params(R"(
342344
SELECT name, score, play_time_ms FROM retired_players
343-
ORDER BY score DESC, play_time_ms, name LIMIT 100;
344-
)");
345+
ORDER BY score DESC, play_time_ms, name
346+
OFFSET $1 LIMIT $2;
347+
)", from, to - from);
345348

346349
work.commit();
347350

348351
boost::json::array records_json;
352+
349353
for (const auto& row : result) {
350354
boost::json::object record;
351355
record["name"] = row["name"].c_str();
@@ -355,16 +359,14 @@ Application::Application(model::Game& game, db::ConnectionPool& pool)
355359
records_json.push_back(record);
356360
}
357361

358-
std::string response_json =
359-
boost::json::serialize(records_json);
360-
361-
return response_json;
362+
return boost::json::serialize(records_json);
362363

363364
} catch (const std::exception& e) {
364365
throw std::runtime_error(e.what());
365366
}
366367
}
367368

369+
368370
void Application::SavePlayerStatsToDB(std::shared_ptr<player::Player> player) {
369371
try {
370372
auto connection = connection_pool_.GetConnection();

sprint4/problems/leave_game/solution/src/application.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ namespace app {
159159
const std::string GetSerializedPlayersList(const Token& token) const;
160160
const std::string GetSerializedGameState(const Token& token) const;
161161

162-
std::string GetGameRecords() const;
162+
std::string GetGameRecords(int from = 0, int to = 10) const;
163163

164164
model::MapService& GetGameMapService() {
165165
return game_.GetMapService();

sprint4/problems/leave_game/solution/src/request_handler.cpp

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,84 @@ namespace http_handler {
219219
router_->AddRoute({"GET"}, "/api/v1/game/records",
220220
std::make_shared<HTTPResponseMaker>(
221221
[this](const StringRequest& req, const JsonResponseHandler& json_response) -> StringResponse {
222-
return this->GetRecordsRequest(json_response);
222+
return this->GetRecordsRequest(req, json_response);
223223
})
224224
);
225225
}
226226

227-
StringResponse ApiRequestHandler::GetRecordsRequest(const JsonResponseHandler& json_response) const {
228-
auto result = app_.GetGameRecords();
227+
std::unordered_map<std::string, std::string> ParseQueryString(const std::string& query) {
228+
std::unordered_map<std::string, std::string> params;
229+
230+
size_t start_pos = query.find('?');
231+
if (start_pos == std::string::npos) {
232+
return params;
233+
}
234+
235+
std::string query_string = query.substr(start_pos + 1);
236+
std::istringstream ss(query_string);
237+
std::string token;
238+
239+
while (std::getline(ss, token, '&')) {
240+
size_t eq_pos = token.find('=');
241+
if (eq_pos != std::string::npos) {
242+
std::string key = token.substr(0, eq_pos);
243+
std::string value = token.substr(eq_pos + 1);
244+
params[key] = value;
245+
}
246+
}
247+
248+
return params;
249+
}
250+
251+
std::optional<std::pair<int, int>> ParseQueryParams(const std::string& query) {
252+
auto params = ParseQueryString(query);
253+
int start = 0;
254+
int max_items = 100;
255+
256+
try {
257+
bool has_any_param = false;
258+
if (params.find("start") != params.end()) {
259+
start = std::stoi(params["start"]);
260+
has_any_param = true;
261+
}
262+
263+
if (params.find("maxItems") != params.end()) {
264+
max_items = std::stoi(params["maxItems"]);
265+
has_any_param = true;
266+
}
267+
268+
if (has_any_param) {
269+
return std::make_pair(start, max_items);
270+
}
271+
272+
return std::nullopt;
273+
274+
} catch (...) {
275+
return std::nullopt; // Ошибка парсинга
276+
}
277+
278+
return std::nullopt;
279+
}
280+
281+
StringResponse
282+
ApiRequestHandler::GetRecordsRequest(const StringRequest& req,
283+
const JsonResponseHandler& json_response) const {
284+
std::string result;
285+
286+
auto params = ParseQueryParams(req.body().c_str());
287+
288+
if (params.has_value()) {
289+
int from = params->first;
290+
int to = params->second;
291+
292+
if (to > 100) {
293+
return ErrorHandler::MakeBadRequestResponse(json_response);
294+
}
295+
296+
result = app_.GetGameRecords(from, to);
297+
} else {
298+
result = app_.GetGameRecords();
299+
}
229300

230301
return json_response(http::status::ok, std::move(result), ContentType::APP_JSON);
231302
}

sprint4/problems/leave_game/solution/src/request_handler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ namespace http_handler {
120120
const JsonResponseHandler& json_response) const;
121121
StringResponse GetGameState(const StringRequest& req,
122122
const JsonResponseHandler& json_response) const;
123-
StringResponse GetRecordsRequest(const JsonResponseHandler& json_response) const;
124-
123+
StringResponse
124+
GetRecordsRequest(const StringRequest& req,
125+
const JsonResponseHandler& json_response) const;
125126
private:
126127

127128
template <typename Fn>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
22 serialization::archive 19 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 4 map3 5 Map 3 3 0 0 9 0 0 0 0 0 2 2 2 16 12 7 12 0 20 0 20 14 14 16 14 14 8 7 8 14 2 7 20 7 12 0 20 0 20 14 8 14 2 16 14 16 0 0 5 0 0 0 0 0 4 2 0 0 6 3 14 2 4 3 4 9 2 5 9 9 4 3 14 9 4 3 0 0 1 0 0 0 0 0 2 o1 8 14 0 0 1 -1 3.00000000000000000e+00 0 0 2 13 0 0 0 1 30 0 10 0 0 2 1 0 1 22 1 0
1+
22 serialization::archive 19 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 4 map3 5 Map 3 3 0 0 9 0 0 0 0 0 2 2 2 16 12 7 12 0 20 0 20 14 14 16 14 14 8 7 8 14 2 7 20 7 12 0 20 0 20 14 8 14 2 16 14 16 0 0 5 0 0 0 0 0 4 2 0 0 6 3 14 2 4 3 4 9 2 5 9 9 4 3 14 9 4 3 0 0 1 0 0 0 0 0 2 o1 8 14 0 0 1 -1 3.00000000000000000e+00 0 0 2 13 0 0 0 0 10 1 30 0 0 3 1 0 1 22 1 0
22
0 6 Digger 0 0 0 0 9.90700000000007819e+00 1.44000000000000004e+01 0 0 0.00000000000000000e+00 0.00000000000000000e+00 1 110 0 0 0 2 0 0 0 5 0 6 1 3.00000000000000000e+00 22
3-
1 4 Dog5 1.35349290783293696e+01 1.40265300385123037e+01 0.00000000000000000e+00 0.00000000000000000e+00 2 0 1 0 0 3.00000000000000000e+00 0 0 2 0 0 0 7 1 1.65538026667696627e+01 -1.20818678151292269e-01 8 1 6.51335613487967180e+00 1.58987953181691566e+01 9 0 0 0 1 13 0 0 0 0 0 0 0 3 13 0 0 0 4 map1 0 0 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 4 town 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 4 map3 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 0 0 1 13 0 0 0 4 map3 0 0 0 3 0 4 map1 5 Map 1 3 4 0 0 0 40 0 40 0 40 30 40 30 0 30 0 0 0 30 1 0 5 5 30 20 1 0 2 o0 40 30 5 0 4.00000000000000000e+00 4 town 4 Town 3 22 0 0 0 40 0 40 0 40 30 40 30 0 30 0 15 40 15 20 0 20 30 0 22 17 22 17 18 17 27 10 22 10 30 0 10 10 10 10 10 10 5 10 5 20 5 20 30 20 40 20 40 10 40 20 40 30 40 30 0 30 10 20 25 25 25 30 25 30 30 20 20 25 20 30 15 30 20 35 20 40 20 40 25 35 25 30 30 30 35 20 0 2 2 6 6 12 7 6 6 22 2 6 11 32 2 6 11 22 16 4 3 33 16 4 3 34 21 4 3 22 21 4 3 22 26 5 3 34 26 5 3 28 21 4 3 2 16 5 5 9 16 6 3 12 24 3 4 2 24 6 4 12 1 7 2 11 34 4 4 22 31 6 2 22 35 6 4 17 41 7 4 1 0 2 o0 40 30 5 0 3.00000000000000000e+00 4 map3 5 Map 3 3 9 0 2 2 2 16 12 7 12 0 20 0 20 14 14 16 14 14 8 7 8 14 2 7 20 7 12 0 20 0 20 14 8 14 2 16 14 16 5 0 4 2 6 3 14 2 4 3 4 9 2 5 9 9 4 3 14 9 4 3 1 0 2 o1 8 14 1 -1 3.00000000000000000e+00 3 13 0 4 map3 2 4 town 1 4 map1 0 3.00000000000000000e+00 1.00000000000000000e+01 0 0 0 0 0 0 0 0
3+
1 4 Dog5 1.35349290783293696e+01 1.40265300385123037e+01 0.00000000000000000e+00 0.00000000000000000e+00 2 0 1 0 0 3.00000000000000000e+00 22
4+
2 4 Dog3 7.62678135069243179e+00 9.77732133391603675e+00 0.00000000000000000e+00 0.00000000000000000e+00 2 70 2 0 0 3.00000000000000000e+00 0 0 3 0 0 0 7 1 1.65538026667696627e+01 -1.20818678151292269e-01 11 1 1.36327272872589198e+01 1.59987170344796308e+01 12 1 1.39779797575944364e+01 1.59194406548155314e+01 13 0 0 0 1 13 0 0 0 0 0 0 0 3 13 0 0 0 4 map3 0 0 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 4 town 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 4 map1 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 0 0 1 13 0 0 0 4 map3 0 0 0 3 0 4 map1 5 Map 1 3 4 0 0 0 40 0 40 0 40 30 40 30 0 30 0 0 0 30 1 0 5 5 30 20 1 0 2 o0 40 30 5 0 4.00000000000000000e+00 4 town 4 Town 3 22 0 0 0 40 0 40 0 40 30 40 30 0 30 0 15 40 15 20 0 20 30 0 22 17 22 17 18 17 27 10 22 10 30 0 10 10 10 10 10 10 5 10 5 20 5 20 30 20 40 20 40 10 40 20 40 30 40 30 0 30 10 20 25 25 25 30 25 30 30 20 20 25 20 30 15 30 20 35 20 40 20 40 25 35 25 30 30 30 35 20 0 2 2 6 6 12 7 6 6 22 2 6 11 32 2 6 11 22 16 4 3 33 16 4 3 34 21 4 3 22 21 4 3 22 26 5 3 34 26 5 3 28 21 4 3 2 16 5 5 9 16 6 3 12 24 3 4 2 24 6 4 12 1 7 2 11 34 4 4 22 31 6 2 22 35 6 4 17 41 7 4 1 0 2 o0 40 30 5 0 3.00000000000000000e+00 4 map3 5 Map 3 3 9 0 2 2 2 16 12 7 12 0 20 0 20 14 14 16 14 14 8 7 8 14 2 7 20 7 12 0 20 0 20 14 8 14 2 16 14 16 5 0 4 2 6 3 14 2 4 3 4 9 2 5 9 9 4 3 14 9 4 3 1 0 2 o1 8 14 1 -1 3.00000000000000000e+00 3 13 0 4 map1 0 4 town 1 4 map3 2 3.00000000000000000e+00 1.00000000000000000e+01 0 0 0 0 0 0 0 0

sprint4/problems/leave_game/solution/src/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace util {
130130
http::response<http::file_body> res;
131131
res.version(11); // HTTP/1.1
132132
res.result(http::status::ok);
133-
const std::string_view content_type = MimeType(ExtractFileExtension(file_path));
133+
const std::string_view content_type = MimeType(std::string_view{ExtractFileExtension(file_path)});
134134
res.set(http::field::content_type, content_type);
135135

136136
http::file_body::value_type file;

0 commit comments

Comments
 (0)