Exemple #1
0
        public static SingleSubscription SubscribeToEventOnceAsync <T>(Action <EventHandler <T> > subscribe,
                                                                       Action <EventHandler <T> > unsubscribe,
                                                                       EventHandler <T> callback)
            where T : EventArgs
        {
            var result               = new SingleSubscription();
            var accessLock           = new object();
            var done                 = false;
            EventHandler <T> handler = null;

            handler = new EventHandler <T>(
                (o, e) =>
            {
                //Ensure no concurrent invocations of the event, though I'm not sure if .net allows for that
                lock (accessLock)
                {
                    //Check if we're done calling the event once.  If so, we don't want to invoke twice.
                    if (!done)
                    {
                        //We're now done.  Set the flag so we aren't called again.
                        done = true;

                        //Invoke the user's code for the one-time event subscription
                        callback(o, e);

                        //Signal that the user's code is done running, so the SubscribeToEventOnce caller
                        //thread can be unblocked.
                        result.Signal();

                        //Yay closures
                        unsubscribe(handler);
                    }
                }
            }
                );
            //Subscribe to the event which we are trying to listen to once
            subscribe(handler);
            return(result);
        }
        public async Task <string> MakeSingleJobSubscription(User user, string phone, long jobApplicationId)
        {
            //To be done in the controller

            try
            {
                decimal constantCharge = 100m;
                if (!(await ConfirmPhoneNumberAsync(phone)))
                {
                    return("Error confirming phone number");
                }
                if (!(await DebitPhoneAsync(phone, constantCharge)))
                {
                    return("Transaction failed");
                }

                Subscription newEntry = new SingleSubscription
                {
                    DateCreated      = DateTime.Now,
                    DateModified     = DateTime.Now,
                    SubscriptionType = Pitchme.Models.Enums.SubscriptionType.Single,
                    IsActive         = true,
                    PhoneNumber      = phone,
                    User             = user,
                    Amount           = constantCharge,
                    JobApplicationId = jobApplicationId
                };

                uow.jobSubscriptionRepository.Add(newEntry);
                uow.Save();
                return("Success");
            }
            catch (Exception ex)
            {
                //Log
                return("Error saving subscription, please contact admin");
            }
        }