Esempio n. 1
0
        public async Task HandleAsync(CreateFlatForRentAnnouncementPreferenceCommand command, CancellationToken cancellationToken = default)
        {
            var getUserResult = await _userGetterService.GetByIdAsync(command.UserId);

            if (!getUserResult.Success)
            {
                throw new ResourceNotFoundException(getUserResult.Errors);
            }

            var cityAndCityDistrictsVerificationResult = await _cityVerificationService.VerifyCityAndCityDistrictsAsync(command.CityId, command.CityDistricts);

            if (!cityAndCityDistrictsVerificationResult.Success)
            {
                throw new ValidationException(cityAndCityDistrictsVerificationResult.Errors);
            }

            var announcementPreferenceLimitIsNotExceededVerificationResult =
                _userVerificationService.VerifyAnnouncementPreferenceLimitIsNotExceeded(
                    getUserResult.Value.AnnouncementPreferenceLimit,
                    getUserResult.Value.FlatForRentAnnouncementPreferences.Count +
                    getUserResult.Value.RoomForRentAnnouncementPreferences.Count);

            if (!announcementPreferenceLimitIsNotExceededVerificationResult.Success)
            {
                throw new ValidationException(announcementPreferenceLimitIsNotExceededVerificationResult.Errors);
            }

            var flatForRentAnnouncementPreference =
                _mapper.Map <CreateFlatForRentAnnouncementPreferenceCommand, FlatForRentAnnouncementPreference>(command);
            var flatForRentAnnouncementPreferences = getUserResult.Value.FlatForRentAnnouncementPreferences.ToList();

            flatForRentAnnouncementPreferences.Add(flatForRentAnnouncementPreference);
            var flatForRentAnnouncementPreferencesVerificationResult =
                _flatForRentAnnouncementPreferenceVerificationService.VerifyFlatForRentAnnouncementPreferences(flatForRentAnnouncementPreferences);

            if (!flatForRentAnnouncementPreferencesVerificationResult.Success)
            {
                throw new ValidationException(flatForRentAnnouncementPreferencesVerificationResult.Errors);
            }

            getUserResult.Value.AddFlatForRentAnnouncementPreference(flatForRentAnnouncementPreference, command.CorrelationId);
            await _communicationBus.DispatchDomainEventsAsync(getUserResult.Value, cancellationToken);

            await _userRepository.UpdateAsync(getUserResult.Value);

            var userFlatForRentAnnouncementPreferenceCreatedIntegrationEvent =
                new UserFlatForRentAnnouncementPreferenceCreatedIntegrationEvent(command.CorrelationId,
                                                                                 getUserResult.Value.Id,
                                                                                 flatForRentAnnouncementPreference.Id,
                                                                                 getUserResult.Value.Email,
                                                                                 flatForRentAnnouncementPreference.CityId,
                                                                                 getUserResult.Value.ServiceActive,
                                                                                 getUserResult.Value.AnnouncementSendingFrequency.ConvertToEnum(),
                                                                                 flatForRentAnnouncementPreference.PriceMin,
                                                                                 flatForRentAnnouncementPreference.PriceMax,
                                                                                 flatForRentAnnouncementPreference.RoomNumbersMin,
                                                                                 flatForRentAnnouncementPreference.RoomNumbersMax,
                                                                                 flatForRentAnnouncementPreference.CityDistricts);
            await _integrationEventBus.PublishIntegrationEventAsync(userFlatForRentAnnouncementPreferenceCreatedIntegrationEvent);
        }
