示例#1
0
        /// <summary>
        /// Add series to my list
        /// </summary>
        /// <param name="id">Series Id</param>
        public void AddSeriesToMySeries(int id)
        {
            var user = this.Utils.GetCurrentUser();

            var mapping =
                this._databaseContext.UserSeriesSwitch.FirstOrDefault(x => x.UserId == user.Id && x.SeriesId == id);

            if (mapping == null)
            {
                mapping = new UserSeries {
                    SeriesId = id, UserId = user.Id, AddedOn = DateTime.Now, IsAdded = true
                };
                this._databaseContext.UserSeriesSwitch.Add(mapping);
                this._databaseContext.SaveChanges();
                this.Logger.LogInformation(user, this.GetService(), this.GetEvent("add my"), id);
                this.Notification.AddStatusLibraryNotificationByType(StatusLibraryNotificationType.MySeriesListUpdated,
                                                                     user);
            }
            else
            {
                mapping.AddedOn = DateTime.Now;
                mapping.IsAdded = true;
                this._databaseContext.UserSeriesSwitch.Update(mapping);
                this.Logger.LogInformation(user, this.GetService(), this.GetEvent("add my"), id);
                this.Notification.AddStatusLibraryNotificationByType(StatusLibraryNotificationType.MySeriesListUpdated,
                                                                     user);
            }
        }
        public async Task <int?> RateSeries(string userId, RateApiRequestModel model)
        {
            var series = this.dbContext.Series.Find(model.Id);

            if (series != null)
            {
                var userSeries = series.UsersSeries.FirstOrDefault(us => us.UserId == userId);

                if (userSeries == null)
                {
                    userSeries = new UserSeries
                    {
                        UserId   = userId,
                        SeriesId = model.Id,
                        Score    = model.Score,
                    };

                    series.UsersSeries.Add(userSeries);

                    this.dbContext.Update(series);
                    await this.dbContext.SaveChangesAsync();
                }
                else
                {
                    userSeries.Score = model.Score;

                    this.dbContext.Update(series);
                    await this.dbContext.SaveChangesAsync();
                }

                return(model.Score);
            }

            return(null);
        }
        public void Add(int id, string userId)
        {
            if (string.IsNullOrWhiteSpace(userId))
            {
                return;
            }
            var series = _seriesRepository.GetById(id);

            if (series == null)
            {
                return;
            }
            if (IsFollowing(id, userId))
            {
                return;
            }

            var userSeries = new UserSeries
            {
                Date     = DateTime.Now,
                SeriesId = id,
                UserId   = userId
            };

            _repository.Add(userSeries);
            SaveChanges();
        }
示例#4
0
        public async Task AddSeriesToList(string userId, int id)
        {
            var series = this.dbContext.Series.Find(id);

            if (series == null)
            {
                throw new KeyNotFoundException("Series with given id does not exist.");
            }

            var user = this.dbContext.Users
                       .Include(u => u.UsersSeries)
                       .FirstOrDefault(u => u.Id == userId);

            if (user.UsersSeries.Any(uv => uv.Series == series))
            {
                throw new InvalidOperationException("User has already added given series to their list.");
            }

            var userSeries = new UserSeries {
                User = user, Series = series
            };

            user.UsersSeries.Add(userSeries);
            await this.dbContext.SaveChangesAsync();
        }
示例#5
0
        /// <summary>
        /// Update my list
        /// </summary>
        /// <param name="ids">Current my series list</param>
        public void UpdateMySeries(List <int> ids)
        {
            var user = this.Utils.GetCurrentUser();

            var currentMappings = this._databaseContext.UserSeriesSwitch.Where(x => x.UserId == user.Id).ToList();

            foreach (var i in currentMappings)
            {
                if (ids.FindIndex(x => x == i.SeriesId) == -1)
                {
                    i.IsAdded = false;
                    i.AddedOn = null;
                    this._databaseContext.UserSeriesSwitch.Update(i);
                }
                else
                {
                    i.IsAdded = true;
                    i.AddedOn = DateTime.Now;
                    this._databaseContext.UserSeriesSwitch.Update(i);
                }
            }

            foreach (int i in ids)
            {
                if (currentMappings.FirstOrDefault(x => x.SeriesId == i) == null)
                {
                    var map = new UserSeries {
                        SeriesId = i, UserId = user.Id, AddedOn = DateTime.Now, IsAdded = true
                    };
                    this._databaseContext.UserSeriesSwitch.Add(map);
                }
            }

            this._databaseContext.SaveChanges();
            this.Logger.LogInformation(user, this.GetService(), this.GetEvent("update my"), ids);
            this.Notification.AddStatusLibraryNotificationByType(StatusLibraryNotificationType.MySeriesListUpdated,
                                                                 user);
        }
