예제 #1
0
        private bool InsertOrUpdateUser(UserInformationDto userInformation)
        {
            var user = _unitOfWork.UserRepository.GetSingle(p => p.Email == userInformation.email);

            if (user != null)
            {
                user.ExternalId = userInformation.id;
                user.Token      = userInformation.accessToken;
                _unitOfWork.UserRepository.Update(user);
            }
            else
            {
                user = new User()
                {
                    Id         = -1,
                    Email      = userInformation.email,
                    ExternalId = userInformation.id,
                    Token      = userInformation.accessToken
                };
                _unitOfWork.UserRepository.Insert(user);
            }

            _unitOfWork.Save();

            SimplerNewsSQLDb db = new SimplerNewsSQLDb();

            db.ResetUserPreferences(user.Id);

            return(true);
        }
예제 #2
0
        public bool UpdateUserPreferences(UserInformationDto userInformation)
        {
            try
            {
                InsertOrUpdateUser(userInformation);
                var user = _unitOfWork.UserRepository.GetSingle(p => p.Email == userInformation.email);

                if (userInformation.facebookJSON?.likes != null)
                {
                    SimplerNewsSQLDb db = new SimplerNewsSQLDb();
                    db.ResetUserPreferences(user.Id);

                    foreach (var like in userInformation.facebookJSON.likes)
                    {
                        var facebookLikeCategory =
                            _unitOfWork.FacebookCategoryRepository.GetSingle(p => p.CategoryName == like.category);
                        if (facebookLikeCategory != null)
                        {
                            var pref =
                                _unitOfWork.UserPreferencesRepository.GetSingle(
                                    p => p.YoutubeCategoryId == facebookLikeCategory.VideoCategoryId && p.UserId == user.Id);
                            if (pref != null)
                            {
                                double additionalScore = (1.0 / (DateTime.Now.Year - DateTime.Parse(like.liked_date).Year + 1));
                                // ReSharper disable once PossibleLossOfFraction
                                decimal addition = Convert.ToDecimal(additionalScore);
                                pref.Score += addition;
                                _unitOfWork.UserPreferencesRepository.Update(pref);
                                _unitOfWork.Save();
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
예제 #3
0
 public UnitOfWork()
 {
     _context = new SimplerNewsSQLDb();
 }
예제 #4
0
        public List <VideoDto> GetPersonalizedVideos(string token, int numberOfVideos)
        {
            SimplerNewsSQLDb db        = new SimplerNewsSQLDb();
            List <VideoDto>  videoList = new List <VideoDto>();
            var user = _unitOfWork.UserRepository.GetSingle(p => p.Token == token);

            if (user == null)
            {
                return(videoList);
            }

            var userPreferences = _unitOfWork.UserPreferencesRepository.GetMany(p => p.UserId == user.Id).ToList();

            decimal sumPref = userPreferences.Sum(p => p.Score);

            while (videoList.Count < numberOfVideos)
            {
                decimal randomNumber = Convert.ToDecimal(RandomHelper.RandomNumberBetween(0, Convert.ToDouble(sumPref)));

                userPreferences = userPreferences.OrderBy(p => new Guid()).ToList();

                for (int i = 1; i < userPreferences.Count; i++)
                {
                    var sum = userPreferences.Take(i).Sum(p => p.Score);
                    if (sum >= randomNumber)
                    {
                        var categoryId = userPreferences[i - 1].YoutubeCategoryId;
                        //var videoId  = _unitOfWork.VideoRepository.GetFirst(p => p.PublishedAt > DateTime.Now.AddDays(-5)
                        //                                                    && p.VideoCategoryId == categoryId
                        //                                                    && !user.UserVideoWatched.Select(x => x.VideoId).Contains(p.Id));


                        int videoId = -1;

                        var userVideoWatched = user.UserVideoWatched.Select(p => p.VideoId).ToList();
                        var dateFrom         = DateTime.Now.AddDays(-7);
                        var v =
                            db.Video.FirstOrDefault(
                                p =>
                                p.PublishedAt > dateFrom && p.VideoCategoryId == categoryId &&
                                !userVideoWatched.Contains(p.Id));
                        if (v != null)
                        {
                            videoId = v.Id;
                        }

                        var video = _unitOfWork.VideoRepository.GetByID(videoId);

                        if (video != null)
                        {
                            VideoDto videoDto = new VideoDto()
                            {
                                YoutubeChannelId = video.YoutubeChannelId,
                                Description      = video.Description,
                                VideoCategory    =
                                    new VideoCategoryDto()
                                {
                                    Id = video.VideoCategory.Id,
                                    YoutbeVideoCategoryId = video.VideoCategory.YoutbeVideoCategoryId,
                                    VideoCategoryName     = video.VideoCategory.VideoCategoryName
                                },
                                Id               = video.Id,
                                PublishedAt      = video.PublishedAt,
                                YoutubeId        = video.YoutubeId,
                                Title            = video.Title,
                                VideoTagList     = video.VideoTag.ToList().Select(p => p.Tag).ToList(),
                                YoutubeLink      = video.YoutubeLink,
                                NumberOfDislikes = video.NumberOfDislikes ?? 0,
                                NumberOfViews    = video.NumberOfViews ?? 0,
                                NumberOfLikes    = video.NumberOfLikes ?? 0,
                                Etag             = video.Etag,
                                NumberOfComments = video.NumberOfComments ?? 0,
                                Kind             = video.Kind
                            };
                            if (!videoList.Select(p => p.Id).Contains(videoDto.Id))
                            {
                                videoList.Add(videoDto);
                            }
                        }
                    }
                }
            }


            foreach (var videoDto in videoList)
            {
                _unitOfWork.UserVideoWatchedRepository.Insert(new UserVideoWatched()
                {
                    Id      = -1,
                    UserId  = user.Id,
                    VideoId = videoDto.Id
                });
            }

            _unitOfWork.Save();

            return(videoList);
        }
 /// <summary>
 /// Public Constructor,initializes privately declared local variables.
 /// </summary>
 /// <param name="context"></param>
 public GenericRepositorySQL(SimplerNewsSQLDb context)
 {
     this.Context = context;
     this.DbSet   = context.Set <TEntity>();
 }