Beispiel #1
0
        public async Task <string> InsertAsync(Offer offer, string region)
        {
            NullCheck.ThrowIfNull <Offer>(offer);

            var provider = offer.provider;

            //Build as entities

            var offerEntity        = new OfferEntity().Build(provider, region);
            var offerAddressEntity = new AddressEntity().build(provider.address);

            //Create the coordinates and store the address of the offer

            _addressMaker.SetCoordinates(offerAddressEntity);
            await offerAddressEntity.InsertAsync(_context);

            //Store the offer including the address id as foreign key, the token and a timestamp
            offerEntity.address_id = offerAddressEntity.Id;
            offerEntity.token      = createToken();
            offerEntity.timestamp  = DateTime.Now;
            await offerEntity.InsertAsync(_context);

            //create the entities for the resources, calculate their coordinates, give them the offer foreign key and store them
            //Update the original offer with the ids from the created entities (helps us for testing and if we want to do more stuff with the offer in future features)

            int offer_id = offerEntity.id;

            if (!(offer.consumables is null))
            {
                foreach (var c in offer.consumables)
                {
                    await InsertAsync(offer_id, c);
                }
            }
            if (!(offer.personals is null))
            {
                foreach (var p in offer.personals)
                {
                    await InsertAsync(offer_id, p);
                }
            }
            if (!(offer.devices is null))
            {
                foreach (var d in offer.devices)
                {
                    await InsertAsync(offer_id, d);
                }
            }

            //Give back only the token
            return(offerEntity.token);
        }
        public async Task <string> InsertAsync(Demand demand)
        {
            NullCheck.ThrowIfNull <Offer>(demand);
            Provider provider = demand.provider;

            //Build as entities
            DemandEntity demandEntity = new DemandEntity().Build(provider);

            // Store address if available
            if (provider.address != null && provider.address.ContainsInformation())
            {
                AddressEntity demandAddressEntity = new AddressEntity().build(provider.address);
                _addressMaker.SetCoordinates(demandAddressEntity);
                await demandAddressEntity.InsertAsync(_context);

                demandEntity.address_id = demandAddressEntity.Id;
            }

            // Store demand
            demandEntity.created_at_timestamp = DateTime.Now;
            demandEntity.token = CreateToken();
            await demandEntity.InsertAsync(_context);

            var demandId = demandEntity.id;

            // Store resources
            if (demand.consumables != null)
            {
                foreach (Consumable c in demand.consumables)
                {
                    ConsumableDemandEntity entity = new ConsumableDemandEntity().Build(c);
                    entity.demand_id            = demandId;
                    entity.created_at_timestamp = DateTime.Now;
                    await entity.InsertAsync(_context);
                }
            }
            if (demand.devices != null)
            {
                foreach (var d in demand.devices)
                {
                    DeviceDemandEntity entity = new DeviceDemandEntity().Build(d);
                    entity.demand_id            = demandId;
                    entity.created_at_timestamp = DateTime.Now;
                    await entity.InsertAsync(_context);
                }
            }

            return(demandEntity.token);
        }
Beispiel #3
0
        public async Task SubscribeRegionAsync(RegionSubscription subscription, string region)
        {
            NullCheck.ThrowIfNull <RegionSubscription>(subscription);
            AddressEntity addressEntity = new AddressEntity()
            {
                PostalCode = subscription.postal_code,
                Country    = "Deutschland"
            };

            _addressMaker.SetCoordinates(addressEntity);
            subscription.latitude  = addressEntity.Latitude;
            subscription.longitude = addressEntity.Longitude;
            subscription.active    = true;
            subscription.region    = region;
            await subscription.InsertAsync(_context);
        }
