ornwipa / book_recommender

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

rate a book (or change rating) #5

Closed ornwipa closed 4 years ago

ornwipa commented 4 years ago

Users can change their rating of a book, save and go Back to Index in case of Book you rated. Users can mark and read and rate a book, ... same as above ... in case of Books recommended for you.

ornwipa commented 4 years ago

To create partial view on SetUser page (list of rated books)... there will no longer be an Edit page.

ornwipa commented 4 years ago

Still need to link the new ratings to database somehow. This function is not yet completed.

ornwipa commented 4 years ago

What happens when a rating by new user is not saved = DivideByZeroException: Attempted to divide by zero recommender.Models.User.getRecommendedBook() in User.cs

                sum_rating_cutoff = sum_book_rating.Sum()/no_similar_users/2;

recommender.Models.User.setRecommendedBook() in User.cs

            this.recommended_books = this.getRecommendedBook();

recommender.Controllers.HomeController.RecommendBook(string user_id) in HomeController.cs

                current_user.setRecommendedBook();

i.e. can't get recommended book when no_similar_users = 0, need to resolve this issue by enable saving of rating changes.

ornwipa commented 4 years ago

Useful resource from https://www.tektutorialshub.com/entity-framework/ef-update-record/ Current code:

            var optionBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            using (ApplicationDbContext db = new ApplicationDbContext(optionBuilder.Options))
            {
                var row = db.Ratings.Where(r => r.book_id == book_id && r.user_id == Convert.ToInt32(user_id)).FirstOrDefault();
                if (row == null) throw new Exception("Rating record is not found. Something wrong with indexing");
                row.rating_ = rating_;
                db.SaveChanges();
            }

Not working because DbContext wasn't actually connected properly.

ornwipa commented 4 years ago

Initial error with unsuccessful connection to database provider.

InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)

Then trying to solve by overriding OnConfiguring method and add optionsBuilder.UseSqlServer(@"DataSource=app.db"); but got error:

ArgumentException: Keyword not supported: 'datasource'.

Microsoft.Data.Common.DbConnectionOptions.ParseInternal(Dictionary<string, string> parsetable, string connectionString, bool buildChain, Dictionary<string, string> synonyms, bool firstKey)
ornwipa commented 4 years ago

Trying with optionsBuilder.UseSqlite("DataSource=app.db"); then

SqliteException: SQLite Error 1: 'no such table: Ratings'.

Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(int rc, sqlite3 db)

So there is app.db, but no Ratings table yet ... next: redo migrations and update

ornwipa commented 4 years ago

The previous issue was solved by creating sqlite (.db file) from a python script.

The current problem is that the DbContext cannot be saved inside controller method, referring to:

Are controllers in ASP.NET created for every HTTP request, or are they created at application startup and reused throughout requests? from https://stackoverflow.com/questions/5425920/asp-net-mvc-is-controller-created-for-every-request