Example #1
0
        public async Task <IResponseModel <CampaignCreateResponse> > Handle(CampaignCreateCommand request,
                                                                            CancellationToken cancellationToken)
        {
            // TODO: Logs
            var fromBuilding = await _buildingQueryService.FindAsync(request.FromIdBuilding);

            var toBuilding = await _buildingQueryService.FindAsync(request.ToIdBuilding);

            if (fromBuilding.City != toBuilding.City)
            {
                return(new BadRequestResponse <CampaignCreateResponse>("The buildings must be in the same city."));
            }

            if (fromBuilding.Street != toBuilding.Street)
            {
                return(new BadRequestResponse <CampaignCreateResponse>("The buildings must be on the same street."));
            }

            if (toBuilding.StreetNumber % 2 == 0 != (fromBuilding.StreetNumber % 2 == 0))
            {
                return(new BadRequestResponse <CampaignCreateResponse>(
                           "The buildings must be on the same side of the road."));
            }

            if (fromBuilding.StreetNumber > toBuilding.StreetNumber)
            {
                return(new BadRequestResponse <CampaignCreateResponse>(
                           "FromIdBuilding must have smaller street number than ToIdBuilding."));
            }

            if (fromBuilding.StreetNumber == toBuilding.StreetNumber)
            {
                return(new BadRequestResponse <CampaignCreateResponse>(
                           "The buildings must have different street number."));
            }

            var buildings = _buildingQueryService.GetAll(fromBuilding.City, fromBuilding.Street,
                                                         fromBuilding.IdBuilding, toBuilding.IdBuilding,
                                                         toBuilding.StreetNumber % 2 == 0);

            var calculation =
                await _bannerCalculateService.CalculateAsync(buildings.ToList(), request.PricePerSquareMeter);

            if (calculation == null)
            {
                return(new InternalError <CampaignCreateResponse>("The calculation task could not be completed."));
            }

            var result = await _campaignCreateService.CreateAsync(request, calculation);

            if (result == null)
            {
                return(new InternalError <CampaignCreateResponse>("The campaign could not be created."));
            }

            return(new SuccessResponse <CampaignCreateResponse>("Campaign has been created.", calculation));
        }
        public async Task <IResponseModel <IEnumerable <BuildingResponse> > > Handle(BuildingGetAllQuery request,
                                                                                     CancellationToken cancellationToken)
        {
            // TODO: FluentValidation
            var buildings = _buildingQueryService.GetAll(request.City, request.Street,
                                                         request.StreetStartNumber, request.StreetEndNumber, request.Even)?.ToList();

            if (buildings == null || !buildings.Any())
            {
                return(new NotFoundResponse <IEnumerable <BuildingResponse> >("No buildings could be found"));
            }

            return(new SuccessResponse <IEnumerable <BuildingResponse> >(
                       buildings.Select(_mapper.Map <Building, BuildingResponse>)));
        }