Example #1
0
        public IActionResult Act(string userAction, string idPhoto)
        {
            if (!Enum.TryParse(typeof(UserAction), userAction, true, out var aRaw))
            {
                throw new ArgumentException($"Action '{userAction}' is not support", nameof(userAction));
            }

            UserAction a     = (UserAction)aRaw;
            var        topic = _ContentManager.GetMetadata().First().Topics.FirstOrDefault(t => t.Blobs.Any(b => b.IdContent == idPhoto));

            if (topic == null)
            {
                throw new ArgumentException($"Photo '{userAction}' not found", nameof(idPhoto));
            }

            string idUser = User?.Identity?.Name;

            if (string.IsNullOrEmpty(idUser))
            {
                throw new InvalidOperationException("Action requires authenticated user");
            }

            _photosRepository.AddUserAction(topic.Title, idUser, idPhoto, a);
            UserPhoto userPhoto = _photosRepository.GetUserPhoto(topic.Title, idUser, idPhoto);
            var       photo     = _photosRepository.GetContent(topic.Title, idPhoto);

            JsPhotoModel ret = new JsPhotoModel()
            {
                Id            = userPhoto.IdContent,
                Url           = "Not applicable",
                Height        = -1,
                Width         = -1,
                MyDislikes    = userPhoto.Dislikes,
                MyLikes       = userPhoto.Likes,
                MyShares      = userPhoto.Shares,
                MyStars       = userPhoto.Stars,
                TotalDislikes = photo.Dislikes,
                TotalLikes    = photo.Likes,
                TotalShares   = photo.Shares,
                TotalStars    = photo.Stars,
            };

            return(Ok(ret));
        }
        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);
            }
        }