Compare commits
3 Commits
0b348e8c10
...
907f8a7b41
Author | SHA1 | Date | |
---|---|---|---|
907f8a7b41 | |||
009e3f09bf | |||
320346911c |
24
README.md
24
README.md
@ -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:
|
||||
|
7
TODO.md
7
TODO.md
@ -5,6 +5,7 @@
|
||||
- [ ] Add more documentation to `help` subcommand.
|
||||
- [ ] Add import/export functionality.
|
||||
- [ ] Properly align output columns from `list` subcommand.
|
||||
- [ ] Add feature for editing recipe name and description.
|
||||
- [ ] Name
|
||||
- [ ] Description
|
||||
- [X] Add feature for editing recipe name and description.
|
||||
- [X] Name
|
||||
- [X] Description
|
||||
- [ ] Allow for writing description in editor.
|
||||
|
@ -28,6 +28,8 @@ enum cmd_id {
|
||||
CMD_DEL,
|
||||
CMD_LIST,
|
||||
CMD_INFO,
|
||||
CMD_EDIT_NAME,
|
||||
CMD_EDIT_DESC,
|
||||
CMD_ADD_INGR,
|
||||
CMD_RM_INGR,
|
||||
CMD_ADD_TAG,
|
||||
@ -41,6 +43,8 @@ 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_EDIT_DESC, {"edit-description", "edit-desc"} },
|
||||
{ CMD_ADD_INGR, {"add-ingr"} },
|
||||
{ CMD_RM_INGR, {"rm-ingr"} },
|
||||
{ CMD_ADD_TAG, {"add-tag"} },
|
||||
@ -66,6 +70,8 @@ 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"
|
||||
"\tedit-description, edit-desc Change recipe description.\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"
|
||||
|
42
src/cmd.cpp
42
src/cmd.cpp
@ -168,6 +168,48 @@ 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_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) {
|
||||
db db;
|
||||
std::vector<std::string> ingr_list = split(ingredients, ",");
|
||||
|
@ -21,6 +21,8 @@ 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_edit_desc(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);
|
||||
|
20
src/db.cpp
20
src/db.cpp
@ -175,6 +175,26 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
const std::vector<std::string> &tags)
|
||||
{
|
||||
|
@ -59,6 +59,8 @@ 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);
|
||||
void update_recipe_desc(const int id, const std::string &new_desc);
|
||||
std::vector<struct recipe> get_recipes(const std::vector<std::string> &ingredients,
|
||||
const std::vector<std::string> &tags);
|
||||
|
||||
|
@ -50,6 +50,12 @@ 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_EDIT_DESC:
|
||||
ret = cmd_edit_desc(std::stoi(argv[2]));
|
||||
break;
|
||||
case CMD_ADD_INGR:
|
||||
ret = cmd_add_ingr(std::stoi(argv[2]), argv[3]);
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user