/// <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); }
/// <summary> /// Inserts a new event record in the Events table. /// </summary> public void CreateEvent(long hostId, CreateEventsRequest value) { long newEventId = 0; List <string> eventTierUniqueIdList = new List <string>(); //Generate address string eventUniqueId = 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 = eventUniqueId, 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 eventTierUniqueId = 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 = eventTierUniqueId, record_status = RecordStatuses.Live, created_date = DateTime.UtcNow }; context.EventsTier.Add(newEventTier); context.SaveChanges(); eventTierUniqueIdList.Add(eventTierUniqueId); } //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.SaveWallet(newEventId, WalletOwnerTypes.Event, eventUniqueId); //Commit to blockchain IdentityServices identityServices = new IdentityServices(); string hostAddress = identityServices.AddressOf(IdentityServices.AddressTypes.Host, hostId); ContractApi blockchainContract = new ContractApi(); blockchainContract.CreateEvent(eventUniqueId, hostAddress, value.ReferenceId, value.Title, value.StartDate.Value, "Active"); for (int i = 0; i < value.Tiers.Count; i++) { blockchainContract.CreateEventTier( eventTierUniqueIdList[i], hostAddress, eventUniqueId, value.Tiers[i].ReferenceId, value.Tiers[i].Title, value.Tiers[i].TotalTickets.Value, value.Tiers[i].TotalTickets.Value, value.Tiers[i].Price.Value); } }