ornwipa / book_recommender

Final project for ComIT's FULL STACK .NET course
GNU General Public License v3.0
1 stars 1 forks source link

search for a book #7

Closed ornwipa closed 4 years ago

ornwipa commented 4 years ago

User should have an ability to add a book into their read list so that they can rate.

ornwipa commented 4 years ago

HTML form in SetUser page... algorithm needs to look for all books in the data set (jagged array) to find a match Search will be available for authors, title/original_title, ISBN, year - that's all.

ornwipa commented 4 years ago

Referring to https://stackoverflow.com/questions/8167853/performing-search-in-asp-net-mvc, successfully implemented book search on MVC

ornwipa commented 4 years ago

Search query example at https://stackoverflow.com/questions/20546948/how-to-perform-a-search-in-entity-framework-6 Solving issue w.r.t. client evaluation, see https://docs.microsoft.com/en-ca/ef/core/querying/client-eval

ornwipa commented 4 years ago

Current error on null value in some of the fields/attributes in Book entity NullReferenceException: Object reference not set to an instance of an object.

recommender.Models.Book+<>c__DisplayClass45_0.<searchBook>b__0(Book data) in Book.cs

        public static List<Book> searchBook(string text_input, IBookService bookservice)
        {
            List<Book> matched = new List<Book>();            
            var optionBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            var context = new ApplicationDbContext(optionBuilder.Options);
            matched = context.Books.AsEnumerable().

                                    Where(data => data.title.Contains(text_input) ||

                                    data.original_title.Contains(text_input) ||
                                    data.authors.Contains(text_input)).ToList();
            /* var book_data = context.Books.ToArray();
            // var book_data = bookservice.getBookData();
            // var book_data = TinyCsvParserBook.ReadBookCsv();
            for (int b = 0; b < book_data.Length; b++)

System.Linq.Enumerable+WhereEnumerableIterator.ToList() System.Linq.Enumerable.ToList(IEnumerable source) recommender.Models.Book.searchBook(string text_input, IBookService bookservice) in Book.cs

        /// <returns>a list of matched Book; if not found return an empty list</returns>
        public static List<Book> searchBook(string text_input, IBookService bookservice)
        {
            List<Book> matched = new List<Book>();            
            var optionBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            var context = new ApplicationDbContext(optionBuilder.Options);

            matched = context.Books.AsEnumerable().

                                    Where(data => data.title.Contains(text_input) ||
                                    data.original_title.Contains(text_input) ||
                                    data.authors.Contains(text_input)).ToList();
            /* var book_data = context.Books.ToArray();
            // var book_data = bookservice.getBookData();
            // var book_data = TinyCsvParserBook.ReadBookCsv();

recommender.Controllers.HomeController.Search(string search, string user_id) in HomeController.cs

        }
        [HttpGet]
        public IActionResult Search(string search, string user_id)
        {
            current_user = new User(this._bookService, this._ratingService, user_id);

            current_user.search_matched = Book.searchBook(search, this._bookService);

            if (current_user.search_matched == null)
            {
                return Content("Not Found");
            }
            else
            {

lambda_method(Closure , object , object[] )

ornwipa commented 4 years ago

Solved in Book.cs


        public static Book selectBook(int book_index, IBookService bookservice)
        {
            var optionBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            var context = new ApplicationDbContext(optionBuilder.Options);
            return context.Books.Where(x => x.id == (book_index+1)).FirstOrDefault();
        }

        public static List<Book> searchBook(string text_input, IBookService bookservice)
        {
            List<Book> matched = new List<Book>();    
            for (int b = 0; b < 10000; b++)
            {
                try { 
                    Book book = Book.selectBook(b, bookservice);
                    if (book.ToString().ToLower().Contains(text_input.ToLower()))
                    {
                        matched.Add(book);
                    } 
                }
                catch { 
                    continue; 
                }
            }                 
            return matched;
        }