Add removal feature.
This commit is contained in:
parent
5f53180a60
commit
e25fb66dbc
@ -26,6 +26,7 @@ enum cmd_id {
|
|||||||
CMD_UNKNOWN = 0,
|
CMD_UNKNOWN = 0,
|
||||||
CMD_ADD,
|
CMD_ADD,
|
||||||
CMD_LIST,
|
CMD_LIST,
|
||||||
|
CMD_DEL,
|
||||||
CMD_HELP,
|
CMD_HELP,
|
||||||
CMD_VERSION,
|
CMD_VERSION,
|
||||||
};
|
};
|
||||||
@ -33,6 +34,7 @@ enum cmd_id {
|
|||||||
static const std::map<enum cmd_id, std::vector<std::string>> commands = {
|
static const std::map<enum cmd_id, std::vector<std::string>> commands = {
|
||||||
{ CMD_ADD, {"add", "new"} },
|
{ CMD_ADD, {"add", "new"} },
|
||||||
{ CMD_LIST, {"list", "ls"} },
|
{ CMD_LIST, {"list", "ls"} },
|
||||||
|
{ CMD_DEL, {"del", "rm"} },
|
||||||
{ CMD_HELP, {"help", "-h", "--help"} },
|
{ CMD_HELP, {"help", "-h", "--help"} },
|
||||||
{ CMD_VERSION, {"version", "-v", "--version"} },
|
{ CMD_VERSION, {"version", "-v", "--version"} },
|
||||||
};
|
};
|
||||||
@ -52,6 +54,7 @@ static inline void print_help(void) {
|
|||||||
std::cout << "COMMANDS:\n"
|
std::cout << "COMMANDS:\n"
|
||||||
"\tadd, new Add a new recipe to the database.\n"
|
"\tadd, new Add a new recipe to the database.\n"
|
||||||
"\tlist, ls List recipes with filters.\n"
|
"\tlist, ls List recipes with filters.\n"
|
||||||
|
"\tdel, rm Delete recipe by ID.\n"
|
||||||
"\thelp, -h, --help Show this help information.\n"
|
"\thelp, -h, --help Show this help information.\n"
|
||||||
"\tversion, -v, --version Show version information.\n"
|
"\tversion, -v, --version Show version information.\n"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
32
src/cmd.cpp
32
src/cmd.cpp
@ -105,3 +105,35 @@ int command_list(int argc, char *argv[]) {
|
|||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int command_delete(int argc, char *argv[]) {
|
||||||
|
int ret = EXIT_SUCCESS;
|
||||||
|
std::vector<int> recipe_ids;
|
||||||
|
|
||||||
|
if(argc < 1) {
|
||||||
|
std::cerr << "No specified IDs. Use 'help' for more information." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(not db_open()) {
|
||||||
|
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < argc; ++i) {
|
||||||
|
const int id = std::stoi(argv[i]);
|
||||||
|
|
||||||
|
if(not db_recipe_exists(id)) {
|
||||||
|
std::cerr << "No recipe exists with ID " << id << "." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
} else {
|
||||||
|
recipe_ids.push_back(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = (db_del_recipes(recipe_ids)) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
|
|
||||||
|
db_close();
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
@ -19,3 +19,4 @@
|
|||||||
|
|
||||||
int command_add(void);
|
int command_add(void);
|
||||||
int command_list(int argc, char *argv[]);
|
int command_list(int argc, char *argv[]);
|
||||||
|
int command_delete(int argc, char *argv[]);
|
||||||
|
50
src/db.cpp
50
src/db.cpp
@ -112,6 +112,56 @@ int db_add_recipe(const std::string &name, const std::string &description) {
|
|||||||
return db_get_recipe_id(name);
|
return db_get_recipe_id(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool db_del_recipe(const int id) {
|
||||||
|
if(not db)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(sqlite3_exec(db, std::format("DELETE FROM recipes WHERE id={}", id).c_str(),
|
||||||
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool db_del_recipes(const std::vector<int> &ids) {
|
||||||
|
std::string stmt = "DELETE FROM recipes WHERE id IN (";
|
||||||
|
|
||||||
|
if(not db)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
bool first = true;
|
||||||
|
for(auto id : ids) {
|
||||||
|
if(first)
|
||||||
|
first = false;
|
||||||
|
else
|
||||||
|
stmt += ",";
|
||||||
|
|
||||||
|
stmt += std::to_string(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt += ");";
|
||||||
|
|
||||||
|
if(sqlite3_exec(db, stmt.c_str(), nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool db_recipe_exists(const int id) {
|
||||||
|
bool exists = false;
|
||||||
|
|
||||||
|
if(not db)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
sqlite3_exec(db, std::format("SELECT id FROM recipes WHERE id={}", id).c_str(),
|
||||||
|
[](void *found,int,char**,char**) {
|
||||||
|
*static_cast<bool*>(found) = true;
|
||||||
|
return 0;
|
||||||
|
}, &exists, nullptr);
|
||||||
|
|
||||||
|
return exists;
|
||||||
|
}
|
||||||
|
|
||||||
int db_get_recipe_id(const std::string &name) {
|
int db_get_recipe_id(const std::string &name) {
|
||||||
if(not db)
|
if(not db)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -38,8 +38,11 @@ void db_close(void);
|
|||||||
* @return ID of newly created recipe, -1 if DB isn't open, -2 on other failure.
|
* @return ID of newly created recipe, -1 if DB isn't open, -2 on other failure.
|
||||||
*/
|
*/
|
||||||
int db_add_recipe(const std::string &name, const std::string &description);
|
int db_add_recipe(const std::string &name, const std::string &description);
|
||||||
|
bool db_del_recipe(const int id);
|
||||||
|
bool db_del_recipes(const std::vector<int> &ids);
|
||||||
int db_get_recipe_id(const std::string &name);
|
int db_get_recipe_id(const std::string &name);
|
||||||
static inline int db_recipe_exists(const std::string &name) {
|
bool db_recipe_exists(const int id);
|
||||||
|
static inline bool db_recipe_exists(const std::string &name) {
|
||||||
return (db_get_recipe_id(name) > 0);
|
return (db_get_recipe_id(name) > 0);
|
||||||
}
|
}
|
||||||
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
||||||
@ -54,7 +57,7 @@ std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingred
|
|||||||
*/
|
*/
|
||||||
int db_add_ingredient(const std::string &name);
|
int db_add_ingredient(const std::string &name);
|
||||||
int db_get_ingredient_id(const std::string &name);
|
int db_get_ingredient_id(const std::string &name);
|
||||||
static inline int db_ingredient_exists(const std::string &name) {
|
static inline bool db_ingredient_exists(const std::string &name) {
|
||||||
return (db_get_ingredient_id(name) > 0);
|
return (db_get_ingredient_id(name) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +70,7 @@ static inline int db_ingredient_exists(const std::string &name) {
|
|||||||
*/
|
*/
|
||||||
int db_add_tag(const std::string &name);
|
int db_add_tag(const std::string &name);
|
||||||
int db_get_tag_id(const std::string &name);
|
int db_get_tag_id(const std::string &name);
|
||||||
static inline int db_tag_exists(const std::string &name) {
|
static inline bool db_tag_exists(const std::string &name) {
|
||||||
return (db_get_tag_id(name) > 0);
|
return (db_get_tag_id(name) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +41,9 @@ int main(int argc, char *argv[]) {
|
|||||||
case CMD_LIST:
|
case CMD_LIST:
|
||||||
ret = command_list(argc - 1, argv + 1);
|
ret = command_list(argc - 1, argv + 1);
|
||||||
break;
|
break;
|
||||||
|
case CMD_DEL:
|
||||||
|
ret = command_delete(argc - 2, argv + 2);
|
||||||
|
break;
|
||||||
case CMD_HELP:
|
case CMD_HELP:
|
||||||
print_help();
|
print_help();
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user