Esempio n. 1
0
        public async Task <IActionResult> GetMyServiceNodes(string id)
        {
            var myServiceNodes = repository.GetServiceNodes(id);

            if (myServiceNodes == null)
            {
                return(NotFound());
            }

            if (myServiceNodes.Count == 0)
            {
                return(Ok(CreateMyServiceNodeViewModel(myServiceNodes)));
            }

            var authorizationResult = await authorizationService.AuthorizeAsync(User, myServiceNodes.FirstOrDefault(), "CanCrudOwnServicenode");

            if (authorizationResult.Succeeded)
            {
                var allServiceNodes = servicenodeService.serviceNodeList();

                // ServiceNodeInfoResponse response;
                // foreach (var serviceNode in myServiceNodes)
                // {
                //     response = allServiceNodes.Find(sn => sn.Address == serviceNode.Address);
                // }

                unitOfWork.Complete();

                var myServiceNodesViewModel = CreateMyServiceNodeViewModel(myServiceNodes);

                return(Ok(myServiceNodesViewModel));
            }
            else if (User.Identity.IsAuthenticated)
            {
                return(new ForbidResult());
            }
            else
            {
                return(new ChallengeResult());
            }
        }
Esempio n. 2
0
        public IActionResult GetServiceNodeList([FromQuery] ServiceNodeQueryViewModel filterViewModel)
        {
            var result = new ServiceNodeQueryResult <Core.Models.ServiceNodeInfoResponse>();

            var configs      = xrouterService.xrShowConfigs();
            var servicenodes = servicenodeService.serviceNodeList();

            var xrouterEnabledServicenodes = servicenodes.Join(configs, sn => sn.SNodeKey, c => c.NodePubKey, (sn, c) => new {
                Config = c, ServiceNode = sn
            });

            var query = xrouterEnabledServicenodes.Select(q => {
                return(new Core.Models.ServiceNodeInfoResponse
                {
                    Type = q.ServiceNode.Exr ? "Enterprise" : "Regular",
                    Address = q.ServiceNode.Address,
                    Score = q.ServiceNode.Score,
                    SNodeKey = q.ServiceNode.SNodeKey,
                    Status = q.ServiceNode.Status,
                    Tier = q.ServiceNode.Tier,
                    TimeLastSeen = q.ServiceNode.TimeLastSeen,
                    TimeLastSeenStr = q.ServiceNode.TimeLastSeenStr,
                    SpvWallets = q.ServiceNode.Services.Where(xw => xw.Split(':')[0].Equals("xr")).Where(xw => !xw.Equals("xr")).ToList(),
                    XCloudServices = q.ServiceNode.Services.Where(xw => xw.Split(':')[0].Equals("xrs")).Where(xw => !xw.Equals("xrs")).ToList()
                });
            }).AsQueryable();

            if (filterViewModel.Page != null && filterViewModel.PageSize != null)
            {
                var queryObj = new ServiceNodeQuery
                {
                    Page          = (int)filterViewModel.Page,
                    PageSize      = (byte)filterViewModel.PageSize,
                    Type          = filterViewModel.Type,
                    SpvWallet     = filterViewModel.SpvWallet,
                    XCloudService = filterViewModel.XCloudService,
                    Search        = filterViewModel.Search
                };
                query = query.ApplyServiceNodeFiltering(queryObj);

                result.TotalItems = query.Count();

                result.TotalSpvWallets = query.SelectMany(sn => sn.SpvWallets)
                                         .Distinct()
                                         .Count();
                result.TotalXCloudServices = query.SelectMany(sn => sn.XCloudServices)
                                             .Distinct()
                                             .Count();

                query = query.ApplyPaging(queryObj);
            }
            else
            {
                result.TotalItems = query.Count();

                result.TotalSpvWallets = query.SelectMany(sn => sn.SpvWallets)
                                         .Distinct()
                                         .Count();
                result.TotalXCloudServices = query.SelectMany(sn => sn.XCloudServices)
                                             .Distinct()
                                             .Count();
            }

            result.Items = query.ToList();

            //TODO: Add an automapper module
            var viewModel = new ServiceNodeQueryResultViewModel <ServiceNodeViewModel>
            {
                Items = result.Items.Select(sn => new ServiceNodeViewModel
                {
                    Type            = sn.Type,
                    Score           = sn.Score,
                    Tier            = sn.Tier,
                    TimeLastSeen    = sn.TimeLastSeen,
                    TimeLastSeenStr = sn.TimeLastSeenStr,
                    Address         = sn.Address,
                    SNodeKey        = sn.SNodeKey,
                    Status          = sn.Status,
                    SpvWallets      = sn.SpvWallets,
                    XCloudServices  = sn.XCloudServices
                }).ToList(),
                TotalItems          = result.TotalItems,
                TotalSpvWallets     = result.TotalSpvWallets,
                TotalXCloudServices = result.TotalXCloudServices
            };

            return(Ok(viewModel));
        }