From 8648c130ea5cfe598fdae45b4849e3820a4ff067 Mon Sep 17 00:00:00 2001 From: odweta Date: Sun, 16 Aug 2026 18:22:28 +0200 Subject: [PATCH] detail command working --- cli/dao.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/cli/dao.cpp b/cli/dao.cpp index 0d677bd..0335734 100644 --- a/cli/dao.cpp +++ b/cli/dao.cpp @@ -110,6 +110,17 @@ vector filter_by_status(const vector& tickets, const string return out; } +bool filter_by_id(ticket_t& ticket, const vector& tickets, const unsigned long id) { + for (const auto& t: tickets) { + if (t.id == id) { + ticket = t; + return true; + } + } + + return false; +} + vector Dao::fetch_ticket_get_by_status(string status) { string response; string url = this->base_url + "/tickets"; @@ -144,5 +155,40 @@ vector Dao::fetch_ticket_get_by_status(string status) { } ticket_t Dao::fetch_ticket_get_by_id(unsigned long id) { + string response; + string url = this->base_url + "/tickets"; + CURL *handle = curl_easy_init(); + + struct curl_slist* headers = nullptr; + headers = curl_slist_append(headers, "Content-Type: application/json"); + headers = curl_slist_append(headers, ("Authorization: Bearer " + this->api_key).c_str()); + + curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); + curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); + + curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response); + + CURLcode rc = curl_easy_perform(handle); + + curl_slist_free_all(headers); + curl_easy_cleanup(handle); + + if (rc != CURLE_OK) { + throw std::runtime_error(std::string("curl_easy_perform failed: ") + curl_easy_strerror(rc)); + } + + nlohmann::json j = nlohmann::json::parse(response); + + vector tickets = j.get>(); + ticket_t return_ticket; + + if (filter_by_id(return_ticket, tickets, id)) { + return return_ticket; + } else { + cout << "No ticket found with id " << id << ".\n"; + exit(1); + } } \ No newline at end of file