-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrate.cpp
122 lines (105 loc) · 4.13 KB
/
integrate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <SQLiteCpp/SQLiteCpp.h>
#include <string>
#include <map>
// Task Struct
struct Task {
int id;
std::string description;
bool completed;
};
// Initialize database: creates table if it doesn't exist
void initDatabase() {
try {
SQLite::Database db("tasks.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
db.exec("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, description TEXT, completed INTEGER);");
} catch (std::exception& e) {
std::cerr << "Error initializing database: " << e.what() << std::endl;
}
}
// Add a task to the database
void addTask(const std::string& description) {
try {
SQLite::Database db("tasks.db", SQLite::OPEN_READWRITE);
SQLite::Statement query(db, "INSERT INTO tasks (description, completed) VALUES (?, 0)");
query.bind(1, description);
query.exec();
std::cout << "Task added: " << description << std::endl;
} catch (std::exception& e) {
std::cerr << "Error adding task: " << e.what() << std::endl;
}
}
// List all tasks in the database
std::string listTasks() {
std::string response;
try {
SQLite::Database db("tasks.db", SQLite::OPEN_READONLY);
SQLite::Statement query(db, "SELECT id, description, completed FROM tasks");
response = "<ul>";
while (query.executeStep()) {
int id = query.getColumn(0);
std::string description = query.getColumn(1);
bool completed = query.getColumn(2);
response += "<li>" + std::to_string(id) + ": " + description + (completed ? " (Completed)" : " (Pending)") + "</li>";
}
response += "</ul>";
} catch (std::exception& e) {
std::cerr << "Error listing tasks: " << e.what() << std::endl;
}
return response;
}
// Mark task as completed in the database
void completeTask(int taskId) {
try {
SQLite::Database db("tasks.db", SQLite::OPEN_READWRITE);
SQLite::Statement query(db, "UPDATE tasks SET completed = 1 WHERE id = ?");
query.bind(1, taskId);
query.exec();
std::cout << "Task ID " << taskId << " marked as completed." << std::endl;
} catch (std::exception& e) {
std::cerr << "Error updating task: " << e.what() << std::endl;
}
}
// Parse CGI-style form data (e.g., action=add&description=Test+Task)
std::map<std::string, std::string> parseFormData(const std::string& query) {
std::map<std::string, std::string> formData;
size_t pos = 0;
std::string token;
std::string queryCopy = query;
while ((pos = queryCopy.find("&")) != std::string::npos) {
token = queryCopy.substr(0, pos);
size_t delimiterPos = token.find("=");
if (delimiterPos != std::string::npos) {
formData[token.substr(0, delimiterPos)] = token.substr(delimiterPos + 1);
}
queryCopy.erase(0, pos + 1);
}
return formData;
}
int main() {
initDatabase();
std::cout << "Content-type: text/html\n\n";
std::string queryString = getenv("QUERY_STRING") ? getenv("QUERY_STRING") : "";
if (!queryString.empty()) {
std::map<std::string, std::string> formData = parseFormData(queryString);
if (formData["action"] == "add" && formData.find("description") != formData.end()) {
addTask(formData["description"]);
} else if (formData["action"] == "complete" && formData.find("id") != formData.end()) {
int taskId = std::stoi(formData["id"]);
completeTask(taskId);
}
}
std::cout << "<h1>Task List</h1>";
std::cout << listTasks();
std::cout << "<form method='get'>";
std::cout << "<input type='text' name='description' placeholder='New Task'>";
std::cout << "<input type='hidden' name='action' value='add'>";
std::cout << "<input type='submit' value='Add Task'>";
std::cout << "</form>";
std::cout << "<form method='get'>";
std::cout << "<input type='number' name='id' placeholder='Task ID'>";
std::cout << "<input type='hidden' name='action' value='complete'>";
std::cout << "<input type='submit' value='Mark Completed'>";
std::cout << "</form>";
return 0;
}