Beispiel #4
0
        public async IAsyncEnumerable <DemandResource <Consumable> > QueryDemandsAsync(Consumable con)
        {
            NullCheck.ThrowIfNull <Consumable>(con);

            var consumable = new ConsumableDemandEntity().Build(con);

            var           maxDistance = con.kilometer;
            AddressEntity locationOfDemandedConsumable = null;

            if (con.address.ContainsInformation())
            {
                var consumableAddress = con.address;
                locationOfDemandedConsumable = new AddressEntity().build(consumableAddress);
                _addressMaker.SetCoordinates(locationOfDemandedConsumable);
            }

            var query = from demand in _context.demand as IQueryable <DemandEntity>
                        join c in _context.demand_consumable on demand.id equals c.demand_id
                        join ad in _context.address on demand.address_id equals ad.Id into tmp
                        from ad in tmp.DefaultIfEmpty()
                        where consumable.category == c.category && !c.is_deleted
                        select new { demand, c, ad };


            if (!string.IsNullOrEmpty(consumable.name))
            {
                query = query.Where(collection => consumable.name == collection.c.name);
            }

            if (!string.IsNullOrEmpty(consumable.manufacturer))
            {
                query = query.Where(collection => consumable.manufacturer == collection.c.manufacturer);
            }

            if (consumable.amount > 0)
            {
                query = query.Where(collection => consumable.amount <= collection.c.amount);
            }

            var results = await query.ToListAsync();

            foreach (var data in results)
            {
                var resource = new Consumable().build(data.c);

                // If the query specifies a location but the demand does not, the demand should not be considered.
                if (locationOfDemandedConsumable != null && data.ad == null)
                {
                    continue;
                }

                if (locationOfDemandedConsumable != null)
                {
                    var yLatitude  = data.ad.Latitude;
                    var yLongitude = data.ad.Longitude;
                    var distance   = DistanceCalculator.computeDistance(
                        locationOfDemandedConsumable.Latitude, locationOfDemandedConsumable.Longitude,
                        yLatitude, yLongitude);
                    if (distance > maxDistance && maxDistance != 0)
                    {
                        continue;
                    }
                    resource.kilometer = (int)Math.Round(distance);
                }

                var demand = new DemandResource <Consumable>()
                {
                    resource = resource
                };

                yield return(demand);
            }
        }
        public async IAsyncEnumerable <OfferResource <Consumable> > QueryOffersAsync(Consumable con, string region)
        {
            NullCheck.ThrowIfNull <Consumable>(con);

            var consumable        = new ConsumableEntity().Build(con);
            var maxDistance       = con.kilometer;
            var consumableAddress = con.address;
            var location          = new AddressEntity().build(consumableAddress);

            _addressMaker.SetCoordinates(location);

            var query = from o in _context.offer as IQueryable <OfferEntity>
                        join c in _context.consumable on o.id equals c.offer_id
                        join ap in _context.address on o.address_id equals ap.Id
                        where consumable.category == c.category && !c.is_deleted && o.region == region
                        select new { o, c, ap };

            if (!string.IsNullOrEmpty(consumable.name))
            {
                query = query.Where(collection => consumable.name == collection.c.name);
            }
            if (!string.IsNullOrEmpty(consumable.manufacturer))
            {
                query = query.Where(collection => consumable.manufacturer == collection.c.manufacturer);;
            }
            if (consumable.amount > 0)
            {
                query = query.Where(collection => consumable.amount <= collection.c.amount);
            }

            List <OfferResource <Consumable> > resources = new List <OfferResource <Consumable> >();
            var results = await query.ToListAsync();

            foreach (var data in results)
            {
                var resource = new Consumable().build(data.c);

                var yLatitude  = data.ap.Latitude;
                var yLongitude = data.ap.Longitude;
                var distance   = DistanceCalculator.computeDistance(location.Latitude, location.Longitude, yLatitude, yLongitude);
                if (distance > maxDistance && maxDistance != 0)
                {
                    continue;
                }
                resource.kilometer = (int)Math.Round(distance);

                var provider        = new Provider().Build(data.o);
                var providerAddress = new Address().Build(data.ap);

                provider.address = providerAddress;

                var offer = new OfferResource <Consumable>()
                {
                    resource = resource
                };

                // ---- HOTFIX
                // Vorerst sollen keine persönliche Daten veröffentlicht werden.
                provider.ispublic = false;
                // ---- END HOTFIX

                if (provider.ispublic)
                {
                    offer.provider = provider;
                }

                yield return(offer);
            }
        }