Ejemplo n.º 1
0
        public async Task <IActionResult> Get(
            [FromServices] LegacyContext context,
            [FromServices] SyndicationContext syndicationContext,
            [FromServices] IOptions <ResponseOptions> responseOptions,
            [FromRoute] int persistentLocalId,
            [FromRoute] Taal?taal,
            CancellationToken cancellationToken = default)
        {
            var address = await context
                          .AddressDetail
                          .AsNoTracking()
                          .SingleOrDefaultAsync(item => item.PersistentLocalId == persistentLocalId, cancellationToken);

            if (address != null && address.Removed)
            {
                throw new ApiException("Adres werd verwijderd.", StatusCodes.Status410Gone);
            }

            if (address == null || !address.Complete)
            {
                throw new ApiException("Onbestaand adres.", StatusCodes.Status404NotFound);
            }

            var streetName = await syndicationContext.StreetNameLatestItems.FindAsync(new object[] { address.StreetNameId }, cancellationToken);

            var municipality = await syndicationContext.MunicipalityLatestItems.FirstAsync(m => m.NisCode == streetName.NisCode, cancellationToken);

            var defaultMunicipalityName = AddressMapper.GetDefaultMunicipalityName(municipality);
            var defaultStreetName       = AddressMapper.GetDefaultStreetNameName(streetName, municipality.PrimaryLanguage);
            var defaultHomonymAddition  = AddressMapper.GetDefaultHomonymAddition(streetName, municipality.PrimaryLanguage);

            var gemeente = new AdresDetailGemeente(
                municipality.NisCode,
                string.Format(responseOptions.Value.GemeenteDetailUrl, municipality.NisCode),
                new GeografischeNaam(defaultMunicipalityName.Value, defaultMunicipalityName.Key));

            var straat = new AdresDetailStraatnaam(
                streetName.PersistentLocalId,
                string.Format(responseOptions.Value.StraatnaamDetailUrl, streetName.PersistentLocalId),
                new GeografischeNaam(defaultStreetName.Value, defaultStreetName.Key));

            var postInfo = string.IsNullOrEmpty(address.PostalCode)
                ? null
                : new AdresDetailPostinfo(
                address.PostalCode,
                string.Format(responseOptions.Value.PostInfoDetailUrl, address.PostalCode));

            var homoniemToevoeging = defaultHomonymAddition == null
                ? null
                : new HomoniemToevoeging(new GeografischeNaam(defaultHomonymAddition.Value.Value, defaultHomonymAddition.Value.Key));

            return(Ok(
                       new AddressResponse(
                           responseOptions.Value.Naamruimte,
                           address.PersistentLocalId.ToString(),
                           address.HouseNumber,
                           address.BoxNumber,
                           gemeente,
                           straat,
                           homoniemToevoeging,
                           postInfo,
                           AddressMapper.GetAddressPoint(address.Position),
                           AddressMapper.ConvertFromGeometryMethod(address.PositionMethod),
                           AddressMapper.ConvertFromGeometrySpecification(address.PositionSpecification),
                           AddressMapper.ConvertFromAddressStatus(address.Status),
                           defaultStreetName.Key,
                           address.OfficiallyAssigned,
                           address.VersionTimestamp.ToBelgianDateTimeOffset())));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> List(
            [FromServices] LegacyContext context,
            [FromServices] AddressQueryContext queryContext,
            [FromServices] IOptions <ResponseOptions> responseOptions,
            Taal?taal,
            CancellationToken cancellationToken = default)
        {
            var filtering  = Request.ExtractFilteringRequest <AddressFilter>();
            var sorting    = Request.ExtractSortingRequest();
            var pagination = Request.ExtractPaginationRequest();

            var pagedAddresses = new AddressListQuery(queryContext)
                                 .Fetch(filtering, sorting, pagination);

            Response.AddPagedQueryResultHeaders(pagedAddresses);

            var addresses = await pagedAddresses.Items
                            .Select(a =>
                                    new
            {
                a.PersistentLocalId,
                a.StreetNameId,
                a.HouseNumber,
                a.BoxNumber,
                a.PostalCode,
                a.VersionTimestamp
            })
                            .ToListAsync(cancellationToken);

            var streetNameIds = addresses
                                .Select(x => x.StreetNameId)
                                .Distinct()
                                .ToList();

            var streetNames = await queryContext
                              .StreetNameLatestItems
                              .Where(x => streetNameIds.Contains(x.StreetNameId))
                              .ToListAsync(cancellationToken);

            var nisCodes = streetNames
                           .Select(x => x.NisCode)
                           .Distinct()
                           .ToList();

            var municipalities = await queryContext
                                 .MunicipalityLatestItems
                                 .Where(x => nisCodes.Contains(x.NisCode))
                                 .ToListAsync(cancellationToken);

            var addressListItemResponses = addresses
                                           .Select(a =>
            {
                var streetName   = streetNames.Single(x => x.StreetNameId == a.StreetNameId);
                var municipality = municipalities.Single(x => x.NisCode == streetName.NisCode);
                return(new AddressListItemResponse(
                           a.PersistentLocalId,
                           responseOptions.Value.Naamruimte,
                           responseOptions.Value.DetailUrl,
                           a.HouseNumber,
                           a.BoxNumber,
                           AddressMapper.GetVolledigAdres(a.HouseNumber, a.BoxNumber, a.PostalCode, streetName, municipality),
                           a.VersionTimestamp.ToBelgianDateTimeOffset()));
            })
                                           .ToList();

            return(Ok(new AddressListResponse
            {
                Adressen = addressListItemResponses,
                Volgende = pagedAddresses.PaginationInfo.BuildNextUri(responseOptions.Value.VolgendeUrl)
            }));
        }