Ejemplo n.º 1
0
        public async Task Initialize()
        {
            if (_isInitialized)
            {
                return;
            }

            await _initalizationLock.WaitAsync();

            try
            {
                if (_isInitialized)
                {
                    return;
                }

                var groupsTask = ClientGroupsResource.GetGroups(_restClient);

                var allClients = await ClientsResource.FetchAsync(_restClient);

                _restClient.HubConnection.On <ClientDto>(HubEventNames.ClientConnected, OnClientConnected);
                _restClient.HubConnection.On <int>(HubEventNames.ClientDisconnected, OnClientDisconnected);

                var viewModels = await _appDispatcher.Current.InvokeAsync(() =>
                                                                          new ObservableCollection <ClientViewModel>(allClients.Select(dto => new DefaultClientViewModel(dto))));

                Clients          = new ConcurrentDictionary <int, ClientViewModel>(viewModels.ToDictionary(x => x.ClientId, x => x));
                ClientViewModels = viewModels;

                var groups = await groupsTask;
                _restClient.HubConnection.On <ClientGroupDto>(HubEventNames.ClientGroupCreated, OnClientGroupCreated);
                _restClient.HubConnection.On <ClientGroupDto>(HubEventNames.ClientGroupUpdated, OnClientGroupUpdated);
                _restClient.HubConnection.On <int>(HubEventNames.ClientGroupRemoved, OnClientGroupRemoved);

                var groupViewModels = await _appDispatcher.Current.InvokeAsync(() =>
                                                                               new ObservableCollection <ClientGroupViewModel>(groups.Select(x => new DefaultClientGroupViewModel(x, Clients))));

                Groups          = new ConcurrentDictionary <int, ClientGroupViewModel>(groupViewModels.ToDictionary(x => x.ClientGroupId, x => x));
                GroupViewModels = groupViewModels;

                _isInitialized = true;
            }
            finally
            {
                _initalizationLock.Release();
            }

            var foo = _appDispatcher.Current.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
            {
                OnPropertyChanged(nameof(Clients));
                OnPropertyChanged(nameof(ClientViewModels));
                OnPropertyChanged(nameof(Groups));
                OnPropertyChanged(nameof(GroupViewModels));
            }));
        }
        public async Task <IActionResult> PostAsync([FromBody] ClientsResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var client = _mapper.Map <ClientsResource, client>(resource);
            var result = await _clientService.SaveAsync(client);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            var ClientsResource = _mapper.Map <client, ClientsResource>(result.client);

            return(Ok(ClientsResource));
        }