Пример #1
0
        public ImageBucket Top100ImagesByDate()
        {
            IEnumerable <ResponseItem> top100ImagesByDate = CacheHandler.Register <IEnumerable <ResponseItem> >("LegacyApi.Top100ImagesByDate",
                                                                                                                () =>
            {
                using (voatEntities db = new voatEntities(CONSTANTS.CONNECTION_READONLY))
                {
                    var query = from pro in db.Submissions.Where(s => s.Type == 2 && (s.Content.EndsWith(".png") || s.Content.EndsWith(".jpg")) && s.UpCount > 10 && !s.Subverse1.IsAdult).OrderByDescending(s => s.CreationDate).Take(100)
                                select new ResponseItem
                    {
                        SubmissionId = pro.ID,
                        Alt          = pro.LinkDescription,
                        Img          = pro.Content,
                        DownVotes    = pro.DownCount,
                        SubmittedBy  = pro.UserName,
                        SubmittedOn  = pro.CreationDate,
                        Subverse     = pro.Subverse,
                        UpVotes      = pro.UpCount
                    };
                    return(query.ToList());
                }
            }, TimeSpan.FromMinutes(90), 0);

            var bucket   = new ImageBucket();
            var itemList = top100ImagesByDate.ToList();

            bucket.Items = itemList;
            return(bucket);
        }
Пример #2
0
        public async Task TestNewImageBucketCreatesResult()
        {
            // arrange
            var key = new ImageBucketKey
            {
                Store = "some-store"
            };
            var imageBucket = new ImageBucket
            {
                StoreBucket = key,
                ImageList   = new List <string> {
                    "profile-picture-1", "profile-picture-2"
                }
            };

            // act
            var(newImageBucket, outcomeAction, error) = await _imageBucketRepository.NewImageBucket(imageBucket);

            // assert
            Assert.NotNull(newImageBucket);
            Assert.Equal(RepositoryOutcomeAction.OkCreated, outcomeAction);
            Assert.Null(error);
            Assert.Equal(key.Store, newImageBucket.StoreBucket.Store);
            Assert.NotNull(newImageBucket.StoreBucket.Bucket);
            Assert.Collection(newImageBucket.ImageList,
                              _ => Assert.Equal("profile-picture-1", _),
                              _ => Assert.Equal("profile-picture-2", _));
            Assert.True(
                _dataStore
                .TestImageBucket(_ =>
                                 _.StoreBucket.Store == newImageBucket.StoreBucket.Store &&
                                 _.StoreBucket.Bucket == newImageBucket.StoreBucket.Bucket));
        }
Пример #3
0
        public async Task TestUpdateImageBucketReturnsNullOnNonExistantImageBucket()
        {
            // arrange
            var key = new ImageBucketKey
            {
                Store  = "non-existant-store",
                Bucket = Guid.NewGuid().ToString("n")
            };

            var imageBucketToUpdate =
                new ImageBucket
            {
                StoreBucket = key,
                ImageList   = new List <string> {
                    "profile-picture-5", "profile-picture-6"
                }
            };

            _dataStore.EnsureImageBucketDoesNotExist(key);

            // act
            var(updatedImageBucket, outcomeAction, error) = await _imageBucketRepository.UpdateImageBucket(imageBucketToUpdate);

            // assert
            Assert.Null(updatedImageBucket);
            Assert.Equal(RepositoryOutcomeAction.NotFoundNone, outcomeAction);
            Assert.Null(error);
        }
