Skip to content

Commit

Permalink
delete command done
Browse files Browse the repository at this point in the history
  • Loading branch information
KDesp73 committed Aug 15, 2024
1 parent d033093 commit 6686837
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
5 changes: 1 addition & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

### Added

- edit command implemented

### Changed

- Updated version to 0.0.4
- edit command implemented


## [0.0.3] - 2024-08-15
Expand Down
1 change: 1 addition & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ int is_number(const char *str);
void append_line(const char* file, const char* line, size_t index);
int is_blank(const char *str);
char* char_repeat(char c, size_t count);
void clear_input_buffer();

#endif // UTILS_H
69 changes: 64 additions & 5 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,70 @@ void command_add(Options options)

void command_delete(Options options)
{
INFO("delete");
Options list_options = {
.version.full = VERSION_UNRELEASED,
.argc = 1,
.argv = (char**) malloc(sizeof(char*) * 4)
};

if (list_options.argv == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return;
}

list_options.argv[0] = malloc(strlen("-V") + 1);
if (list_options.argv[0] == NULL) {
fprintf(stderr, "Memory allocation for argv[0] failed\n");
free(list_options.argv);
return;
}
strcpy(list_options.argv[0], "-V");

command_list(list_options);

sqlite3* db;
sqlite3_open(SQLITE_DB, &db);

size_t count;
Entry* entries = select_entries(db, "version = 'unreleased'", "date DESC", &count);
sqlite3_close(db);

int index = -1;
char index_str[10];
printf("\n");
while (1) {
printf("Enter index to delete: ");
if (scanf("%9s", index_str) != 1) {
ERRO("Error reading input.\n");
clear_input_buffer();
continue;
}

if (!is_number(index_str)) {
ERRO("'%s' is not a number\n", index_str);
clear_input_buffer();
continue;
}

index = atoi(index_str);

if(index < 1 || index > count) {
if(count == 1) ERRO("index can only be 1\n");
else ERRO("index must be between 1 and %zu\n", count);
continue;
}

break;
}
char* condition = clib_format_text("message = '%s' AND date = '%s'", entries[index-1].message, entries[index-1].date.full);
query_builder_t* qb = create_query_builder();
delete_q(qb, TABLE_ENTRIES);
where_q(qb, condition);
char* query = build_query(qb);
sqlite_execute_sql(SQLITE_DB, query);
free(query);
free(condition);
free(qb);
}

char* update_config_version(const char* release_type)
Expand Down Expand Up @@ -215,10 +278,6 @@ void command_export(Options options)
INFO("Export complete.");
}

void clear_input_buffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF); // Discard characters until newline or EOF
}

void command_edit(Options options)
{
Expand Down
2 changes: 1 addition & 1 deletion src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void list_help()
void delete_help()
{
PTN("%sUSAGE%s", BOLD, RESET);
PTNI("%s list ", EXECUTABLE_NAME);
PTNI("%s delete <options>", EXECUTABLE_NAME);
PTN("");
PTN("%sOPTIONS%s", BOLD, RESET);
PTNI("-h --help Prints this message");
Expand Down
5 changes: 5 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ char* char_repeat(char c, size_t count)
return result;
}

void clear_input_buffer()
{
int c;
while ((c = getchar()) != '\n' && c != EOF); // Discard characters until newline or EOF
}
2 changes: 1 addition & 1 deletion todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ TODO: disable sqlite logging
TODO: resolve memory leaks
TODO: parse CHANGELOG.md into the database
TODO: use existing releases to populate the changelog
TODO: edit command
TODO: read the config file if it exists
TODO: check for sql injections
DONE: add methods to clib.h
Expand All @@ -15,3 +14,4 @@ DONE: fix usage in help message
DONE: export command
DONE: get command
DONE: list command
DONE: edit command

0 comments on commit 6686837

Please sign in to comment.