detail command working

This commit is contained in:
odweta
2026-08-16 18:22:28 +02:00
parent 686833c721
commit 8648c130ea
+46
View File
@@ -110,6 +110,17 @@ vector<ticket_t> filter_by_status(const vector<ticket_t>& tickets, const string
return out;
}
bool filter_by_id(ticket_t& ticket, const vector<ticket_t>& tickets, const unsigned long id) {
for (const auto& t: tickets) {
if (t.id == id) {
ticket = t;
return true;
}
}
return false;
}
vector<ticket_t> Dao::fetch_ticket_get_by_status(string status) {
string response;
string url = this->base_url + "/tickets";
@@ -144,5 +155,40 @@ vector<ticket_t> 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<ticket_t> tickets = j.get<vector<ticket_t>>();
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);
}
}