Beispiel #1
0
        private async Task <ProviderVenueDetailViewModel> CreateOrUpdateProviderVenueAsync(ProviderVenueQualificationDto providerVenueQualification, int providerId)
        {
            if (string.IsNullOrEmpty(providerVenueQualification.VenuePostcode))
            {
                return(null);
            }

            var venueViewModel = await _providerVenueService.GetVenueWithTrimmedPostcodeAsync(providerId,
                                                                                              providerVenueQualification.VenuePostcode);

            if (venueViewModel == null)
            {
                var addProviderVenue = new AddProviderVenueViewModel
                {
                    ProviderId = providerId,
                    Postcode   = providerVenueQualification.VenuePostcode,
                    Source     = Source
                };

                var venueId = await _providerVenueService.CreateVenueAsync(addProviderVenue);

                Debug.WriteLine(
                    $"Created venue with id {venueId} for provider {providerId} and postcode {providerVenueQualification.VenuePostcode}");

                venueViewModel = await _providerVenueService.GetVenueAsync(venueId);
            }

            var providerVenueValidator = ValidateProviderVenueToUpdate(venueViewModel, providerVenueQualification);

            if (providerVenueValidator.IsUpdated)
            {
                await _providerVenueService.UpdateVenueAsync(providerVenueValidator.ProviderVenueDetailViewModel);
            }

            // Provider Venue Delete
            if (venueViewModel.IsRemoved != providerVenueQualification.VenueIsRemoved)
            {
                var removeProviderVenueViewModel = new RemoveProviderVenueViewModel
                {
                    Postcode        = venueViewModel.Postcode,
                    ProviderId      = venueViewModel.ProviderId,
                    ProviderVenueId = venueViewModel.Id
                };

                if (providerVenueQualification.VenueIsRemoved)
                {
                    await _providerVenueService.UpdateVenueAsync(removeProviderVenueViewModel);
                }
                else
                {
                    await _providerVenueService.UpdateVenueToNotRemovedAsync(removeProviderVenueViewModel);
                }
            }

            return(venueViewModel);
        }