Add feature to edit name.

And documentation for editing the description.
This commit is contained in:
Nicolás A. Ortega Froysa 2024-10-21 18:37:53 +02:00
parent 0b348e8c10
commit 320346911c
8 changed files with 61 additions and 4 deletions

View File

@ -97,7 +97,25 @@ $ menu-helper list
1 | Linguine Scampi | A lemony Italian pasta dish.
```
### Modifying Recipe Ingredients/Tags
### Modifying Recipes
#### Name & Description
To correct or otherwise modify the name or description of your recipe, you can
use the `edit-name` and `edit-description` subcommands. These will prompt you
for the new name or description respectively and overwrite what was previously
stored in the database:
```console
$ menu-helper edit-name 1
New name: Lenguine agli Scampi
$ menu-helper edit-description 1
New description: A zesty Italian pasta dish.
$ menu-helper list
1 | Linguine agli Scampi | A zesty Italian pasta dish.
```
#### Ingredients/Tags
If there are ingredients/tags which you forgot to add to a recipe, or that you
added erringly, you can correct this with the following commands:
@ -117,8 +135,8 @@ Scampi) that it is a pasta dish. We can do this with the following command:
```console
$ menu-helper add-tag 1 pasta
$ menu-helper info 1
Name: Linguine Scampi
Description: A lemony Italian pasta dish.
Name: Linguine agli Scampi
Description: A zesty Italian pasta dish.
ID: 1
Ingredients:

View File

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

View File

@ -28,6 +28,7 @@ enum cmd_id {
CMD_DEL,
CMD_LIST,
CMD_INFO,
CMD_EDIT_NAME,
CMD_ADD_INGR,
CMD_RM_INGR,
CMD_ADD_TAG,
@ -41,6 +42,7 @@ static const std::map<enum cmd_id, std::vector<std::string>> commands = {
{ CMD_DEL, {"del", "rm"} },
{ CMD_LIST, {"list", "ls"} },
{ CMD_INFO, {"info", "i"} },
{ CMD_EDIT_NAME, {"edit-name"} },
{ CMD_ADD_INGR, {"add-ingr"} },
{ CMD_RM_INGR, {"rm-ingr"} },
{ CMD_ADD_TAG, {"add-tag"} },
@ -66,6 +68,7 @@ static inline void print_help(void) {
"\tdel, rm Delete recipe by ID.\n"
"\tlist, ls List recipes with filters.\n"
"\tinfo Show recipe information.\n"
"\tedit-name Change recipe name.\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"

View File

@ -168,6 +168,27 @@ int cmd_info(const int id) {
return EXIT_SUCCESS;
}
int cmd_edit_name(const int id) {
db db;
std::string new_name;
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_name);
db.update_recipe_name(id, new_name);
db.close();
return EXIT_SUCCESS;
}
int cmd_add_ingr(const int recipe_id, const char *ingredients) {
db db;
std::vector<std::string> ingr_list = split(ingredients, ",");

View File

@ -21,6 +21,7 @@ int cmd_add(void);
int cmd_list(int argc, char *argv[]);
int cmd_delete(int argc, char *argv[]);
int cmd_info(const int id);
int cmd_edit_name(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);

View File

@ -175,6 +175,16 @@ struct recipe db::get_recipe(const int id) {
return recipe;
}
void db::update_recipe_name(const int id, const std::string &new_name) {
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 name='{}' WHERE id={};", new_name, id).c_str(),
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
throw std::runtime_error(std::format("Failed to modify name of recipe with ID {}.", id));
}
}
std::vector<struct recipe> db::get_recipes(const std::vector<std::string> &ingredients,
const std::vector<std::string> &tags)
{

View File

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

View File

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