Add list feature.

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

View File

@ -25,12 +25,14 @@
enum cmd_id {
CMD_UNKNOWN = 0,
CMD_ADD,
CMD_LIST,
CMD_HELP,
CMD_VERSION,
};
static const std::map<enum cmd_id, std::vector<std::string>> commands = {
{ CMD_ADD, {"add", "new"} },
{ CMD_LIST, {"list", "ls"} },
{ CMD_HELP, {"help", "-h", "--help"} },
{ CMD_VERSION, {"version", "-v", "--version"} },
};
@ -48,7 +50,8 @@ static inline void print_help(void) {
print_usage();
std::cout << "COMMANDS:\n"
"\tadd, new Add a new recipe to the database\n"
"\tadd, new Add a new recipe to the database.\n"
"\tlist, ls List recipes with filters.\n"
"\thelp, -h, --help Show this help information.\n"
"\tversion, -v, --version Show version information.\n"
<< std::endl;

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;
}

View File

@ -18,3 +18,4 @@
#pragma once
int command_add(void);
int command_list(int argc, char *argv[]);

View File

@ -23,9 +23,10 @@
#include <filesystem>
#include <format>
#include <string>
#include <vector>
#define DB_VERSION 1
static sqlite3 *db = NULL;
static sqlite3 *db = nullptr;
int db_open(void) {
std::string xdg_data_home;
@ -55,14 +56,14 @@ int db_open(void) {
rc = sqlite3_open(db_path.c_str(), &db);
if(rc == SQLITE_OK && new_db) {
sqlite3_exec(db, "CREATE TABLE db_version(version INTEGER UNIQUE NOT NULL);", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE db_version(version INTEGER UNIQUE NOT nullptr);", nullptr, nullptr, nullptr);
//snprintf(insert_version_stmt, 64, "INSERT INTO db_version VALUES(%d);", DB_VERSION);
sqlite3_exec(db, std::format("INSERT INTO db_version VALUES({});", DB_VERSION).c_str(), NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE ingredients(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE recipe_tag(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE);", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE recipe_ingredient(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, ingredient_id INTEGER REFERENCES ingredients(id) ON DELETE CASCADE);", NULL, NULL, NULL);
sqlite3_exec(db, std::format("INSERT INTO db_version VALUES({});", DB_VERSION).c_str(), nullptr, nullptr, nullptr);
sqlite3_exec(db, "CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", nullptr, nullptr, nullptr);
sqlite3_exec(db, "CREATE TABLE ingredients(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", nullptr, nullptr, nullptr);
sqlite3_exec(db, "CREATE TABLE recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", nullptr, nullptr, nullptr);
sqlite3_exec(db, "CREATE TABLE recipe_tag(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE);", nullptr, nullptr, nullptr);
sqlite3_exec(db, "CREATE TABLE recipe_ingredient(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, ingredient_id INTEGER REFERENCES ingredients(id) ON DELETE CASCADE);", nullptr, nullptr, nullptr);
}
return rc == SQLITE_OK;
@ -94,7 +95,7 @@ int table_get_id_by_name(const std::string &table, const std::string &name) {
int id = 0;
if(sqlite3_exec(db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(),
query_id_cb, &id, NULL) != SQLITE_OK)
query_id_cb, &id, nullptr) != SQLITE_OK)
return -2;
return id;
@ -105,7 +106,7 @@ int db_add_recipe(const std::string &name, const std::string &description) {
return -1;
if(sqlite3_exec(db, std::format("INSERT INTO recipes(name,description) VALUES('{}','{}');", name, description).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
nullptr, nullptr, nullptr) != SQLITE_OK)
return -2;
return db_get_recipe_id(name);
@ -118,12 +119,88 @@ int db_get_recipe_id(const std::string &name) {
return table_get_id_by_name("recipes", name);
}
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
const std::vector<std::string> &tags)
{
std::vector<struct recipe> recipes;
std::string stmt = "SELECT id,name,description FROM recipes";
std::string filters;
if(not ingredients.empty() or not tags.empty())
filters += " WHERE";
if(not ingredients.empty()) {
bool first = true;
for(auto &i : ingredients) {
int id;
if(first)
first = false;
else
filters += " AND";
filters += " id IN (SELECT recipe_id FROM recipe_ingredient WHERE ingredient_id=";
// TODO: use throw?
if((id = db_get_ingredient_id(i)) < 0) {
std::cerr << "Failed to find ingredient '" << i << "'" << std::endl;
return std::vector<struct recipe>();
} else {
filters += std::to_string(id);
}
filters += ")";
}
}
if(not tags.empty()) {
if(not filters.empty())
filters += " AND";
bool first = true;
for(auto &i : tags) {
int id;
if(first)
first = false;
else
filters += " AND";
filters += " id IN (SELECT recipe_id FROM recipe_tag WHERE tag_id=";
// TODO: use throw?
if((id = db_get_tag_id(i)) < 0) {
std::cerr << "Failed to find tag '" << i << "'" << std::endl;
return std::vector<struct recipe>();
} else {
filters += std::to_string(id);
}
filters += ")";
}
}
stmt += filters + ";";
sqlite3_exec(db, stmt.c_str(),
[](void *recipe_list, int, char **col_data, char**) {
auto recipe_vec = static_cast<std::vector<struct recipe>*>(recipe_list);
recipe_vec->push_back({
std::atoi(col_data[0]),
col_data[1],
col_data[2] });
return 0;
}, &recipes, nullptr);
return recipes;
}
int db_add_ingredient(const std::string &name) {
if(not db)
return -1;
if(sqlite3_exec(db, std::format("INSERT INTO ingredients(name) VALUES(lower('{}'));", name).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
nullptr, nullptr, nullptr) != SQLITE_OK)
return -2;
return db_get_ingredient_id(name);
@ -141,7 +218,7 @@ int db_add_tag(const std::string &name) {
return -1;
if(sqlite3_exec(db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
nullptr, nullptr, nullptr) != SQLITE_OK)
return -2;
return db_get_tag_id(name);
@ -159,7 +236,7 @@ int db_conn_recipe_ingredient(int recipe_id, int ingredient_id) {
return -1;
if(sqlite3_exec(db, std::format("INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES({},{});", recipe_id, ingredient_id).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
nullptr, nullptr, nullptr) != SQLITE_OK)
return -2;
return 1;
@ -170,7 +247,7 @@ int db_conn_recipe_tag(int recipe_id, int tag_id) {
return -1;
if(sqlite3_exec(db, std::format("INSERT INTO recipe_tag(recipe_id, tag_id) VALUES({},{});", recipe_id, tag_id).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
nullptr, nullptr, nullptr) != SQLITE_OK)
return -2;
return 1;

View File

@ -18,6 +18,13 @@
#pragma once
#include <string>
#include <vector>
struct recipe {
int id;
std::string name;
std::string description;
};
int db_open(void);
void db_close(void);
@ -35,6 +42,8 @@ int db_get_recipe_id(const std::string &name);
static inline int db_recipe_exists(const std::string &name) {
return (db_get_recipe_id(name) > 0);
}
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
const std::vector<std::string> &tags);
/**
* @brief Add a new ingredient to the database.

View File

@ -24,6 +24,7 @@
int main(int argc, char *argv[]) {
enum cmd_id id;
int ret = EXIT_SUCCESS;
if(argc < 2) {
std::cerr << "Invalid number of arguments. Use 'help' sub-command." << std::endl;
@ -35,7 +36,10 @@ int main(int argc, char *argv[]) {
switch(id) {
case CMD_ADD:
command_add();
ret = command_add();
break;
case CMD_LIST:
ret = command_list(argc - 1, argv + 1);
break;
case CMD_HELP:
print_help();
@ -49,5 +53,5 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
return ret;
}