示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ValidateUser(string login, string password)
        {
            var asyncCallStatus = new AsyncCallStatus <ValidateUserCompletedEventArgs>();

            Client.ValidateUserCompleted += (sender, args) =>
            {
                var status = args.UserState as AsyncCallStatus <ValidateUserCompletedEventArgs>;
                if (status != null)
                {
                    status.CompletedEventArgs = args;
                }

                AutoResetEvent.Set();
            };

            Client.ValidateUserAsync(login, password, asyncCallStatus);

            AutoResetEvent.WaitOne();

            if (asyncCallStatus.CompletedEventArgs.Error != null)
            {
                throw asyncCallStatus.CompletedEventArgs.Error;
            }
            return(asyncCallStatus.CompletedEventArgs.Result);
        }
        /// <summary>
        ///  Insert new category in service.
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public bool AddCategory(Category category)
        {
            try
            {
                var json = JsonConvert.SerializeObject(category);


                var asyncCallStatus = new AsyncCallStatus <AddCategoryCompletedEventArgs>();
                Client.AddCategoryCompleted += (sender, args) =>
                {
                    var status = args.UserState as AsyncCallStatus <AddCategoryCompletedEventArgs>;
                    if (status != null)
                    {
                        status.CompletedEventArgs = args;
                    }

                    AutoResetEvent.Set();
                };

                Client.AddCategoryAsync(json);
                AutoResetEvent.WaitOne();

                return(true);
            }
            catch (Exception operationException)
            {
                CurrentException = operationException;
                return(false);
            }
        }
        /// <summary>
        /// Retreview default category for aplication
        /// </summary>

        public void GetAllCategoryInCloud()
        {
            try
            {
                var categoryRepository = DependencyResolver.Container.GetService <ICategoryRepository>();

                var asyncCallStatus = new AsyncCallStatus <GetAllCategoryCompletedEventArgs>();
                Client.GetAllCategoryCompleted += (sender, args) =>
                {
                    var status = args.UserState as AsyncCallStatus <GetAllCategoryCompletedEventArgs>;
                    if (status != null)
                    {
                        status.CompletedEventArgs = args;
                    }

                    AutoResetEvent.Set();
                };

                Client.GetAllCategoryAsync(string.Empty, asyncCallStatus);
                AutoResetEvent.WaitOne();

                List <Category> categories = JsonConvert.DeserializeObject <List <Category> >(asyncCallStatus.CompletedEventArgs.Result);



                var allCategoryDataBase = categoryRepository.FindAll().ToList();
                foreach (var cat in categories)
                {
                    if (!allCategoryDataBase.Where(p => p.Description == cat.Description).Any())
                    {
                        Repository.Save(cat);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// Adds new expense.
        /// </summary>
        /// <returns><c>true</c>, if expense was added, <c>false</c> otherwise.</returns>
        /// <param name="expense">Expense.</param>
        public bool AddExpense(Expense expense)
        {
            try
            {
                var json = JsonConvert.SerializeObject
                           (
                    new ExpensiveProxy
                {
                    Id          = expense.Id,
                    Description = expense.Description,
                    Value       = expense.Value,
                    Category    = expense.Category.Id.ToString(),
                    Date        = expense.Date.ToString()
                }
                           );
                var asyncCallStatus = new AsyncCallStatus <AddExpenseCompletedEventArgs>();
                Client.AddExpenseCompleted += (sender, args) =>
                {
                    var status = args.UserState as AsyncCallStatus <AddExpenseCompletedEventArgs>;
                    if (status != null)
                    {
                        status.CompletedEventArgs = args;
                    }

                    AutoResetEvent.Set();
                };

                Client.AddExpenseAsync(json, asyncCallStatus);
                AutoResetEvent.WaitOne();

                return(true);
            }
            catch (Exception operationException)
            {
                CurrentException = operationException;
                return(false);
            }
        }