Add feature to edit recipe description.

This commit is contained in:
Nicolás A. Ortega Froysa 2024-10-21 18:44:38 +02:00
parent 320346911c
commit 009e3f09bf
7 changed files with 52 additions and 13 deletions

View File

@ -5,6 +5,6 @@
- [ ] Add more documentation to `help` subcommand. - [ ] Add more documentation to `help` subcommand.
- [ ] Add import/export functionality. - [ ] Add import/export functionality.
- [ ] Properly align output columns from `list` subcommand. - [ ] Properly align output columns from `list` subcommand.
- [ ] Add feature for editing recipe name and description. - [X] Add feature for editing recipe name and description.
- [X] Name - [X] Name
- [ ] Description - [X] Description

View File

@ -29,6 +29,7 @@ enum cmd_id {
CMD_LIST, CMD_LIST,
CMD_INFO, CMD_INFO,
CMD_EDIT_NAME, CMD_EDIT_NAME,
CMD_EDIT_DESC,
CMD_ADD_INGR, CMD_ADD_INGR,
CMD_RM_INGR, CMD_RM_INGR,
CMD_ADD_TAG, CMD_ADD_TAG,
@ -43,6 +44,7 @@ static const std::map<enum cmd_id, std::vector<std::string>> commands = {
{ CMD_LIST, {"list", "ls"} }, { CMD_LIST, {"list", "ls"} },
{ CMD_INFO, {"info", "i"} }, { CMD_INFO, {"info", "i"} },
{ CMD_EDIT_NAME, {"edit-name"} }, { CMD_EDIT_NAME, {"edit-name"} },
{ CMD_EDIT_DESC, {"edit-description", "edit-desc"} },
{ CMD_ADD_INGR, {"add-ingr"} }, { CMD_ADD_INGR, {"add-ingr"} },
{ CMD_RM_INGR, {"rm-ingr"} }, { CMD_RM_INGR, {"rm-ingr"} },
{ CMD_ADD_TAG, {"add-tag"} }, { CMD_ADD_TAG, {"add-tag"} },
@ -64,17 +66,18 @@ static inline void print_help(void) {
print_usage(); print_usage();
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"
"\tdel, rm Delete recipe by ID.\n" "\tdel, rm Delete recipe by ID.\n"
"\tlist, ls List recipes with filters.\n" "\tlist, ls List recipes with filters.\n"
"\tinfo Show recipe information.\n" "\tinfo Show recipe information.\n"
"\tedit-name Change recipe name.\n" "\tedit-name Change recipe name.\n"
"\tadd-ingr Add ingredient to a recipe.\n" "\tedit-description, edit-desc Change recipe description.\n"
"\trm-ingr Remove ingredient from a recipe.\n" "\tadd-ingr Add ingredient to a recipe.\n"
"\tadd-tag Add tag to a recipe.\n" "\trm-ingr Remove ingredient from a recipe.\n"
"\trm-tag Remove tag from a recipe.\n" "\tadd-tag Add tag to a recipe.\n"
"\thelp, -h, --help Show this help information.\n" "\trm-tag Remove tag from a recipe.\n"
"\tversion, -v, --version Show version information.\n" "\thelp, -h, --help Show this help information.\n"
"\tversion, -v, --version Show version information.\n"
<< std::endl; << std::endl;
} }

View File

@ -189,6 +189,27 @@ int cmd_edit_name(const int id) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int cmd_edit_desc(const int id) {
db db;
std::string new_desc;
db.open();
if(not db.recipe_exists(id)) {
std::cerr << "Recipe with ID " << id << " does not exist." << std::endl;
db.close();
return EXIT_FAILURE;
}
std::cout << "New name: ";
std::getline(std::cin, new_desc);
db.update_recipe_desc(id, new_desc);
db.close();
return EXIT_SUCCESS;
}
int cmd_add_ingr(const int recipe_id, const char *ingredients) { int cmd_add_ingr(const int recipe_id, const char *ingredients) {
db db; db db;
std::vector<std::string> ingr_list = split(ingredients, ","); std::vector<std::string> ingr_list = split(ingredients, ",");

View File

@ -22,6 +22,7 @@ int cmd_list(int argc, char *argv[]);
int cmd_delete(int argc, char *argv[]); int cmd_delete(int argc, char *argv[]);
int cmd_info(const int id); int cmd_info(const int id);
int cmd_edit_name(const int id); int cmd_edit_name(const int id);
int cmd_edit_desc(const int id);
int cmd_add_ingr(const int recipe_id, const char *ingredients); int cmd_add_ingr(const int recipe_id, const char *ingredients);
int cmd_rm_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_add_tag(const int recipe_id, const char *tags);

View File

@ -185,6 +185,16 @@ void db::update_recipe_name(const int id, const std::string &new_name) {
} }
} }
void db::update_recipe_desc(const int id, const std::string &new_desc) {
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("UPDATE OR IGNORE recipes SET description='{}' WHERE id={};", new_desc, id).c_str(),
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
throw std::runtime_error(std::format("Failed to modify description of recipe with ID {}.", id));
}
}
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,
const std::vector<std::string> &tags) const std::vector<std::string> &tags)
{ {

View File

@ -60,6 +60,7 @@ public:
bool recipe_exists(const int id); bool recipe_exists(const int id);
struct recipe get_recipe(const int id); struct recipe get_recipe(const int id);
void update_recipe_name(const int id, const std::string &new_name); void update_recipe_name(const int id, const std::string &new_name);
void update_recipe_desc(const int id, const std::string &new_desc);
std::vector<struct recipe> get_recipes(const std::vector<std::string> &ingredients, std::vector<struct recipe> get_recipes(const std::vector<std::string> &ingredients,
const std::vector<std::string> &tags); const std::vector<std::string> &tags);

View File

@ -53,6 +53,9 @@ int main(int argc, char *argv[]) {
case CMD_EDIT_NAME: case CMD_EDIT_NAME:
ret = cmd_edit_name(std::stoi(argv[2])); ret = cmd_edit_name(std::stoi(argv[2]));
break; break;
case CMD_EDIT_DESC:
ret = cmd_edit_desc(std::stoi(argv[2]));
break;
case CMD_ADD_INGR: case CMD_ADD_INGR:
ret = cmd_add_ingr(std::stoi(argv[2]), argv[3]); ret = cmd_add_ingr(std::stoi(argv[2]), argv[3]);
break; break;