示例#6
0
        /// <summary>
        /// Updates series's rate status for current user.
        /// </summary>
        /// <param name="id">Series Id</param>
        /// <param name="model">Rate model</param>
        public void UpdateRate(int id, SeriesRateModel model)
        {
            var user = this.Utils.GetCurrentUser();

            var map = user.MySeries.FirstOrDefault(x => x.Series.Id == id);

            if (map != null)
            {
                map.Rate = model.Rate;
                this._databaseContext.UserSeriesSwitch.Update(map);
                this._databaseContext.SaveChanges();
                this.Logger.LogInformation(user, this.GetService(), this.GetEvent("rate"), map.Series.Id);
            }
            else
            {
                map = new UserSeries
                {
                    UserId = user.Id, SeriesId = id, IsAdded = false, Rate = model.Rate
                };
                this._databaseContext.UserSeriesSwitch.Add(map);
                this._databaseContext.SaveChanges();
                this.Logger.LogInformation(user, this.GetService(), this.GetEvent("rate"), id);
            }
        }
示例#7
0
        // GET: Series/Details/5
        public async Task <IActionResult> Details(UserSeriesDetailViewModel viewModel)
        {
            Series series = null;

            if (ModelState.IsValid)
            {
                string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

                var userSeries =
                    await _context.UsersSeries.FirstOrDefaultAsync(x =>
                                                                   x.UserId == userId && x.SeriesId == viewModel.SeriesId);

                var favouriteSeries =
                    await _context.FavoritesSeries.FirstOrDefaultAsync(x => x.UserId == userId && x.SeriesId == viewModel.SeriesId);

                if (favouriteSeries == null && viewModel.IsFavourite)
                {
                    var newFavoritesSeries = new FavoritesSeries
                    {
                        SeriesId  = viewModel.SeriesId,
                        UserId    = userId,
                        CreatedAt = DateTime.UtcNow,
                        CreatedBy = userId
                    };

                    _context.FavoritesSeries.Add(newFavoritesSeries);
                    await _context.SaveChangesAsync();
                }
                else if (favouriteSeries != null && !viewModel.IsFavourite)
                {
                    _context.FavoritesSeries.Remove(favouriteSeries);
                    await _context.SaveChangesAsync();
                }

                if (userSeries == null)
                {
                    userSeries = new UserSeries
                    {
                        SeriesId        = viewModel.SeriesId,
                        UserId          = userId,
                        Rating          = (int)viewModel.SeriesRating,
                        WatchStatus     = viewModel.WatchStatus,
                        CreatedBy       = userId,
                        CreatedAt       = DateTime.UtcNow,
                        EpisodesWatched = viewModel.EpisodesWatched,
                        SeasonsWatched  = viewModel.SeasonsWatched,
                    };

                    _context.Add(userSeries);


                    if (viewModel.SeriesRating != SeriesRating.NotRated)
                    {
                        series = await _context.Series.FirstOrDefaultAsync(x => x.Id == viewModel.SeriesId);

                        series.UserVotes++;
                        var seriesRating = series.SeriesUsers.Sum(x => x.Rating);
                        series.UserRating = (decimal)seriesRating / series.UserVotes;
                        _context.Update(series);
                    }
                }
                else
                {
                    userSeries.WatchStatus = viewModel.WatchStatus;
                    userSeries.Rating      = (int)viewModel.SeriesRating;

                    series = await _context.Series
                             .Include(x => x.SeriesGenres)
                             .ThenInclude(x => x.Genre)
                             .FirstOrDefaultAsync(x => x.Id == viewModel.SeriesId);

                    if (userSeries.Rating != 0 && series.UserVotes != 0)
                    {
                        var seriesRating = series.SeriesUsers.Sum(x => x.Rating);
                        series.UserRating = (decimal)seriesRating / series.UserVotes;
                    }

                    _context.Update(series);
                    _context.Update(userSeries);
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", new { id = viewModel.SeriesId }));
            }
            else
            {
                series = await _context.Series
                         .Include(x => x.SeriesGenres)
                         .ThenInclude(x => x.Genre)
                         .FirstOrDefaultAsync(x => x.Id == viewModel.SeriesId);

                viewModel.SiteRating    = series.UserRating;
                viewModel.PosterContent = series.PosterContent;

                viewModel.Genres = new List <Genre>();

                foreach (var seriesGenre in series.SeriesGenres)
                {
                    viewModel.Genres.Add(seriesGenre.Genre);
                }

                return(View(viewModel));
            }
        }