summaryrefslogtreecommitdiff
path: root/cmd/cmd_todo.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/cmd_todo.c')
-rw-r--r--cmd/cmd_todo.c181
1 files changed, 179 insertions, 2 deletions
diff --git a/cmd/cmd_todo.c b/cmd/cmd_todo.c
index 2b3993b..50218b0 100644
--- a/cmd/cmd_todo.c
+++ b/cmd/cmd_todo.c
@@ -1,10 +1,105 @@
#include "cmd_todo.h"
+static int add_todo(sqlite3 *db, char *user, char *todo)
+{
+ int rc;
+
+ char sql_add_table[1024];
+ snprintf(sql_add_table, 1024, "INSERT INTO todo(user,todo) VALUES('%s','%s');", user, todo);
+ printf("%s\n", sql_add_table);
+
+ if ((rc = sqlite3_exec(db, sql_add_table, 0, 0, 0)) != SQLITE_OK)
+ {
+ printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db));
+ return -1;
+ }
+
+ return 0;
+}
+
+static int cb_list_todo_table(void *param1, int argc, char **argv, char **cname)
+{
+ int i;
+
+ sds *out = (sds *)param1;
+ //sds local_out = sdsempty();
+
+ for (i=0; i<argc; i++)
+ {
+ printf("%s = %s, ", cname[i], argv[i] ? argv[i] : "NULL");
+ if (strncmp(cname[i],"id",2) == 0)
+ {
+ *out = sdscat(*out, argv[i]);
+ *out = sdscat(*out, " - ");
+ } else if (strncmp(cname[i],"todo",4) == 0)
+ {
+ *out = sdscat(*out, argv[i]);
+ *out = sdscat(*out, "\n");
+ }
+ }
+
+ printf("\n");
+
+ return 0;
+}
+
+static int list_todo(sqlite3 *db, char *user, sds *out)
+{
+ int rc;
+ char *err_msg = NULL;
+
+ char sql_list_table[1024];
+ snprintf(sql_list_table, 1024, "SELECT * FROM todo WHERE user='%s'", user);
+
+ if ((rc = sqlite3_exec(db, sql_list_table, cb_list_todo_table, out, &err_msg )) != SQLITE_OK)
+ {
+ printf("Cant list todo: %s\n", err_msg);
+
+ sqlite3_free(err_msg);
+
+ return -1;
+ }
+
+ return 0;
+}
+
+static int del_todo(sqlite3 *db, char *user, int id)
+{
+ int rc;
+ char *err_msg;
+
+ char sql_del_table[1024];
+ snprintf(sql_del_table, 1024, "DELETE FROM todo WHERE user='%s' AND id=%d;", user, id);
+
+ if ((rc = sqlite3_exec(db, sql_del_table, 0, 0, &err_msg )) != SQLITE_OK)
+ {
+ printf("Cant del todo: %s\n", err_msg);
+
+ sqlite3_free(err_msg);
+
+ return -1;
+ }
+
+ return 0;
+}
+
void *cmd_todo(void *data)
{
char *ret = NULL;
char *req_data = NULL;
int fret;
+ int rc;
+ int i;
+
+ int count;
+ sds params = sdsempty();
+ sds out_result = sdsempty(), out_todo = sdsempty();
+ sds *tokens;
+
+ sqlite3 *db=NULL;
+ sqlite3_stmt *res=NULL;
+ char *err_msg = NULL;
+ int table_todo_exists=1;
rpc_call_request *req = NULL;
rpc_call_response *resp = NULL;
@@ -42,8 +137,90 @@ void *cmd_todo(void *data)
//----------------------------------------------------------------------------
//main code
- PRINT("%s-%s-%s-%s-%s\n", req->method, req->params, req->user, req->mask, req->server);
+ PRINT("(%s)-(%s)-(%s)-(%s)-(%s)\n", req->method, req->params, req->user, req->mask, req->server);
+
+ if ((rc = sqlite3_open("todo.db", &db)) != SQLITE_OK)
+ {
+ printf("Cannot open todo database: %s\n", sqlite3_errmsg(db));
+ sqlite3_close(db);
+
+ return 1;
+ }
+
+ //check if table excists
+ char *sql_check_table = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='todo'";
+ if ((rc = sqlite3_prepare_v2(db, sql_check_table, -1, &res, 0)) != SQLITE_OK)
+ {
+ printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db));
+ sqlite3_close(db);
+ return 1;
+ }
+
+ rc = sqlite3_step(res);
+ if (rc == SQLITE_ROW)
+ {
+ int iret = sqlite3_column_int(res, 0);
+ //printf("%d\n", iret);
+ if (iret == 0)
+ {
+ table_todo_exists = 0;
+ } else
+ {
+ table_todo_exists = 1;
+ }
+ }
+
+ //if table doesnt excists then create new one
+ if (table_todo_exists == 0)
+ {
+ char *sql_create_table = "CREATE TABLE todo(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, todo TEXT)";
+ if ((rc = sqlite3_exec(db, sql_create_table, 0, 0, &err_msg)) != SQLITE_OK)
+ {
+ printf("Cant create table: %s\n", err_msg);
+ sqlite3_free(err_msg);
+ sqlite3_close(db);
+ }
+ }
+
+ params = sdsnew(req->params);
+ tokens = sdssplitargs(params, &count);
+ if (strncmp(tokens[1],"add",3) == 0)
+ {
+ PRINT("ADD\n");
+ if (count > 2)
+ {
+ sds sAdd = sdsempty();
+ for (i=2;i<count;i++)
+ {
+ sAdd = sdscat(sAdd, tokens[i]);
+ sAdd = sdscat(sAdd, " ");
+ }
+ add_todo(db, req->user, sAdd);
+ }
+ } else if (strncmp(tokens[1],"del",3) == 0)
+ {
+ PRINT("DEL\n");
+ if (count > 2)
+ {
+ int id = atoi(tokens[2]);
+ del_todo(db, req->user, id);
+ }
+
+ } else if (strncmp(tokens[1],"list",4) == 0)
+ {
+ PRINT("LIST\n");
+ list_todo(db, req->user, &out_todo);
+ PRINT("%s\n",out_todo);
+ out_result = sdscatsds(out_result, out_todo);
+ out_result[sdslen(out_result)-1] = 0; //dirty hack mate
+ sdsfree(out_todo);
+ } else
+ {
+
+ }
+ sqlite3_finalize(res);
+ sqlite3_close(db);
//----------------------------------------------------------------------------
//prepare response
@@ -51,7 +228,7 @@ void *cmd_todo(void *data)
nb_init(nb_resp);
PNL();
- resp = rpc_call_resp_new("Success","None",1);
+ resp = rpc_call_resp_new(out_result,"None",1);
resp->user = alloc_new_str(" ");
resp->server = alloc_new_str(" ");
resp->mask = alloc_new_str(" ");