Skip to content

Commit f04c0f7

Browse files
committed
(wip) feature: add edit subcommand (#20)
1 parent defbfb6 commit f04c0f7

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Here is a short introduction how to use **booktrack-cli**.
4040

4141
# Delete a book from the given library by the book id
4242
./booktrack-cli -l ~/my_lib.json delete --by-id 2
43+
44+
# Edit the title of the book from the given library with the book id 4
45+
./booktrack-cli -l ~/my_lib.json edit --by-id 4 --title "Introduction to Algorithms"
4346
```
4447

4548
You can see a full list of commandline options below.
@@ -59,6 +62,7 @@ Subcommands:
5962
list List books in the library
6063
statistics Show statistics for loaded library
6164
details Show details for a book
65+
edit Edit a book in the library
6266
```
6367

6468
## Notes

src/book.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ class Book {
4040
*/
4141
inline auto GetTitle() const { return title_; };
4242

43+
/**
44+
* @brief Set the title of the current book
45+
*/
46+
inline auto SetTitle(const std::string& title) { title_ = title; }
47+
4348
/**
4449
* @brief Get the author of the current book
4550
*/
4651
inline auto GetAuthor() const { return author_; }
4752

53+
/**
54+
* @brief Set the author of the current book
55+
*/
56+
inline auto SetAuthor(const std::string& author) { author_ = author; }
57+
4858
/**
4959
* @brief Get the reading end time of the current book
5060
*/
@@ -60,6 +70,11 @@ class Book {
6070
*/
6171
inline auto GetPages() const { return pages_; }
6272

73+
/**
74+
* @brief Set the number of pages of the current book
75+
*/
76+
inline auto SetPages(unsigned int pages) { pages_ = pages; }
77+
6378
/**
6479
* @brief Get the shelf to which the current book belongs to
6580
*/
@@ -86,7 +101,7 @@ class Book {
86101

87102
/**
88103
* @brief Sets all information of a Book instance from a JSON object
89-
*
104+
*
90105
* @throws std::exception subclass on error
91106
*/
92107
void SetDataFromJson(const nlohmann::json& book_json);

src/cli.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ void AddDetailsOptions(CLI::App& app, CliOptions& opt) {
9191
sub_details->add_option("--by-id", opt.details.id, "")->mandatory();
9292
}
9393

94+
void AddEditOptions(CLI::App& app, CliOptions& opt) {
95+
auto sub_edit =
96+
app.add_subcommand("edit", "Edit a book in the library")
97+
->callback([&opt]() { opt.command = PrimaryCommand::kEdit; });
98+
99+
sub_edit->add_option("--by-id", opt.edit.id, "")->mandatory();
100+
sub_edit->add_option("-t,--title", opt.edit.changeset.title, "");
101+
sub_edit->add_option("-a,--author", opt.edit.changeset.author, "");
102+
sub_edit->add_option("-p,--pages", opt.edit.changeset.pages, "");
103+
}
104+
94105
} // namespace
95106

96107
void booktrack_cli::AddCliOptions(CLI::App& app, CliOptions& opt) {
@@ -100,4 +111,5 @@ void booktrack_cli::AddCliOptions(CLI::App& app, CliOptions& opt) {
100111
AddListOptions(app, opt);
101112
AddStatisticsOptions(app, opt);
102113
AddDetailsOptions(app, opt);
114+
AddEditOptions(app, opt);
103115
}

src/cli.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum class PrimaryCommand {
1616
kDelete,
1717
kStatistics,
1818
kDetails,
19+
kEdit,
1920
};
2021

2122
/**
@@ -59,6 +60,14 @@ struct CliOptionsDetails {
5960
size_t id{0};
6061
};
6162

63+
/**
64+
* @brief Contains available options for the "edit" subcommand
65+
*/
66+
struct CliOptionsEdit {
67+
size_t id{0};
68+
CliOptionsAdd changeset;
69+
};
70+
6271
/**
6372
* @brief Structure containing all options for the command line interface
6473
*/
@@ -70,6 +79,7 @@ struct CliOptions {
7079
CliOptionsList list;
7180
CliOptionsStatistics statistics;
7281
CliOptionsDetails details;
82+
CliOptionsEdit edit;
7383
};
7484

7585
/**

src/main.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@ void SubcmdDetails(Library& lib, const CliOptionsDetails& options) {
9393
}
9494
}
9595

96+
void SubcmdEdit(Library& lib, [[maybe_unused]] const CliOptionsEdit& options) {
97+
auto check_exist = lib.GetBookById(options.id);
98+
99+
if (check_exist.has_value()) {
100+
auto book = check_exist.value();
101+
auto changes = options.changeset;
102+
103+
// update book
104+
if (!changes.title.empty()) {
105+
book.SetTitle(changes.title);
106+
}
107+
108+
if (!changes.author.empty()) {
109+
book.SetAuthor(changes.author);
110+
}
111+
112+
book.SetPages(changes.pages);
113+
114+
// Update library
115+
lib.RemoveBookById(book.GetId());
116+
lib.AddBook(book).StoreToFile();
117+
} else {
118+
std::cout << "Could not find a book with id: " << options.id << ".\n";
119+
}
120+
}
121+
96122
} // namespace
97123

98124
int main(int argc, char* argv[]) {
@@ -120,6 +146,9 @@ int main(int argc, char* argv[]) {
120146
case PrimaryCommand::kDetails:
121147
SubcmdDetails(library_from_file, options.details);
122148
break;
149+
case PrimaryCommand::kEdit:
150+
SubcmdEdit(library_from_file, options.edit);
151+
break;
123152
default:
124153
break;
125154
}

0 commit comments

Comments
 (0)