Esempio n. 2
0
        public void VerifyFlatForRentAnnouncementPreferences_Should_Return_Success_VerificationResult()
        {
            var cityId        = Guid.NewGuid();
            var cityDistricts = new List <Guid> {
                Guid.NewGuid()
            };
            var flatForRentAnnouncementPreferences = new List <FlatForRentAnnouncementPreference>
            {
                FlatForRentAnnouncementPreference.Builder()
                .SetId(Guid.NewGuid())
                .SetCityId(cityId)
                .SetPriceMin(0)
                .SetPriceMax(1000)
                .SetRoomNumbersMin(1)
                .SetRoomNumbersMax(1)
                .SetCityDistricts(cityDistricts)
                .Build(),
                FlatForRentAnnouncementPreference.Builder()
                .SetId(Guid.NewGuid())
                .SetCityId(cityId)
                .SetPriceMin(0)
                .SetPriceMax(2000)
                .SetRoomNumbersMin(1)
                .SetRoomNumbersMax(2)
                .SetCityDistricts(cityDistricts)
                .Build(),
                FlatForRentAnnouncementPreference.Builder()
                .SetId(Guid.NewGuid())
                .SetCityId(Guid.NewGuid())
                .SetPriceMin(0)
                .SetPriceMax(2000)
                .SetRoomNumbersMin(1)
                .SetRoomNumbersMax(2)
                .SetCityDistricts(new List <Guid> {
                    Guid.NewGuid()
                })
                .Build()
            };
            var expectedResult = VerificationResult.Ok();

            var result = _service.VerifyFlatForRentAnnouncementPreferences(flatForRentAnnouncementPreferences);

            result.Should().BeEquivalentTo(expectedResult);
        }
Esempio n. 3
0
        public async Task HandleAsync(UpdateFlatForRentAnnouncementPreferenceCommand command, CancellationToken cancellationToken = default)
        {
            var getUserResult = await _userGetterService.GetByIdAsync(command.UserId);

            if (!getUserResult.Success)
            {
                throw new ResourceNotFoundException(getUserResult.Errors);
            }

            var getFlatForRentAnnouncementPreferenceResult =
                _flatForRentAnnouncementPreferenceGetterService.GetByByUserAndId(
                    getUserResult.Value, command.FlatForRentAnnouncementPreferenceId);

            if (!getFlatForRentAnnouncementPreferenceResult.Success)
            {
                throw new ResourceNotFoundException(getFlatForRentAnnouncementPreferenceResult.Errors);
            }

            await UpdateCityAndCityDistrictsAsync(getFlatForRentAnnouncementPreferenceResult.Value, command.CityId, command.CityDistricts.ToList());

            getFlatForRentAnnouncementPreferenceResult.Value.ChangePriceMin(command.PriceMin);
            getFlatForRentAnnouncementPreferenceResult.Value.ChangePriceMax(command.PriceMax);
            getFlatForRentAnnouncementPreferenceResult.Value.ChangeRoomNumbersMin(command.RoomNumbersMin);
            getFlatForRentAnnouncementPreferenceResult.Value.ChangeRoomNumbersMax(command.RoomNumbersMax);

            var flatForRentAnnouncementPreferencesVerificationResult =
                _flatForRentAnnouncementPreferenceVerificationService.VerifyFlatForRentAnnouncementPreferences(getUserResult.Value.FlatForRentAnnouncementPreferences);

            if (!flatForRentAnnouncementPreferencesVerificationResult.Success)
            {
                throw new ValidationException(flatForRentAnnouncementPreferencesVerificationResult.Errors);
            }

            getUserResult.Value.AddFlatForRentAnnouncementPreferenceChangedEvent(getFlatForRentAnnouncementPreferenceResult.Value, command.CorrelationId);
            await _communicationBus.DispatchDomainEventsAsync(getUserResult.Value, cancellationToken);

            await _userRepository.UpdateAsync(getUserResult.Value);

            var userFlatForRentAnnouncementPreferenceUpdatedIntegrationEvent =
                new UserFlatForRentAnnouncementPreferenceUpdatedIntegrationEvent(command.CorrelationId,
                                                                                 getUserResult.Value.Id,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.Id,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.CityId,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.PriceMin,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.PriceMax,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.RoomNumbersMin,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.RoomNumbersMax,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.CityDistricts);
            await _integrationEventBus.PublishIntegrationEventAsync(userFlatForRentAnnouncementPreferenceUpdatedIntegrationEvent);
        }