Пример #4
0
        public async Task TestUpdateImageBucketUpdatesExitentImageBucket()
        {
            // arrange
            var key = new ImageBucketKey
            {
                Store  = "some-store",
                Bucket = Guid.NewGuid().ToString("n")
            };

            var imageBucketToUpdate =
                new ImageBucket
            {
                StoreBucket = key,
                ImageList   = new List <string> {
                    "profile-picture-3", "profile-picture-5"
                }
            };

            _dataStore.EnsureImageBucket(
                new ImageBucket
            {
                StoreBucket = key,
                ImageList   = new List <string> {
                    "profile-picture-3", "profile-picture-4"
                }
            });

            // act
            var(updatedImageBucket, outcomeAction, error) = await _imageBucketRepository.UpdateImageBucket(imageBucketToUpdate);

            // assert
            Assert.NotNull(updatedImageBucket);
            Assert.Equal(RepositoryOutcomeAction.OkUpdated, outcomeAction);
            Assert.Null(error);
            Assert.Equal(key.Store, updatedImageBucket.StoreBucket.Store);
            Assert.Equal(key.Bucket, updatedImageBucket.StoreBucket.Bucket);
            Assert.Collection(updatedImageBucket.ImageList,
                              _ => Assert.Equal("profile-picture-3", _),
                              _ => Assert.Equal("profile-picture-5", _));
            Assert.True(
                _dataStore
                .TestImageBucket(_ =>
                                 _.StoreBucket.Store == updatedImageBucket.StoreBucket.Store &&
                                 _.StoreBucket.Bucket == updatedImageBucket.StoreBucket.Bucket));
        }
Пример #5
0
 public void EnsureImageBucket(ImageBucket imageBucket)
 {
     _mongoDatabase.ImageBuckets().InsertOne(imageBucket);
 }
Пример #6
0
        public async Task <(ImageBucket imageBucket, RepositoryOutcomeAction outcomeAction, object error)> UpdateImageBucket(ImageBucket imageBucket, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (imageBucket == null)
            {
                throw new ArgumentNullException(nameof(imageBucket));
            }
            var result = await _mongoDatabase.ImageBuckets().ReplaceOneAsync(_ => _.StoreBucket.Store == imageBucket.StoreBucket.Store && _.StoreBucket.Bucket == imageBucket.StoreBucket.Bucket, imageBucket, cancellationToken: cancellationToken);

            return
                (result.ModifiedCount == 1 && result.ModifiedCount == result.MatchedCount
                    ? (imageBucket, RepositoryOutcomeAction.OkUpdated, (object)null)
                    : (null, RepositoryOutcomeAction.NotFoundNone, null));
        }
Пример #7
0
        public async Task <(ImageBucket imageBucket, RepositoryOutcomeAction outcomeAction, object error)> NewImageBucket(ImageBucket imageBucket, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (imageBucket == null)
            {
                throw new ArgumentNullException(nameof(imageBucket));
            }
            imageBucket.StoreBucket.Bucket = Guid.NewGuid().ToString("n");
            await _mongoDatabase.ImageBuckets().InsertOneAsync(imageBucket, cancellationToken: cancellationToken);

            return(imageBucket, RepositoryOutcomeAction.OkCreated, null);
        }
Пример #8
0
        public ImageBucket Top100ImagesByDate()
        {
            IEnumerable<ResponseItem> top100ImagesByDate = CacheHandler.Register<IEnumerable<ResponseItem>>("LegacyApi.Top100ImagesByDate",
                () =>
                {
                    using (voatEntities db = new voatEntities(CONSTANTS.CONNECTION_READONLY))
                    {
                        var query = from pro in db.Submissions.Where(s => s.Type == 2 && (s.Content.EndsWith(".png") || s.Content.EndsWith(".jpg")) && s.UpCount > 10 && !s.Subverse1.IsAdult).OrderByDescending(s => s.CreationDate).Take(100)
                                    select new ResponseItem
                                    {
                                        SubmissionId = pro.ID,
                                        Alt = pro.LinkDescription, 
                                        Img = pro.Content,
                                        DownVotes = pro.DownCount,
                                        SubmittedBy = pro.UserName,
                                        SubmittedOn = pro.CreationDate,
                                        Subverse = pro.Subverse,
                                        UpVotes = pro.UpCount
                                    };
                        return query.ToList();
                    }
                }, TimeSpan.FromMinutes(90), 0);

            var bucket = new ImageBucket();
            var itemList = top100ImagesByDate.ToList();
            bucket.Items = itemList;
            return bucket;
        }