Enable adding and removing tags from recipes.
This commit is contained in:
parent
5bcc598880
commit
5873ceb627
@ -30,6 +30,8 @@ enum cmd_id {
|
||||
CMD_INFO,
|
||||
CMD_ADD_INGR,
|
||||
CMD_RM_INGR,
|
||||
CMD_ADD_TAG,
|
||||
CMD_RM_TAG,
|
||||
CMD_HELP,
|
||||
CMD_VERSION,
|
||||
};
|
||||
@ -41,6 +43,8 @@ static const std::map<enum cmd_id, std::vector<std::string>> commands = {
|
||||
{ CMD_INFO, {"info", "i"} },
|
||||
{ CMD_ADD_INGR, {"add-ingr"} },
|
||||
{ CMD_RM_INGR, {"rm-ingr"} },
|
||||
{ CMD_ADD_TAG, {"add-tag"} },
|
||||
{ CMD_RM_TAG, {"rm-tag"} },
|
||||
{ CMD_HELP, {"help", "-h", "--help"} },
|
||||
{ CMD_VERSION, {"version", "-v", "--version"} },
|
||||
};
|
||||
@ -64,6 +68,8 @@ static inline void print_help(void) {
|
||||
"\tinfo Show recipe information.\n"
|
||||
"\tadd-ingr Add ingredient to a recipe.\n"
|
||||
"\trm-ingr Remove ingredient from a recipe.\n"
|
||||
"\tadd-tag Add tag to a recipe.\n"
|
||||
"\trm-tag Remove tag from a recipe.\n"
|
||||
"\thelp, -h, --help Show this help information.\n"
|
||||
"\tversion, -v, --version Show version information.\n"
|
||||
<< std::endl;
|
||||
|
57
src/cmd.cpp
57
src/cmd.cpp
@ -224,3 +224,60 @@ int cmd_rm_ingr(const int recipe_id, const char *ingredients) {
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int cmd_add_tag(const int recipe_id, const char *tags) {
|
||||
db db;
|
||||
std::vector<std::string> tag_list = split(tags, ",");
|
||||
|
||||
db.open();
|
||||
if(not db.recipe_exists(recipe_id)) {
|
||||
std::cerr << "Recipe with ID " << recipe_id << " does not exist." << std::endl;
|
||||
db.close();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for(auto &i : tag_list) {
|
||||
int tag_id;
|
||||
trim(i);
|
||||
|
||||
if(not db.tag_exists(i))
|
||||
tag_id = db.add_tag(i);
|
||||
else
|
||||
tag_id = db.get_ingredient_id(i);
|
||||
|
||||
db.conn_recipe_tag(recipe_id, tag_id);
|
||||
}
|
||||
|
||||
db.close();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int cmd_rm_tag(const int recipe_id, const char *tags) {
|
||||
db db;
|
||||
std::vector<std::string> tag_list = split(tags, ",");
|
||||
|
||||
db.open();
|
||||
if(not db.recipe_exists(recipe_id)) {
|
||||
std::cerr << "Recipe with ID " << recipe_id << " does not exist." << std::endl;
|
||||
db.close();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for(auto &i : tag_list) {
|
||||
int tag_id;
|
||||
trim(i);
|
||||
|
||||
if(not db.tag_exists(i)) {
|
||||
std::cerr << "Could not find tag '" << i << "'. Skipping!" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
tag_id = db.get_tag_id(i);
|
||||
db.disconn_recipe_tag(recipe_id, tag_id);
|
||||
}
|
||||
|
||||
db.close();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -23,3 +23,5 @@ int cmd_delete(int argc, char *argv[]);
|
||||
int cmd_info(const int id);
|
||||
int cmd_add_ingr(const int recipe_id, const char *ingredients);
|
||||
int cmd_rm_ingr(const int recipe_id, const char *ingredients);
|
||||
int cmd_add_tag(const int recipe_id, const char *tags);
|
||||
int cmd_rm_tag(const int recipe_id, const char *tags);
|
||||
|
10
src/db.cpp
10
src/db.cpp
@ -336,3 +336,13 @@ void db::conn_recipe_tag(int recipe_id, int tag_id) {
|
||||
recipe_id, tag_id));
|
||||
}
|
||||
}
|
||||
|
||||
void db::disconn_recipe_tag(int recipe_id, int tag_id) {
|
||||
if(not sqlite_db)
|
||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
||||
|
||||
if(sqlite3_exec(sqlite_db, std::format("DELETE FROM recipe_tag WHERE recipe_id={} AND tag_id={};", recipe_id, tag_id).c_str(),
|
||||
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
|
||||
throw std::runtime_error(std::format("Failed to disconnect recipe with ID {} from tag with ID {}.", recipe_id, tag_id));
|
||||
}
|
||||
}
|
||||
|
@ -97,4 +97,5 @@ public:
|
||||
void conn_recipe_ingredient(int recipe_id, int ingredient_id);
|
||||
void disconn_recipe_ingredient(int recipe_id, int ingredient_id);
|
||||
void conn_recipe_tag(int recipe_id, int tag_id);
|
||||
void disconn_recipe_tag(int recipe_id, int tag_id);
|
||||
};
|
||||
|
@ -56,6 +56,12 @@ int main(int argc, char *argv[]) {
|
||||
case CMD_RM_INGR:
|
||||
ret = cmd_rm_ingr(std::stoi(argv[2]), argv[3]);
|
||||
break;
|
||||
case CMD_ADD_TAG:
|
||||
ret = cmd_add_tag(std::stoi(argv[2]), argv[3]);
|
||||
break;
|
||||
case CMD_RM_TAG:
|
||||
ret = cmd_rm_tag(std::stoi(argv[2]), argv[3]);
|
||||
break;
|
||||
case CMD_HELP:
|
||||
print_help();
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user