public void GetPaginatedObjectsQuery_Validation_Failures()
        {
            // Arrange
            var mediator = GetService <IMediator>();
            var ct       = new CancellationToken();
            var query    = new GetPaginatedQuery {
                Page = -1
            };

            // Act & Assert
            var ex = Assert.ThrowsAsync <ValidationException>(() => mediator.Send(query, ct));

            Assert.AreEqual("One or more validation failures have occurred.", ex.Message);
            Assert.IsNotNull(ex.Failures);
            Assert.AreEqual(2, ex.Failures.Count);
            var firstEntry = ex.Failures[nameof(query.Page)];

            Assert.AreEqual(1, firstEntry.Length);
            Assert.AreEqual("'Page' must be greater than or equal to '0'.", firstEntry[0]);
            var secondEntry = ex.Failures[nameof(query.Limit)];

            Assert.AreEqual(3, secondEntry.Length);
            Assert.AreEqual("'Limit' must not be empty.", secondEntry[0]);
            Assert.AreEqual("'Limit' must be greater than '0'.", secondEntry[1]);
            Assert.AreEqual("'Limit' must be one of these values: 10, 25 or 50", secondEntry[2]);
        }
        public void GetPaginatedObjectsQuery()
        {
            // Arrange
            var    mediator = GetService <IMediator>();
            var    ct       = new CancellationToken();
            string filename = "SMIR.Brain_3more.XX.XX.OT.6560.mha";
            string dataPath = GetDataPath(filename);
            var    command  = new CreateObjectCommand()
            {
                OriginalFilename = filename,
                SourcePath       = CreateTempFile(dataPath)
            };
            var commandResult = mediator.Send(command, ct).Result;
            var query         = new GetPaginatedQuery {
                Page = 0, Limit = 25
            };

            try
            {
                // Act
                var result = mediator.Send(query, ct).Result;

                // Assert
                Assert.IsNotNull(result);
                Assert.IsNotNull(result.Pagination);
                Assert.AreEqual(1, result.Pagination.Total);
                Assert.AreEqual(0, result.Pagination.Page);

                DeleteObject(commandResult.Id);
                Assert.IsFalse(File.Exists(GetWorkingDirectoryPath(commandResult.SourcePath)));
            }
            finally
            {
                DeleteDirectory(Path.GetDirectoryName(command.SourcePath));
            }
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        protected override async Task <PaginationResultModel <EventModel> > ProtectedHandleAsync(GetPaginatedQuery request, CancellationToken cancellationToken)
        {
            var principal = PrincipalProvider.GetPrincipal();

            if (principal == null)
            {
                throw new ForbiddenException("Not authenticated");
            }

            Expression <Func <EventEntity, bool> > expression = PredicateBuilder.True <EventEntity>();

            if (!principal.IsInRole(RoleType.Administrator))
            {
                expression = expression.And(e => e.UserId == principal.Identity.Name);
            }

            int total = await Context.EventRepository.CountAsync(expression, cancellationToken);

            var query = Context.EventRepository
                        .GetQuery(expression)
                        .OrderByDescending(e => e.CreatedDate)
                        .Skip(request.Page * request.Limit)
                        .Take(request.Limit);

            var entities = await Context.ToListAsync(query, cancellationToken);

            var models = entities.Select(e => EventModel.Create(e, Serializer));

            return(PaginationResultModel <EventModel> .Create(models, request.Page, request.Limit, total));
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        protected override async Task <PaginationResultModel <AppLogModel> > ProtectedHandleAsync(GetPaginatedQuery request, CancellationToken cancellationToken)
        {
            int total = await appLogRepository.CountAsync(cancellationToken);

            var result = appLogRepository
                         .GetQuery()
                         .OrderByDescending(e => e.Timestamp)
                         .Skip(request.Page * request.Limit)
                         .Take(request.Limit)
                         .Select(e => AppLogModel.Create(e));

            return(PaginationResultModel <AppLogModel> .Create(result, request.Page, request.Limit, total));
        }