Add feature to edit name.
And documentation for editing the description.
This commit is contained in:
@ -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"
|
||||
|
21
src/cmd.cpp
21
src/cmd.cpp
@ -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, ",");
|
||||
|
@ -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);
|
||||
|
10
src/db.cpp
10
src/db.cpp
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user