示例#1
0
 public Device Build(DeviceDemandEntity d)
 {
     NullCheck.ThrowIfNull <DeviceDemandEntity>(d);
     id           = d.id;
     category     = d.category;
     name         = d.name;
     manufacturer = d.manufacturer;
     amount       = d.amount;
     annotation   = d.annotation;
     return(this);
 }
        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> DeviceAnonymousContactAsync([FromBody] ContactInformationDemand contactInformationDemand, int id)
        {
            NullCheck.ThrowIfNull <ContactInformationDemand>(contactInformationDemand);

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

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

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

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

                var resourceNameDE = _languageDE.Device[device.category];
                var resourceNameEN = _languageEN.Device[device.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 <Device> > QueryDemandsAsync(Device dev)
        {
            NullCheck.ThrowIfNull <Device>(dev);

            var device = new DeviceDemandEntity().Build(dev);

            var           maxDistance = dev.kilometer;
            AddressEntity locationOfDemandedDevice = null;

            if (dev.address.ContainsInformation())
            {
                var deviceAddress = dev.address;
                locationOfDemandedDevice = new AddressEntity().build(deviceAddress);
                _addressMaker.SetCoordinates(locationOfDemandedDevice);
            }


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

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

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

            if (device.amount > 0)
            {
                query = query.Where(collection => device.amount <= collection.d.amount);
            }

            List <DemandResource <Device> > resources = new List <DemandResource <Device> >();
            var results = await query.ToListAsync();

            foreach (var data in results)
            {
                var resource = new Device().Build(data.d);

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

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

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

                yield return(demand);
            }
        }