Skip to content

Commit

Permalink
feature: add edit subcommand (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
DSiekmeier committed Mar 20, 2024
1 parent defbfb6 commit 398a9c9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Here is a short introduction how to use **booktrack-cli**.

# Delete a book from the given library by the book id
./booktrack-cli -l ~/my_lib.json delete --by-id 2

# Edit the title of the book from the given library with the book id 4
./booktrack-cli -l ~/my_lib.json edit --by-id 4 --title "Introduction to Algorithms"
```

You can see a full list of commandline options below.
Expand All @@ -59,6 +62,7 @@ Subcommands:
list List books in the library
statistics Show statistics for loaded library
details Show details for a book
edit Edit a book in the library
```

## Notes
Expand Down
17 changes: 16 additions & 1 deletion src/book.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ class Book {
*/
inline auto GetTitle() const { return title_; };

/**
* @brief Set the title of the current book
*/
inline auto SetTitle(const std::string& title) { title_ = title; }

/**
* @brief Get the author of the current book
*/
inline auto GetAuthor() const { return author_; }

/**
* @brief Set the author of the current book
*/
inline auto SetAuthor(const std::string& author) { author_ = author; }

/**
* @brief Get the reading end time of the current book
*/
Expand All @@ -60,6 +70,11 @@ class Book {
*/
inline auto GetPages() const { return pages_; }

/**
* @brief Set the number of pages of the current book
*/
inline auto SetPages(unsigned int pages) { pages_ = pages; }

/**
* @brief Get the shelf to which the current book belongs to
*/
Expand All @@ -86,7 +101,7 @@ class Book {

/**
* @brief Sets all information of a Book instance from a JSON object
*
*
* @throws std::exception subclass on error
*/
void SetDataFromJson(const nlohmann::json& book_json);
Expand Down
12 changes: 12 additions & 0 deletions src/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ void AddDetailsOptions(CLI::App& app, CliOptions& opt) {
sub_details->add_option("--by-id", opt.details.id, "")->mandatory();
}

void AddEditOptions(CLI::App& app, CliOptions& opt) {
auto sub_edit =
app.add_subcommand("edit", "Edit a book in the library")
->callback([&opt]() { opt.command = PrimaryCommand::kEdit; });

sub_edit->add_option("--by-id", opt.edit.id, "")->mandatory();
sub_edit->add_option("-t,--title", opt.edit.changeset.title, "");
sub_edit->add_option("-a,--author", opt.edit.changeset.author, "");
sub_edit->add_option("-p,--pages", opt.edit.changeset.pages, "");
}

} // namespace

void booktrack_cli::AddCliOptions(CLI::App& app, CliOptions& opt) {
Expand All @@ -100,4 +111,5 @@ void booktrack_cli::AddCliOptions(CLI::App& app, CliOptions& opt) {
AddListOptions(app, opt);
AddStatisticsOptions(app, opt);
AddDetailsOptions(app, opt);
AddEditOptions(app, opt);
}
10 changes: 10 additions & 0 deletions src/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum class PrimaryCommand {
kDelete,
kStatistics,
kDetails,
kEdit,
};

/**
Expand Down Expand Up @@ -59,6 +60,14 @@ struct CliOptionsDetails {
size_t id{0};
};

/**
* @brief Contains available options for the "edit" subcommand
*/
struct CliOptionsEdit {
size_t id{0};
CliOptionsAdd changeset;
};

/**
* @brief Structure containing all options for the command line interface
*/
Expand All @@ -70,6 +79,7 @@ struct CliOptions {
CliOptionsList list;
CliOptionsStatistics statistics;
CliOptionsDetails details;
CliOptionsEdit edit;
};

/**
Expand Down
29 changes: 29 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ void SubcmdDetails(Library& lib, const CliOptionsDetails& options) {
}
}

void SubcmdEdit(Library& lib, [[maybe_unused]] const CliOptionsEdit& options) {
auto check_exist = lib.GetBookById(options.id);

if (check_exist.has_value()) {
auto book = check_exist.value();
auto changes = options.changeset;

// update book
if (!changes.title.empty()) {
book.SetTitle(changes.title);
}

if (!changes.author.empty()) {
book.SetAuthor(changes.author);
}

book.SetPages(changes.pages);

// Update library
lib.RemoveBookById(book.GetId());
lib.AddBook(book).StoreToFile();
} else {
std::cout << "Could not find a book with id: " << options.id << ".\n";
}
}

} // namespace

int main(int argc, char* argv[]) {
Expand Down Expand Up @@ -120,6 +146,9 @@ int main(int argc, char* argv[]) {
case PrimaryCommand::kDetails:
SubcmdDetails(library_from_file, options.details);
break;
case PrimaryCommand::kEdit:
SubcmdEdit(library_from_file, options.edit);
break;
default:
break;
}
Expand Down

0 comments on commit 398a9c9

Please sign in to comment.