public async Task <ClientViewModel> LoadClientAsync(int id, ClientViewModel clientViewModel)
        {
            var clientData = await _clientRepositoryAsync.GetByIdAsync(id);

            if (clientData == null)
            {
                return(null);
            }

            clientViewModel = _mapper.Map <Client, ClientViewModel>(clientData);

            return(clientViewModel);
        }
Пример #2
0
        public async Task <Client> UpdateClientAsync(Client newClient, Client originalClient)
        {
            if (originalClient.Id == 0)
            {
                originalClient = await _clientRepositoryAsync.GetByIdAsync(newClient.Id);



                var clientData = await _addressRepositoryAsync.GetByIdAsync(new AddressWithClientSpecification(newClient.ClientAddress.Id));

                originalClient.AddAddress(newClient.Id, clientData.Timestamp, clientData.Id, clientData.AddressName, clientData.AddressStreet,
                                          clientData.Apt, clientData.ZipCode,
                                          clientData.State, clientData.City, clientData.Country);

                foreach (ClientAddressLevel clientLevel in clientData.ClientAddressLevels)
                {
                    originalClient.AddAddressTypes(clientLevel.Id, clientLevel.Timestamp, Enum.GetName(typeof(AddressTypes), clientLevel.ClientAddressType));
                }
            }
            else
            {
                _clientRepositoryAsync.Attach(originalClient);
            }


            originalClient.UpdateClient(newClient.ClientName, Enum.GetName(typeof(ClientTypes), newClient.ClientType));
            originalClient.UpdateAddress(newClient.Id, newClient.ClientAddress.Id, newClient.ClientAddress.AddressName,
                                         newClient.ClientAddress.AddressStreet, newClient.ClientAddress.Apt, newClient.ClientAddress.ZipCode,
                                         newClient.ClientAddress.State, newClient.ClientAddress.City, newClient.ClientAddress.Country);

            foreach (ClientAddressLevel clientLevel in newClient.ClientAddress.ClientAddressLevels)
            {
                if (originalClient.ClientAddress.ClientAddressLevels.Where(e => e.ClientAddressType == clientLevel.ClientAddressType).Count() == 0)
                {
                    originalClient.AddAddressTypes(clientLevel.Id, clientLevel.Timestamp, Enum.GetName(typeof(AddressTypes), clientLevel.ClientAddressType));
                }
            }
            foreach (ClientAddressLevel clientLevel in originalClient.ClientAddress.ClientAddressLevels)
            {
                if (newClient.ClientAddress.ClientAddressLevels.Where(e => e.ClientAddressType == clientLevel.ClientAddressType).Count() == 0)
                {
                    _addressTypeRepositoryAsync.SetDelete(originalClient.ClientAddress.ClientAddressLevels.Where(e => e.Id == clientLevel.Id).FirstOrDefault());
                }
            }
            await _clientRepositoryAsync.UpdateAsync(originalClient);

            return(originalClient);
        }
        public async Task <ClientViewModel> LoadClientAddressAsync(int addressid, ClientViewModel clientViewModel)
        {
            var clientData = await _addressRepositoryAsync.GetByIdAsync(new AddressWithClientSpecification(addressid));


            clientViewModel.ClientAddress = _mapper.Map <ClientAddress, ClientAddressViewModel>(clientData);

            clientViewModel.ClientAddress.AddressLevels = new List <ClientAddressLevelViewModel>();

            foreach (AddressTypes addressType in Enum.GetValues(typeof(AddressTypes)))
            {
                clientViewModel.ClientAddress.AddressLevels.Add(new ClientAddressLevelViewModel()
                {
                    Id = clientData.ClientAddressLevels.Where(c => c.ClientAddressType == addressType).Count() > 0 ?
                         clientData.ClientAddressLevels.Where(c => c.ClientAddressType == addressType).Select(c => c.Id).FirstOrDefault()
                     : 0,
                    Timestamp   = clientData.ClientAddressLevels.Where(c => c.ClientAddressType == addressType).Select(c => c.Timestamp).FirstOrDefault(),
                    AddressType = addressType,
                    IsCheck     = clientData.ClientAddressLevels.Where(c => c.ClientAddressType == addressType).Count() > 0 ? true : false
                });
            }
            return(clientViewModel);
        }