public async Task <MovieRating> AddOrUpdateRatingAsync(int movieId, int userId, decimal rating) { var movie = _context.Movies.FirstOrDefault(x => x.MovieId == movieId); if (movie == null) { return(null); //Invalid movie } var user = _context.Users.FirstOrDefault(x => x.UserId == userId); if (user == null) { return(null); //Invalid user } var movieRating = _context.Ratings.FirstOrDefault(x => x.MovieId == movieId && x.UserId == userId); if (movieRating == null) { movieRating = new MovieRating() { MovieId = movieId, UserId = userId }; _context.Ratings.Add(movieRating); } movieRating.Rating = rating; await _context.SaveChangesAsync().ConfigureAwait(false); return(movieRating); }
public async Task <IActionResult> OnPostAsync() { // Perform an initial check to catch FileUpload class // attribute violations. if (!ModelState.IsValid) { Schedule = await _context.Schedules.AsNoTracking().ToListAsync(); return(Page()); } var publicScheduleData = await FileHelpers.ProcessFormFile(FileUpload.UploadPublicSchedule, ModelState); var privateScheduleData = await FileHelpers.ProcessFormFile(FileUpload.UploadPrivateSchedule, ModelState); // Perform a second check to catch ProcessFormFile method // violations. if (!ModelState.IsValid) { Schedule = await _context.Schedules.AsNoTracking().ToListAsync(); return(Page()); } var schedule = new Schedule() { PublicSchedule = publicScheduleData, PublicScheduleSize = FileUpload.UploadPublicSchedule.Length, PrivateSchedule = privateScheduleData, PrivateScheduleSize = FileUpload.UploadPrivateSchedule.Length, Title = FileUpload.Title, UploadDT = DateTime.UtcNow }; _context.Schedules.Add(schedule); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }