private static async void BuiltInStorageTests(List <PublicTopic> firstTopics)
        {
            PhotosRepository repo = new PhotosRepository();

            var topicNames = firstTopics.Select(x => x.Title);

            Parallel.Invoke(
                () =>
            {
                if (!TheAppContext.IsTravisCI)
                {
                    repo.CreateTopics(topicNames);
                }
            },
                () =>
            {
/*
 *                  Parallel.ForEach(topicsWithBlobs, (topicWithBlobs) =>
 *                  {
 *                      var idContentList = topicWithBlobs.Blobs.Select(x => x.IdContent).ToList();
 *                      repo.CreateContent(idContentList);
 *                      Console.WriteLine($"Saved {idContentList.Count} related blobs of Topic: {topicWithBlobs.Title}");
 *                  });
 */
            });

            var topics       = new[] { "One Topic", "Another Topic" };
            var totalActions = new List <Action>();

            foreach (var topic_ in topics)
            {
                var      topic   = topic_;
                Action[] actions = new Action[]
                {
                    () => repo.AddUserAction(topic, "Tester", "Disliked-content", UserAction.Dislike),
                    () => repo.AddUserAction(topic, "Tester", "Liked-content", UserAction.Like),
                    () => repo.AddUserAction(topic, "Another Tester", "Liked-content", UserAction.Like),
                    () => repo.AddUserAction(topic, "Tester", "Starred-content", UserAction.Star),
                    () => repo.AddUserAction(topic, "Tester", "Shared-content", UserAction.Share),
                    () =>
                    {
                        repo.AddUserAction(topic, "Tester", "Double-Liked-content", UserAction.Like);
                        repo.AddUserAction(topic, "Tester", "Double-Liked-content", UserAction.Like);
                    },
                    () =>
                    {
                        repo.AddUserAction(topic, "Tester", "Liked-and-then-Disliked-content", UserAction.Like);
                        repo.AddUserAction(topic, "Tester", "Liked-and-then-Disliked-content", UserAction.Dislike);
                    },
                    () =>
                    {
                        repo.AddUserAction(topic, "Tester", "Disliked-and-then-Liked-content", UserAction.Dislike);
                        repo.AddUserAction(topic, "Tester", "Disliked-and-then-Liked-content", UserAction.Like);
                    },
                };

                totalActions.AddRange(actions);
            }

            var             numThreads = totalActions.Count;
            ParallelOptions opts       = new ParallelOptions()
            {
                MaxDegreeOfParallelism = numThreads
            };

            Parallel.ForEach(totalActions, opts, (a) =>
            {
                try
                {
                    a();
                }
                catch
                {
                }
            });

            try
            {
                repo.GetUserPhotosByTopic("One Topic", "Tester");
                Stopwatch sw     = Stopwatch.StartNew();
                var       byUser = await repo.GetUserPhotosByTopic("One Topic", "Tester");

                Console.WriteLine($"Marks retrieved in {sw.Elapsed}:");
                foreach (var userPhoto in byUser)
                {
                    Console.WriteLine("  " + userPhoto.Value.ToDebugString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("TEST FAILED: repo.GetUserPhotosByTopic(\"One Topic\", \"Tester\");");
                Console.WriteLine(ex);
            }
        }
Example #2
0
        public async Task <IActionResult> GetSliderHtml([FromForm] string galleryTitle, [FromForm] string limits, [FromForm] string ratio)
        {
            _Logger.LogDebug("User1: " + HttpContext.User?.Identity?.Name
                             + Environment.NewLine + "User2: " + User.Identity.Name
                             );


            PublicLimits              limitsParsed = PublicLimits.Parse(limits);
            List <PublicModel>        meta         = _ContentManager.GetMetadata();
            IEnumerable <PublicModel> byLimits     = meta.Where(x => x.LimitValue == limitsParsed.LimitValue && x.Kind == limitsParsed.Kind);
            IEnumerable <PublicTopic> byTitle      = byLimits.SelectMany(x => x.Topics).Where(x => x.Title == galleryTitle);
            PublicTopic foundGallery = byTitle.FirstOrDefault();

            if (foundGallery == null)
            {
                throw new ArgumentException($"Gallery [{galleryTitle}] with specified limits ({limitsParsed}) not found");
            }

            decimal ratioParsed;

            if (!decimal.TryParse(ratio, out ratioParsed))
            {
                ratioParsed = 0;
            }

            if (ratioParsed < 1)
            {
                ratioParsed = 1;
            }


            // Shuffle gallery based on remote ip
            var galleryCopy = new PublicTopic()
            {
                Title = foundGallery.Title,
                Blobs = new List <PublicBlob>(foundGallery.Blobs)
            };
            var idUser = User?.Identity?.Name;

            if (string.IsNullOrEmpty(idUser))
            {
                idUser = HttpContext.Connection.RemoteIpAddress.ToString();
            }
            var seedByIp = HashExtentions.GetSHA1AsSeed(idUser);

            galleryCopy.Blobs.Shuffle(seedByIp);

            var userAgent = HttpContext.Request.Headers["User-Agent"];
            var uaInfo    = new UserAgentInfo(userAgent);

            var userPhotosByTopic = await _PhotosRepository.GetUserPhotosByTopic(galleryCopy.Title, idUser);

            var photoTotals = await _PhotosRepository.GetPhotoTotalsByTopic(galleryCopy.Title);

            List <JsPhotoModel> jsPhotos =
                galleryCopy.Blobs.Select(x => new JsPhotoModel()
            {
                Id     = x.IdContent,
                Url    = x.Id,
                Height = x.Height,
                Width  = x.Width
            }).ToList();

            foreach (var jsPhoto in jsPhotos)
            {
                userPhotosByTopic.TryGetValue(jsPhoto.Id, out var myFlags);
                photoTotals.TryGetValue(jsPhoto.Id, out var totals);
                if (myFlags != null)
                {
                    jsPhoto.MyDislikes = myFlags.Dislikes;
                    jsPhoto.MyLikes    = myFlags.Likes;
                    jsPhoto.MyShares   = myFlags.Shares;
                    jsPhoto.MyStars    = myFlags.Stars;
                }

                if (totals != null)
                {
                    jsPhoto.TotalDislikes = totals.Dislikes;
                    jsPhoto.TotalLikes    = totals.Likes;
                    jsPhoto.TotalShares   = totals.Shares;
                    jsPhoto.TotalStars    = totals.Stars;
                }
            }

            var angularModel = new
            {
                Gallery  = galleryCopy.Title,
                Ratio    = ratioParsed,
                Limits   = limitsParsed,
                IsMobile = uaInfo.IsMobile,
                Photos   = jsPhotos,
            };

            return(PartialView("GalleryPartial", new PartialGalleryModel()
            {
                Limits = limitsParsed,
                Topic = galleryCopy,
                Ratio = ratioParsed,
                IsMobile = uaInfo.IsMobile,
                AngularModel = angularModel.ToNewtonJSon(TheAppContext.IsDebug)
            }));
        }