diff --git a/cli/build/tickets b/cli/build/tickets deleted file mode 100755 index 89f9aab..0000000 Binary files a/cli/build/tickets and /dev/null differ diff --git a/cli/dao.cpp b/cli/dao.cpp index 6237e5d..0d677bd 100644 --- a/cli/dao.cpp +++ b/cli/dao.cpp @@ -98,8 +98,49 @@ vector Dao::fetch_ticket_get_all() { return t; } -vector Dao::fetch_ticket_get_by_status(string status) { +vector filter_by_status(const vector& tickets, const string status) { + vector out; + for (const auto& t: tickets) { + if (t.status == status) { + out.push_back(t); + } + } + + return out; +} + +vector 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 tickets = j.get>(); + + return filter_by_status(tickets, status); } ticket_t Dao::fetch_ticket_get_by_id(unsigned long id) { diff --git a/cli/main.cpp b/cli/main.cpp index 7103c38..f93a239 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -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; +} \ No newline at end of file diff --git a/webserver/handlers.go b/webserver/handlers.go index 7270511..74b66f2 100644 --- a/webserver/handlers.go +++ b/webserver/handlers.go @@ -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;")