Example #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);
        }