Compare commits

...

2 Commits

Author SHA1 Message Date
54ec5262d3 And the corresponding main.c 2023-07-12 21:29:17 +02:00
33f77d4cfc Implement add and delete actions 2023-07-12 21:28:54 +02:00
3 changed files with 120 additions and 12 deletions

View File

@ -53,12 +53,46 @@ sqlite3* get_db() {
} }
int add_task(task_t t) { //TODO int add_task(task_t t) {
sqlite3* db = get_db(); sqlite3* db = get_db();
sqlite3_stmt* stmt;
int ret = sqlite3_prepare_v2(db, "INSERT INTO tasks (id, text, done, due_to) VALUES (?1, ?2, ?3, ?4);", -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
return 1;
}
char str_id[10];
sprintf(str_id, "%d", (int)t.id);
sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC);
char str_done[2]; // Just a boolean
sprintf(str_done, "%d", t.done);
sqlite3_bind_text(stmt, 3, str_done, -1, SQLITE_STATIC);
char str_due_to[15];
sprintf(str_due_to, "%ld", t.due_to);
sqlite3_bind_text(stmt, 4, str_due_to, -1, SQLITE_STATIC);
ret = sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db); sqlite3_close(db);
(void)t;
return 1; if (ret != SQLITE_DONE) {
if (ret == 19) {
printf("Identifiant déjà utilisé\n");
} else {
printf("Return value: %d\n", ret);
}
return 1;
}
return 0;
} }
int update_task(task_t t) { //TODO int update_task(task_t t) { //TODO
@ -67,6 +101,28 @@ int update_task(task_t t) { //TODO
} }
void delete_task(int id) {
sqlite3* db = get_db();
sqlite3_stmt* stmt;
int ret = sqlite3_prepare_v2(db, "DELETE FROM tasks WHERE id=?1;", -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
exit(1);
}
char str_id[10];
sprintf(str_id, "%d", id);
sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
}
void get_task(int id, task_t* t) { void get_task(int id, task_t* t) {
sqlite3* db = get_db(); sqlite3* db = get_db();
sqlite3_stmt* stmt; sqlite3_stmt* stmt;

View File

@ -24,6 +24,11 @@ Mettre à jour une tâche
*/ */
int update_task(task_t t); int update_task(task_t t);
/*
Supprime la tâche [id] de la base de données. La tâche est supposée existante
*/
void delete_task(int id);
/* /*
Copie la tâche [id] dans *t si elle existe Copie la tâche [id] dans *t si elle existe
*/ */

View File

@ -70,7 +70,7 @@ int add(int argc, char* argv[]) {
if (argc < 3) { if (argc < 3) {
printf(BOLD YELLOW "Argument manquant\n" RESET); printf(BOLD YELLOW "Argument manquant\n" RESET);
help(argv[0]); help(argv[0]);
return 1; // TODO: trouver le bon code d'erreur return 1;
} }
time_t due_to = 0; time_t due_to = 0;
@ -110,7 +110,7 @@ int info(int argc, char* argv[]) {
if (t.id == -1) { if (t.id == -1) {
printf(BOLD YELLOW "Tâche inexistante. Vérifiez que l'identifiant est bien correct\n" RESET); printf(BOLD YELLOW "Tâche inexistante. Vérifiez que l'identifiant est bien correct\n" RESET);
exit(1); return 1;
} }
print_task(t); print_task(t);
@ -119,8 +119,57 @@ int info(int argc, char* argv[]) {
return 0; return 0;
} }
int mark_done(int argc, char* argv[]) {
if (argc < 3) {
printf(BOLD YELLOW "Argument manquant\n" RESET);
help(argv[0]);
return 1;
}
int id = strtol(argv[2], NULL, 10);
task_t t;
t.id = -1;
get_task(id, &t);
if (t.id == -1) {
printf(BOLD YELLOW "Tâche inexistante. Vérifiez que l'identifiant est bien correct\n" RESET);
return 1;
}
t.done = true;
update_task(t);
return 0;
}
int delete(int argc, char* argv[]) {
if (argc < 3) {
printf(BOLD YELLOW "Argument manquant\n" RESET);
help(argv[0]);
return 1;
}
int id = strtol(argv[2], NULL, 10);
task_t t;
t.id = -1;
get_task(id, &t);
if (t.id == -1) {
printf(BOLD YELLOW "Tâche inexistante. Vérifiez que l'identifiant est bien correct\n" RESET);
return 1;
}
delete_task(t.id);
return 0;
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
srand(time(NULL));
if (argc <= 1) { if (argc <= 1) {
help(argv[0]); help(argv[0]);
return 0; return 0;
@ -132,12 +181,10 @@ int main(int argc, char* argv[]) {
return list_tasks(); return list_tasks();
} else if (!strcmp(argv[1], "info")) { } else if (!strcmp(argv[1], "info")) {
return info(argc, argv); return info(argc, argv);
} else if (!strcmp(argv[1], "done")) { //TODO } else if (!strcmp(argv[1], "done")) {
printf("Not Implemented\n"); return mark_done(argc, argv);
return 1; } else if (!strcmp(argv[1], "rm")) {
} else if (!strcmp(argv[1], "rm")) { //TODO return delete(argc, argv);
printf("Not Implemented\n");
return 1;
} }
printf(YELLOW "Option invalide " BOLD "%s\n" RESET, argv[1]); printf(YELLOW "Option invalide " BOLD "%s\n" RESET, argv[1]);