Beispiel #1
0
        /// <summary>
        /// Inserts a new customer record in database.
        /// </summary>
        public CreateCustomerResponse CreateCustomer(long hostId, CreateCustomerRequest value)
        {
            CreateCustomerResponse result = new CreateCustomerResponse();

            long newCustomerId = 0;

            //Generate address
            string customerAddress = HashUtility.GenerateHash();

            using (var context = new AnnoDBContext())
            {
                //Insert customer to database
                Customer customer = new Customer()
                {
                    host_id       = hostId,
                    ref_id        = value.ReferenceId,
                    address       = customerAddress,
                    record_status = RecordStatuses.Live,
                    created_date  = DateTime.UtcNow
                };
                context.Customer.Add(customer);
                context.SaveChanges();

                //Get the Id of the newly created customer
                newCustomerId = customer.customer_id;
            }

            //Create customer wallet
            WalletServices walletServices = new WalletServices();

            walletServices.CreateWallet(newCustomerId, WalletOwnerTypes.Customer, customerAddress);

            //Commit to blockchain
            IdentityServices   identityService    = new IdentityServices();
            BlockchainContract blockchainContract = new BlockchainContract();

            blockchainContract.CreateCustomer(identityService.AddressOf(IdentityServices.AddressTypes.Host, hostId), customerAddress, value.ReferenceId);

            //TODO: For demo purpose, give 1000 ANN tokens to new customers
            walletServices.Transfer(Config.OwnerScriptHash, customerAddress, 1000, null, "Demo");

            //Commit to blockchain
            blockchainContract.Transfer(Config.OwnerScriptHash, customerAddress, 1000);

            result.WalletAddress = customerAddress;
            result.WalletBalance = 1000;

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Inserts a new host record in database.
        /// </summary>
        public void CreateHost(CreateHostRequest value, out string newAPIKey)
        {
            long newHostId = 0;

            //Generate address
            string hostAddress = HashUtility.GenerateHash();

            using (var context = new AnnoDBContext())
            {
                //Insert host to database
                var newHost = new Host()
                {
                    name          = value.Name,
                    address       = hostAddress,
                    record_status = RecordStatuses.Live,
                    created_date  = DateTime.UtcNow
                };
                context.Host.Add(newHost);
                context.SaveChanges();

                //Get the ID of the newly created host
                newHostId = newHost.host_id;

                //Generate new API key
                newAPIKey = Guid.NewGuid().ToString().Replace("-", "");

                //Insert into api_keys table
                context.ApiKey.Add(new ApiKey()
                {
                    host_id       = newHostId,
                    api_key       = newAPIKey,
                    record_status = RecordStatuses.Live,
                    created_date  = DateTime.UtcNow
                });
                context.SaveChanges();

                //Create host wallet
                WalletServices walletService = new WalletServices();
                walletService.CreateWallet(newHostId, WalletOwnerTypes.Host, hostAddress);
            }

            //Commit to blockchain
            BlockchainContract blockchainContract = new BlockchainContract();

            blockchainContract.CreateHost(hostAddress, value.Name);
        }
Beispiel #3
0
        /// <summary>
        /// Inserts a new event record in the Events table.
        /// </summary>
        public void CreateEvent(long hostId, CreateEventsRequest value)
        {
            long          newEventId    = 0;
            List <string> tierAddresses = new List <string>();

            //Generate address
            string eventAddress = HashUtility.GenerateHash();

            using (var context = new AnnoDBContext())
            {
                //Insert event to database
                var newEvent = new Events()
                {
                    host_id       = hostId,
                    ref_id        = value.ReferenceId,
                    title         = value.Title,
                    description   = value.Description,
                    start_date    = value.StartDate,
                    status        = "Active",
                    address       = eventAddress,
                    record_status = RecordStatuses.Pending,
                    created_date  = DateTime.UtcNow
                };
                context.Events.Add(newEvent);
                context.SaveChanges();

                //Get the ID of the newly created record
                newEventId = newEvent.event_id;

                //Insert event tiers to database
                foreach (var tier in value.Tiers)
                {
                    //Generate address
                    string tierAddress = HashUtility.GenerateHash();

                    //Insert event to database
                    var newEventTier = new EventsTier()
                    {
                        host_id           = hostId,
                        event_id          = newEventId,
                        ref_id            = tier.ReferenceId,
                        title             = tier.Title,
                        description       = tier.Description,
                        total_tickets     = tier.TotalTickets,
                        available_tickets = tier.TotalTickets, //available tickets is set to total tickets for initial insert
                        price             = Convert.ToDecimal(tier.Price),
                        status            = "Active",
                        address           = tierAddress,
                        record_status     = RecordStatuses.Live,
                        created_date      = DateTime.UtcNow
                    };
                    context.EventsTier.Add(newEventTier);
                    context.SaveChanges();

                    tierAddresses.Add(tierAddress);
                }

                //Update event record status to live
                var record = context.Events.SingleOrDefault(x => x.event_id == newEventId);
                if (record != null)
                {
                    record.record_status = RecordStatuses.Live;
                    context.SaveChanges();
                }
            }

            //Create event wallet
            WalletServices walletServices = new WalletServices();

            walletServices.CreateWallet(newEventId, WalletOwnerTypes.Event, eventAddress);

            //Commit to blockchain
            IdentityServices identityServices = new IdentityServices();
            string           hostAddress      = identityServices.AddressOf(IdentityServices.AddressTypes.Host, hostId);

            BlockchainContract blockchainContract = new BlockchainContract();

            blockchainContract.CreateEvent(eventAddress, hostAddress, value.ReferenceId, value.Title, value.StartDate.Value, "Active");

            for (int i = 0; i < value.Tiers.Count; i++)
            {
                blockchainContract.CreateEventTier(
                    tierAddresses[i], hostAddress, eventAddress, value.Tiers[i].ReferenceId, value.Tiers[i].Title,
                    value.Tiers[i].TotalTickets.Value, value.Tiers[i].TotalTickets.Value, value.Tiers[i].Price.Value);
            }
        }