export def "books add" [ name: string, # name of the book author: string, # name of the book's author --finished # mark the book already read - default: false ] { let book_id = random uuid { id: $book_id, name: $name, author: $author finished: ($finished | default false) } | into sqlite $env.READINGORDER_DB_PATH -t books print $"Added ($name) successfully to list of books." books show --id $book_id } export def "books show" [ --id: string, # id of the book, identify using `reading-order books show --explore` --explore # show in nushell explorer ] { if not ($env.READINGORDER_DB_PATH | path exists) { print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable." return } mut books = open $env.READINGORDER_DB_PATH | get books if $id != null { $books = $books | where id == $id } if ($explore) { return ($books | explore -p) } return $books } export def "books set-name" [ id: string, # id of the book, identify using `reading-order books show --explore` name: string ] { if not ($env.READINGORDER_DB_PATH | path exists) { print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable." return } open $env.READINGORDER_DB_PATH | query db "UPDATE books SET name = ? WHERE id = ?" -p [$name, $id] print $"Updated name for book with id ($id) successfully." books show --id $id } export def "books set-author-name" [ id: string, # id of the book, identify using `reading-order books show --explore` author: string ] { if not ($env.READINGORDER_DB_PATH | path exists) { print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable." return } open $env.READINGORDER_DB_PATH | query db "UPDATE books SET author = ? WHERE id = ?" -p [$author, $id] print $"Updated author name for book with id ($id) successfully." books show --id $id } export def "books set-finished" [ id: string, # id of the book, identify using `reading-order books show --explore` finished: bool = true, ] { if not ($env.READINGORDER_DB_PATH | path exists) { print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable." return } open $env.READINGORDER_DB_PATH | query db "UPDATE books SET finished = ? WHERE id = ?" -p [$finished, $id] print $"Updated finished status for book with id ($id) successfully." books show --id $id } # Add a reading order export def add [ book1_id: string, # id of the book to be read before book 2, identify using `reading-order books show --explore` book2_id: string, # id of the book to be read after book 1, identify using `reading-order books show --explore` ] { let reading_order_id = random uuid { id: $reading_order_id, book1: $book1_id, book2: $book2_id, } | into sqlite $env.READINGORDER_DB_PATH print $"Added reading order successfully." show --id $reading_order_id } export def show [ --id: string, # id of the book, identify using `reading-order books show --explore` --explore # show in nushell explorer ] { if not ($env.READINGORDER_DB_PATH | path exists) { print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable." return } mut reading_orders = open $env.READINGORDER_DB_PATH | get main if $id != null { return ($reading_orders | where id == $id) } if ($explore) { return ($reading_orders | explore -p) } return $reading_orders } export def read-next [] { if not ($env.READINGORDER_DB_PATH | path exists) { print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable." return } let reading_orders = open $env.READINGORDER_DB_PATH | get main mut books = open $env.READINGORDER_DB_PATH | get books | where { |book| if $book.finished == 1 { return false } for order in $reading_orders { if $order.book2 != $book.id { continue } let book_to_read_before = books show --id $order.book1 if $book_to_read_before.finished != 1 { return false } } return true } return $books }