コード例 #1
0
ファイル: bookdb.cs プロジェクト: dormrod/book_logger
        public List <Book> ExecuteSQLiteReader(SQLiteCommand command, Logfile logfile)
        {
            //Execture SQL read command using command object

            logfile.WriteLine("SQL reader: ", command.CommandText);
            SQLiteDataReader reader = command.ExecuteReader();

            List <Book> bookRecords = new List <Book>();

            while (reader.Read())
            {
                Book book = new Book();
                book.dbId         = reader.GetInt32(0);
                book.title        = reader.GetString(1);
                book.author       = reader.GetString(2);
                book.language     = reader.GetString(3);
                book.date         = reader.GetString(4);
                book.rating       = reader.GetInt32(5);
                book.missing_info = reader.GetBoolean(6);
                bookRecords.Add(book);
            }

            return(bookRecords);
        }
コード例 #2
0
ファイル: bookdb.cs プロジェクト: dormrod/book_logger
        public List <Book> SearchCL(Logfile logfile)
        {
            //Search books table using command line query

            //Get query from user
            Console.WriteLine("\nChoose keyword to search by");
            Menu   queryMenu = new Menu(new string[] { "Title", "Author", "Language", "Rating", "Missing Info" }, new string[] { "title", "author", "language", "rating", "missing_info" });
            string column    = queryMenu.GetUserOptionCL();

            Console.Write("Enter {0}: ", column);
            string searchTerm = Console.ReadLine();
            string query      = "";

            switch (column)
            {
            case "title":
            case "author":
            case "language":
            {
                query = string.Format("SELECT * FROM books WHERE {0}='{1}';", column, searchTerm);
                break;
            }

            case "rating":
            case "missing_info":
            {
                query = string.Format("SELECT * FROM books WHERE {0}={1};", column, searchTerm);
                break;
            }
            }

            //Search book DB
            List <Book> bookRecords = ExecuteSQLiteReader(query, logfile);

            return(bookRecords);
        }
コード例 #3
0
        public (List <bool> synced, List <Book> syncBooks) Sync(List <Book> syncBooks, Logfile logfile)
        {
            //Add books to goodreads which are not already present

            //Get all goodreads books to compare against
            List <Book> goodReadsBooks = GetAllBooks(logfile);
            var         currentIds     = new List <string>();

            foreach (Book book in goodReadsBooks)
            {
                currentIds.Add(book.goodreads_id);
            }

            //Add books sequentially
            var synced = new List <bool>();

            foreach (Book book in syncBooks)
            {
                //Search for book in goodreads to get id
                string bookId = Search(book, logfile);

                //Add book if not already synced from goodreads
                if (!currentIds.Contains(bookId))
                {
                    var request = new RestRequest("review.xml", Method.POST);
                    request.AddParameter("book_id", bookId);
                    if (book.rating != 0)
                    {
                        request.AddParameter("review[rating]", book.rating);
                    }
                    if (book.date != "")
                    {
                        request.AddParameter("review[read_at]", book.date);
                    }
                    request.AddParameter("shelf", "read");
                    var response = ExecutPostRequest(request, logfile);
                    if (response.IsSuccessful)
                    {
                        Console.WriteLine("{0} synced with Goodreads", book.title);
                    }
                    book.goodreads_id = bookId; //update goodread ids
                    synced.Add(true);
                }
                else
                {
                    synced.Add(false);
                }
            }
            return(synced, syncBooks);
        }
コード例 #4
0
        //App to store user information about books they have read

        static void Main(string[] args)
        {
            //Initialise logfile
            Logfile logfile = new Logfile();

            logfile.WriteLine("Book Logger begun");

            //Display welcome message
            Console.WriteLine("Welcome to your Book Logger");

            //Initialise menus
            Menu mainMenu = new Menu(new string[] { "Add Book", "Delete Book", "Search Books", "Show All", "Sync goodreads (down)", "Sync goodreads (up)", "Hard Reset", "Quit" }, new string[] { "add", "delete", "search", "show", "sync down", "sync up", "reset", "quit" });

            logfile.WriteLine("Menus initialised");

            //Initialise DB
            BookDB bookDB = new BookDB(logfile);

            //Intialise Good Reads interface
            GoodReadsInterface goodReads = new GoodReadsInterface(logfile);

            //Run until user quits
            bool   quit = false;
            string option;

            do
            {
                //Main menu
                option = mainMenu.GetUserOptionCL();
                switch (option)
                {
                case "add":
                {
                    Book book = new Book();
                    book.UserInputCL();
                    bookDB.AddBook(book, "IGNORE", logfile);
                    Console.WriteLine("\nYou added the book: \n{0}", book);
                    break;
                }

                case "search":
                {
                    List <Book> searchResults = bookDB.SearchCL(logfile);
                    Console.WriteLine("{0} records found", searchResults.Count);
                    Console.WriteLine("Press enter to display");
                    for (int i = 0; i < searchResults.Count; ++i)
                    {
                        Console.ReadLine();
                        Console.WriteLine(searchResults[i]);
                    }
                    break;
                }

                case "delete":
                {
                    List <Book> searchResults = bookDB.SearchCL(logfile);
                    Console.WriteLine("{0} records found", searchResults.Count);
                    Console.WriteLine("Choose book to delete");
                    Book bookToDelete = new Book();
                    bool bookSelected = false;
                    foreach (Book book in searchResults)
                    {
                        Console.WriteLine(book);
                        Console.WriteLine("Would you like to delete this book?");
                        string response = Console.ReadLine();
                        if (response == "y" || response == "Y" || response == "yes" || response == "Yes")
                        {
                            bookToDelete = book;
                            bookSelected = true;
                            break;
                        }
                    }
                    if (bookSelected)
                    {
                        bookDB.DeleteBook(bookToDelete, logfile);
                        Console.WriteLine("Book deleted.");
                    }
                    else
                    {
                        Console.WriteLine("No book selected for deletion.");
                    }
                    break;
                }

                case "show":
                {
                    List <Book> searchResults = bookDB.GetAllBooks(logfile);
                    Console.WriteLine("{0} records found", searchResults.Count);
                    Console.WriteLine("Press enter to display");
                    for (int i = 0; i < searchResults.Count; ++i)
                    {
                        Console.ReadLine();
                        Console.WriteLine(searchResults[i]);
                    }
                    break;
                }

                case "sync down":
                {
                    List <Book> goodReadsBooks = goodReads.GetAllBooks(logfile);
                    foreach (Book book in goodReadsBooks)
                    {
                        bookDB.AddBook(book, "IGNORE", logfile);
                    }
                    break;
                }

                case "sync up":
                {
                    List <Book> localBooks   = bookDB.GetAllBooks(logfile);
                    var         res          = goodReads.Sync(localBooks, logfile);
                    var         synced       = res.synced;
                    var         updatedBooks = res.syncBooks;
                    for (int i = 0; i < synced.Count; ++i)
                    {
                        if (synced[i])
                        {
                            bookDB.AddBook(updatedBooks[i], "REPLACE", logfile);
                        }
                    }
                    break;
                }

                case "reset":
                {
                    bookDB.ResetTable(logfile);
                    break;
                }

                case "quit":
                {
                    Console.WriteLine("See you next time!");
                    quit = true;
                    break;
                }
                }
            } while (!quit);
        }