Пример #1
0
 public Consumable build(ConsumableDemandEntity c)
 {
     NullCheck.ThrowIfNull <ConsumableDemandEntity>(c);
     id           = c.id;
     category     = c.category;
     name         = c.name;
     manufacturer = c.manufacturer;
     amount       = c.amount;
     unit         = c.unit;
     annotation   = c.annotation;
     return(this);
 }
Пример #2
0
        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);
        }
Пример #3
0
        public async Task <IActionResult> ConsumableAnonymousContactAsync([FromBody] ContactInformationDemand contactInformationDemand, int id)
        {
            NullCheck.ThrowIfNull <ContactInformationDemand>(contactInformationDemand);

            try
            {
                _mailInputValidatorService.validateMail(contactInformationDemand.senderEmail);
                ConsumableDemandEntity consumable = await _resourceDemandQueryService.FindAsync(new ConsumableDemandEntity(), id);

                if (consumable is null)
                {
                    return(NotFound(FailureCodes.NotFoundDevice));
                }

                DemandEntity demand = await _resourceDemandQueryService.FindAsync(new DemandEntity(), consumable.demand_id);

                if (demand is null)
                {
                    return(NotFound(FailureCodes.NotFoundOffer));
                }

                var resourceNameDE = _languageDE.Consumable[consumable.category];
                var resourceNameEN = _languageEN.Consumable[consumable.category];

                var mailAddressReceiver  = demand.mail;
                var mailUserNameReceiver = demand.name;
                // TODO
                var region = "de";
                await _mailService.SendOfferMailToDemanderAsync(region, contactInformationDemand, mailAddressReceiver,
                                                                mailUserNameReceiver, resourceNameDE, resourceNameEN);

                await _mailService.SendDemandConformationMailToDemanderAsync(region, contactInformationDemand);

                return(Ok());
            }
            catch (ArgumentException e)
            {
                return(BadRequest(e.Message));
            }
        }
Пример #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);
            }
        }