Example #1
0
 public override async Task Authorize(GetDeviceConnectionsCommand command)
 {
     if (!await assetDataService.UserHasRoleForDevice(command.UserId, UserAssetRole.Owner, command.DeviceId))
     {
         ApiResponse.IsUnauthorised();
     }
 }
Example #2
0
        public override Task Validate(GetDeviceConnectionsCommand command)
        {
            if (command.Page < 1)
            {
                ApiResponse.AddError(nameof(DeviceConnectionRequestModel.Page), "The Page property must be >= 1.");
            }

            if (command.PerPage < 10 || command.PerPage > 100)
            {
                ApiResponse.AddError(nameof(DeviceConnectionRequestModel.PerPage),
                                     "The PerPage property must be between 10 and 100.");
            }

            return(Task.CompletedTask);
        }
Example #3
0
        public override async Task <ResultsPaginationResponseModel <DeviceConnectionResponseModel> > Handle(
            GetDeviceConnectionsCommand command)
        {
            IQueryable <DeviceConnectionEntity> queryable = repository.GetEntities <DeviceConnectionEntity>()
                                                            .Where(x => x.DeviceId == command.DeviceId);

            int totalResults = await queryable.CountAsync();

            List <DeviceConnectionEntity> connections = await queryable.OrderByDescending(x => x.Id)
                                                        .Skip((command.Page - 1) * command.PerPage)
                                                        .Take(command.PerPage)
                                                        .ToListAsync();

            List <int> deviceConnectionsIds = connections.Select(x => x.Id).ToList();

            var messageCounts =
                await repository.GetEntities <DeviceConnectionMessageEntity>()
                .Where(x => deviceConnectionsIds.Contains(x.DeviceConnectionId))
                .GroupBy(x => x.DeviceConnectionId)
                .Select(x => new { DeviceConnectionId = x.Key, Messages = x.Count() })
                .ToListAsync();

            return(new ResultsPaginationResponseModel <DeviceConnectionResponseModel>
            {
                Results = connections
                          .Select(x => new DeviceConnectionResponseModel
                {
                    Id = x.Id,
                    DeviceId = command.DeviceId,
                    OpenedAt = x.OpenedAt,
                    ClosedAt = x.ClosedAt,
                    RemoteEndPoint = x.RemoteEndPoint,
                    Messages = (messageCounts.FirstOrDefault(y => y.DeviceConnectionId == x.Id)?.Messages)
                               .GetValueOrDefault()
                }).ToList(),
                Pagination =
                {
                    MaxPage = (totalResults + command.PerPage - 1) / command.PerPage,
                    PerPage = command.PerPage,
                    Page    = command.Page
                },
                TotalResults = totalResults
            });
        }