Files
2026-08-16 18:22:28 +02:00

194 lines
5.9 KiB
C++

#include "include/dao.h"
using namespace std;
Dao::Dao() {
this->api_key = getenv("FRONTEND_API_KEY");
if (this->api_key.empty()) {
cout << "FRONTEND_API_KEY environment variable is not set" << endl;
exit(1);
}
this->base_url = getenv("BASE_API_URL");
if (this->base_url.empty()) {
cout << "BASE_API_URL environment variable is not set" << endl;
exit(1);
}
}
static size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) {
size_t total = size * nmemb;
std::string* s = static_cast<std::string*>(userp);
s->append(static_cast<char*>(contents), total);
return total;
}
ticket_t Dao::fetch_ticket_create(std::string title, std::string description) {
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());
string body = nlohmann::json{
{"title", title},
{"description", description}
}.dump();
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(handle, CURLOPT_POST, 1L);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, static_cast<long>(body.size()));
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);
ticket_t t = j.get<ticket_t>();
return t;
}
vector<ticket_t> Dao::fetch_ticket_get_all() {
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> t = j.get<vector<ticket_t>>();
return t;
}
vector<ticket_t> filter_by_status(const vector<ticket_t>& tickets, const string status) {
vector<ticket_t> out;
for (const auto& t: tickets) {
if (t.status == status) {
out.push_back(t);
}
}
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";
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>>();
return filter_by_status(tickets, 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);
}
}