Add info command.
This commit is contained in:
parent
c0c6959774
commit
1a590b477e
@ -27,6 +27,7 @@ enum cmd_id {
|
|||||||
CMD_ADD,
|
CMD_ADD,
|
||||||
CMD_LIST,
|
CMD_LIST,
|
||||||
CMD_DEL,
|
CMD_DEL,
|
||||||
|
CMD_INFO,
|
||||||
CMD_HELP,
|
CMD_HELP,
|
||||||
CMD_VERSION,
|
CMD_VERSION,
|
||||||
};
|
};
|
||||||
@ -35,6 +36,7 @@ static const std::map<enum cmd_id, std::vector<std::string>> commands = {
|
|||||||
{ CMD_ADD, {"add", "new"} },
|
{ CMD_ADD, {"add", "new"} },
|
||||||
{ CMD_LIST, {"list", "ls"} },
|
{ CMD_LIST, {"list", "ls"} },
|
||||||
{ CMD_DEL, {"del", "rm"} },
|
{ CMD_DEL, {"del", "rm"} },
|
||||||
|
{ CMD_INFO, { "info" } },
|
||||||
{ CMD_HELP, {"help", "-h", "--help"} },
|
{ CMD_HELP, {"help", "-h", "--help"} },
|
||||||
{ CMD_VERSION, {"version", "-v", "--version"} },
|
{ CMD_VERSION, {"version", "-v", "--version"} },
|
||||||
};
|
};
|
||||||
@ -55,6 +57,7 @@ static inline void print_help(void) {
|
|||||||
"\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"
|
"\tlist, ls List recipes with filters.\n"
|
||||||
"\tdel, rm Delete recipe by ID.\n"
|
"\tdel, rm Delete recipe by ID.\n"
|
||||||
|
"\tinfo Show recipe information.\n"
|
||||||
"\thelp, -h, --help Show this help information.\n"
|
"\thelp, -h, --help Show this help information.\n"
|
||||||
"\tversion, -v, --version Show version information.\n"
|
"\tversion, -v, --version Show version information.\n"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
39
src/cmd.cpp
39
src/cmd.cpp
@ -137,3 +137,42 @@ int command_delete(int argc, char *argv[]) {
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int command_info(const int id) {
|
||||||
|
struct recipe recipe;
|
||||||
|
std::vector<std::string> ingredients, tags;
|
||||||
|
int ret = EXIT_SUCCESS;
|
||||||
|
|
||||||
|
if(not db_open()) {
|
||||||
|
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(not db_recipe_exists(id)) {
|
||||||
|
std::cerr << "No recipe with ID '" << id << "'";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
recipe = db_get_recipe(id);
|
||||||
|
ingredients = db_get_recipe_ingredients(id);
|
||||||
|
tags = db_get_recipe_tags(id);
|
||||||
|
|
||||||
|
db_close();
|
||||||
|
|
||||||
|
std::cout << "Name: " << recipe.name << "\n"
|
||||||
|
<< "Description: " << recipe.description << "\n"
|
||||||
|
<< "ID: " << recipe.id << "\n"
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
std::cout << "Ingredients:" << std::endl;
|
||||||
|
for(auto &ingredient : ingredients)
|
||||||
|
std::cout << "\t- " << ingredient << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "Tags:" << std::endl;
|
||||||
|
for(auto &tag : tags)
|
||||||
|
std::cout << "\t- " << tag << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
@ -20,3 +20,4 @@
|
|||||||
int command_add(void);
|
int command_add(void);
|
||||||
int command_list(int argc, char *argv[]);
|
int command_list(int argc, char *argv[]);
|
||||||
int command_delete(int argc, char *argv[]);
|
int command_delete(int argc, char *argv[]);
|
||||||
|
int command_info(const int id);
|
||||||
|
36
src/db.cpp
36
src/db.cpp
@ -167,6 +167,18 @@ int db_get_recipe_id(const std::string &name) {
|
|||||||
return table_get_id_by_name("recipes", name);
|
return table_get_id_by_name("recipes", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct recipe db_get_recipe(const int id) {
|
||||||
|
struct recipe recipe;
|
||||||
|
|
||||||
|
sqlite3_exec(db, std::format("SELECT * FROM recipes WHERE id={};", id).c_str(),
|
||||||
|
[](void *recipe, int, char **col_data, char**) {
|
||||||
|
*static_cast<struct recipe*>(recipe) = { std::atoi(col_data[0]), col_data[1], col_data[2] };
|
||||||
|
return 0;
|
||||||
|
}, &recipe, nullptr);
|
||||||
|
|
||||||
|
return recipe;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
||||||
const std::vector<std::string> &tags)
|
const std::vector<std::string> &tags)
|
||||||
{
|
{
|
||||||
@ -254,6 +266,18 @@ int db_add_ingredient(const std::string &name) {
|
|||||||
return db_get_ingredient_id(name);
|
return db_get_ingredient_id(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> db_get_recipe_ingredients(const int id) {
|
||||||
|
std::vector<std::string> ingredients;
|
||||||
|
|
||||||
|
sqlite3_exec(db, std::format("SELECT name FROM ingredients WHERE id IN (SELECT ingredient_id FROM recipe_ingredient WHERE recipe_id={});", id).c_str(),
|
||||||
|
[](void *ingredients, int, char **col_data, char**) {
|
||||||
|
static_cast<std::vector<std::string>*>(ingredients)->push_back(col_data[0]);
|
||||||
|
return 0;
|
||||||
|
}, &ingredients, nullptr);
|
||||||
|
|
||||||
|
return ingredients;
|
||||||
|
}
|
||||||
|
|
||||||
int db_get_ingredient_id(const std::string &name) {
|
int db_get_ingredient_id(const std::string &name) {
|
||||||
if(not db)
|
if(not db)
|
||||||
return -1;
|
return -1;
|
||||||
@ -272,6 +296,18 @@ int db_add_tag(const std::string &name) {
|
|||||||
return db_get_tag_id(name);
|
return db_get_tag_id(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> db_get_recipe_tags(const int id) {
|
||||||
|
std::vector<std::string> tags;
|
||||||
|
|
||||||
|
sqlite3_exec(db, std::format("SELECT name FROM tags WHERE id IN (SELECT tag_id FROM recipe_tag WHERE recipe_id={});", id).c_str(),
|
||||||
|
[](void *tags, int, char **col_data, char**) {
|
||||||
|
static_cast<std::vector<std::string>*>(tags)->push_back(col_data[0]);
|
||||||
|
return 0;
|
||||||
|
}, &tags, nullptr);
|
||||||
|
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
int db_get_tag_id(const std::string &name) {
|
int db_get_tag_id(const std::string &name) {
|
||||||
if(not db)
|
if(not db)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -45,6 +45,7 @@ bool db_recipe_exists(const int id);
|
|||||||
static inline bool db_recipe_exists(const std::string &name) {
|
static inline bool db_recipe_exists(const std::string &name) {
|
||||||
return (db_get_recipe_id(name) > 0);
|
return (db_get_recipe_id(name) > 0);
|
||||||
}
|
}
|
||||||
|
struct recipe db_get_recipe(const int id);
|
||||||
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
||||||
const std::vector<std::string> &tags);
|
const std::vector<std::string> &tags);
|
||||||
|
|
||||||
@ -56,6 +57,7 @@ std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingred
|
|||||||
* @return ID of newly created ingredient, -1 if DB isn't open, -2 on other failure.
|
* @return ID of newly created ingredient, -1 if DB isn't open, -2 on other failure.
|
||||||
*/
|
*/
|
||||||
int db_add_ingredient(const std::string &name);
|
int db_add_ingredient(const std::string &name);
|
||||||
|
std::vector<std::string> db_get_recipe_ingredients(const int id);
|
||||||
int db_get_ingredient_id(const std::string &name);
|
int db_get_ingredient_id(const std::string &name);
|
||||||
static inline bool db_ingredient_exists(const std::string &name) {
|
static inline bool db_ingredient_exists(const std::string &name) {
|
||||||
return (db_get_ingredient_id(name) > 0);
|
return (db_get_ingredient_id(name) > 0);
|
||||||
@ -69,6 +71,7 @@ static inline bool db_ingredient_exists(const std::string &name) {
|
|||||||
* @return ID of newly created tag, -1 if DB isn't open, -2 on other failure.
|
* @return ID of newly created tag, -1 if DB isn't open, -2 on other failure.
|
||||||
*/
|
*/
|
||||||
int db_add_tag(const std::string &name);
|
int db_add_tag(const std::string &name);
|
||||||
|
std::vector<std::string> db_get_recipe_tags(const int id);
|
||||||
int db_get_tag_id(const std::string &name);
|
int db_get_tag_id(const std::string &name);
|
||||||
static inline bool db_tag_exists(const std::string &name) {
|
static inline bool db_tag_exists(const std::string &name) {
|
||||||
return (db_get_tag_id(name) > 0);
|
return (db_get_tag_id(name) > 0);
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "arg_parse.hpp"
|
#include "arg_parse.hpp"
|
||||||
#include "cmd.hpp"
|
#include "cmd.hpp"
|
||||||
@ -44,6 +45,9 @@ int main(int argc, char *argv[]) {
|
|||||||
case CMD_DEL:
|
case CMD_DEL:
|
||||||
ret = command_delete(argc - 2, argv + 2);
|
ret = command_delete(argc - 2, argv + 2);
|
||||||
break;
|
break;
|
||||||
|
case CMD_INFO:
|
||||||
|
ret = command_info(std::stoi(argv[2]));
|
||||||
|
break;
|
||||||
case CMD_HELP:
|
case CMD_HELP:
|
||||||
print_help();
|
print_help();
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user