/// <summary>
        /// Demonstrate the most efficient storage query - the point query - where both partition key and row key are specified.
        /// </summary>
        /// <param name="table">Sample table name</param>
        /// <param name="partitionKey">Partition key </param>
        /// <param name="rowKey">Row key </param>
        /// <returns>A Task object</returns>
        public async Task <TopicDetail> GetAllTopics()
        {
            try
            {
                common cmn = new common(_iconfiguration);
                // Create or reference an existing table
                CloudTable table = await cmn.CreateTableAsync("topic");

                TopicDetail topicDetail = new TopicDetail();

                TableQuery <Topic> tableQuery = new TableQuery <Topic>().Where(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "BhashaGuru"));

                //TableQuery<Topic> employeeQuery = new TableQuery<Topic>().Where(
                //TableQuery.CombineFilters(
                //TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey),
                //TableOperators.And,
                //TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, rowKey)));

                //retrives topic from the db
                var topics = await DataStorageUtils.ExecuteQueryAsync <Topic>(table, tableQuery);

                topicDetail.topic = topics;
                return(topicDetail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <string> ChangePassword(string loginId, string newPassword, string oldpassword)
        {
            try
            {
                common cmn = new common(_iconfiguration);
                // Create or reference an existing table
                CloudTable table = await cmn.CreateTableAsync("user");

                //check if older password exist
                var exitingUser = await DataStorageUtils.RetrieveEntityUsingPointQueryAsync(table, "BhashaGuru", loginId);

                var oldPasswordHash = OneWayHash.Create(oldpassword);
                if (oldPasswordHash.Equals(exitingUser.password))
                {
                    var newPasswordHash = OneWayHash.Create(newPassword);
                    //User user = new User(loginId, "BhashaGuru");
                    exitingUser.password = newPasswordHash;
                    exitingUser.isPasswordResetRequired = false;
                    exitingUser.isActive = true;
                    await DataStorageUtils.InsertOrMergeEntityAsync(table, exitingUser);

                    return(ApplicationMessage.PasswordChangedSuccessfully);
                }
                else
                {
                    return(ApplicationMessage.OldPasswordNotMatch);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <User> ValidateUserLogin(string loginId, string password)
        {
            try
            {
                common cmn = new common(_iconfiguration);
                // Create or reference an existing table
                CloudTable table = await cmn.CreateTableAsync("user");

                //check if older password exist
                var exitingUser = await DataStorageUtils.RetrieveEntityUsingPointQueryAsync(table, "BhashaGuru", loginId);

                if (exitingUser == null)
                {
                    return(null);
                }
                var  passwordHash      = OneWayHash.Create(password);
                bool isPasswordMatches = exitingUser.password.Equals(passwordHash);
                if (isPasswordMatches)
                {
                    return(exitingUser);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <string> ForgetPassword(string loginid)
        {
            try
            {
                common cmn = new common(_iconfiguration);
                // Create or reference an existing table
                CloudTable table = await cmn.CreateTableAsync("user");

                //check if older password exist
                var exitingUser = await DataStorageUtils.RetrieveEntityUsingPointQueryAsync(table, "BhashaGuru", loginid);

                var tempPassword = PasswordHelper.GetTempPassword(7);
                exitingUser.password = OneWayHash.Create(tempPassword);
                exitingUser.isPasswordResetRequired = true;
                await DataStorageUtils.InsertOrMergeEntityAsync(table, exitingUser);

                var fromEmailAddress = _iconfiguration.GetSection("Data").GetSection("SendGridFromEmailAddress").Value;
                var subject          = "Bhashaguru: Your new password for login";
                var plainContent     = "Your password for login is " + tempPassword + " kindly change password after login. Happy Learning :-) .";
                await _unicastEmailSender.SendUnicastMail(fromEmailAddress, exitingUser.emailId, subject, plainContent, String.Empty);

                return(ApplicationMessage.NewPasswordGenerated);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <string> AddUser(RegisterUser registerUser)
        {
            try
            {
                string tableName = "user";
                common cmn       = new common(_iconfiguration);
                // Create or reference an existing table
                CloudTable table = await cmn.CreateTableAsync(tableName);

                var exitingUser = await DataStorageUtils.RetrieveEntityUsingPointQueryAsync(table, "BhashaGuru", registerUser.emailId);

                if (exitingUser != null)
                {
                    return(ApplicationMessage.UserAlreadyExist);
                }
                else
                {
                    User user = new User(registerUser.emailId, "BhashaGuru");
                    user.firstName               = registerUser.firstName;
                    user.lastName                = registerUser.lastName;
                    user.emailId                 = registerUser.emailId;
                    user.createdDateTime         = new DateTime();
                    user.createdDateTime         = DateTime.UtcNow;
                    user.isActive                = false;
                    user.isPasswordResetRequired = true;
                    user.loginId                 = registerUser.emailId;
                    user.modifiedDateTime        = new DateTime();
                    user.modifiedDateTime        = DateTime.UtcNow;
                    user.photoUrl                = String.Empty;
                    user.userLanguage            = "english";

                    var tempPassword = PasswordHelper.GetTempPassword(7);
                    user.password = OneWayHash.Create(tempPassword);
                    var newUser = await DataStorageUtils.InsertOrMergeEntityAsync(table, user);

                    var fromEmailAddress = _iconfiguration.GetSection("Data").GetSection("SendGridFromEmailAddress").Value;
                    var subject          = "Bhashaguru: User registration and temporary password";
                    var plainContent     = "Thanks for registering with Bhashagru, your password for first login is " + tempPassword + " Happy Learning :-) .";
                    await _unicastEmailSender.SendUnicastMail(fromEmailAddress, user.emailId, subject, plainContent, String.Empty);

                    return(ApplicationMessage.UserRegisteredSuccessfully);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Demonstrate the most efficient storage query - the point query - where both partition key and row key are specified.
        /// </summary>
        /// <param name="table">Sample table name</param>
        /// <param name="partitionKey">Partition key </param>
        /// <param name="rowKey">Row key </param>
        /// <returns>A Task object</returns>
        public async Task <Topic> AddTopic(Topic topic)
        {
            try
            {
                string tableName = "topic";
                common cmn       = new common(_iconfiguration);
                // Create or reference an existing table
                CloudTable table = await cmn.CreateTableAsync(tableName);

                var exitingTopic = await DataStorageUtils.RetrieveEntityUsingPointQueryAsync(table, "BhashaGuru", topic.name);

                if (exitingTopic != null)
                {
                    return(null);
                }
                else
                {
                    Topic topicToAdd = new Topic(topic.name, "BhashaGuru");
                    topicToAdd.category         = topic.category;
                    topicToAdd.color            = topic.color;
                    topicToAdd.createdDateTime  = DateTime.UtcNow;
                    topicToAdd.description      = topic.description;
                    topicToAdd.imageUrl         = topic.imageUrl;
                    topicToAdd.language         = topic.language;
                    topicToAdd.modifiedDateTime = DateTime.UtcNow;
                    topicToAdd.name             = topic.name;
                    var newtopic = await DataStorageUtils.InsertOrMergeEntityAsync <Topic>(table, topicToAdd);

                    return(newtopic);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }