createTicket working, solves #3

This commit is contained in:
odweta
2026-08-16 17:41:56 +02:00
parent 94d743edfc
commit 8ae9cccc69
13 changed files with 127 additions and 34 deletions
+58 -7
View File
@@ -1,19 +1,70 @@
#include <cstdlib>
#include <vector>
#include "include/dao.h"
using namespace std;
void Dao::populate_env() {
Dao::Dao() {
this->api_key = getenv("FRONTEND_API_KEY");
this->db_url = getenv("DB_URL");
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);
}
}
ticket_t Dao::fetch_ticket_create(string title, string description) {
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() {
}