list -status now works, solves #4

This commit is contained in:
odweta
2026-08-16 18:10:33 +02:00
parent 45e0353a6d
commit aec73befef
4 changed files with 45 additions and 130 deletions
BIN
View File
Binary file not shown.
+42 -1
View File
@@ -98,8 +98,49 @@ vector<ticket_t> Dao::fetch_ticket_get_all() {
return t;
}
vector<ticket_t> Dao::fetch_ticket_get_by_status(string status) {
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;
}
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) {
+3 -2
View File
@@ -1,8 +1,9 @@
#include "include/parser.h"
#include "include/dao.h"
int main(int argc, char *argv[], char *envp[]) {
int main(int argc, char *argv[]/*, char *envp[]*/) {
Dao *dao = new Dao();
int retval = parse_args(dao, argc, argv);
delete dao;
}
return retval;
}
-127
View File
@@ -11,133 +11,6 @@ import (
"github.com/go-chi/chi/v5"
)
/*
// GET /quotes
func getQuotes(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query(context.Background(), "SELECT * FROM hlaskovnik;")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer rows.Close()
var quotes []Quote
for rows.Next() {
var q Quote
rows.Scan(&q.Id, &q.Author, &q.Body)
quotes = append(quotes, q)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(quotes)
}
// POST /quotes
func createQuote(w http.ResponseWriter, r *http.Request) {
var q Quote
err := json.NewDecoder(r.Body).Decode(&q)
if err != nil {
http.Error(w, "Invalid JSON", 400)
return
}
err = db.QueryRow(
context.Background(),
"INSERT INTO hlaskovnik(author, body) VALUES($1, $2) RETURNING id;",
q.Author, q.Body,
).Scan(&q.Id)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(q)
}
// PUT /quotes/{id}
func updateQuote(w http.ResponseWriter, r *http.Request) {
// get ID from URL
idParam := chi.URLParam(r, "id")
id, err := strconv.Atoi(idParam)
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
}
// parse request body
var q Quote
err = json.NewDecoder(r.Body).Decode(&q)
if err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// update DB
commandTag, err := db.Exec(
context.Background(),
`UPDATE hlaskovnik
SET author=$1, body=$2
WHERE id=$3`,
q.Author, q.Body, id,
)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// check if row exists
if commandTag.RowsAffected() == 0 {
http.Error(w, "Quote not found", http.StatusNotFound)
return
}
// return updated object
q.Id = id
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(q)
}
// DELETE /quotes{id}
func deleteQuote(w http.ResponseWriter, r *http.Request) {
// get id from URL
idParam := chi.URLParam(r, "id")
// convert string → int
id, err := strconv.Atoi(idParam)
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
}
// execute delete
commandTag, err := db.Exec(
context.Background(),
"DELETE FROM hlaskovnik WHERE id=$1",
id,
)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// check if anything was deleted
if commandTag.RowsAffected() == 0 {
http.Error(w, "Quote not found", http.StatusNotFound)
return
}
// success response
w.WriteHeader(http.StatusNoContent) // 204
}
*/
// GET /tickets
func getTickets(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query(context.Background(), "SELECT id, title, description, status, created_at, updated_at FROM tickets ORDER BY id;")