Add list feature.

This commit is contained in:
2024-10-11 09:24:48 +02:00
parent deb3f2d13f
commit 5f53180a60
6 changed files with 154 additions and 22 deletions

View File

@@ -22,16 +22,13 @@
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <unistd.h>
int command_add(void) {
std::string name, description, ingredients, tags;
int recipe_id, ingredient_id, tag_id;
if(not db_open()) {
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Name: ";
getline(std::cin, name);
@@ -44,6 +41,11 @@ int command_add(void) {
std::cout << "Tags (comma separated): ";
getline(std::cin, tags);
if(not db_open()) {
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
return EXIT_FAILURE;
}
if((recipe_id = db_get_recipe_id(name)) <= 0)
recipe_id = db_add_recipe(name, description);
@@ -67,3 +69,39 @@ int command_add(void) {
return EXIT_SUCCESS;
}
int command_list(int argc, char *argv[]) {
std::vector<std::string> ingredients, tags;
int opt;
while((opt = getopt(argc, argv, "i:t:")) != -1) {
switch(opt) {
case 'i':
ingredients = split(optarg, ",");
for(auto &i : ingredients)
trim(i);
break;
case 't':
tags = split(optarg, ",");
for(auto &i : tags)
trim(i);
break;
case '?':
std::cerr << "Unknown option '" << static_cast<char>(optopt)
<< "'. Use 'help' for information." << std::endl;
return EXIT_FAILURE;
}
}
if(not db_open()) {
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
return EXIT_FAILURE;
}
for(const auto &recipe : db_get_recipes(ingredients, tags))
std::cout << recipe.id << " | " << recipe.name << " | " << recipe.description << std::endl;
db_close();
return EXIT_SUCCESS;
}